On 11/16/2016 3:50 AM, Aaron Lewis wrote:
I have a file that contains a HTTP request,

```
GET /xxx.php
Host: xxx
Content-Type: xxx
...
```

I would like to ask PHP cli to parse the HTTP request from a file, and
setup $_FILES, $_POST, $_SERVER etc.
What should I do? I'm familiar with PHP extensions, so I'm capable of
modifying SAPI myself.


The Ultimate Web Scraper Toolkit over on GitHub can get you about 75% of the way there in pure userland in a few lines of application code with the WebServer class:

https://github.com/cubiclesoft/ultimate-web-scraper/


Possible approach:

$fp = fopen("/path/to/file.txt", "rb");
stream_set_blocking($fp, 0);

$server = new WebServer();

// If you want to allow gzipped/deflated input.
//$server->EnableCompression(true);

// If you are actually processing file input...
//$server->SetCacheDir("/path/to/cache/");

$client = $server->InitNewClient();
$client->fp = $fp;

do
{
        $result = $server->Wait();

        if (count($result["removed"]))
        {
                // File contains an invalid request.
                // Maybe bail out more gently?
                echo "Invalid request.\n";

                exit();
        }
} while (!$client->requestcomplete);

// Now use $client's public variables 'cookievars' and 'requestvars' to load various superglobals. There isn't a pure structure though for $_GET and $_POST.
...


is_uploaded_file(), move_uploaded_file(), sesssions, and other useful built-ins might also not work as expected. Hence the 75% metric from earlier.

--
Thomas Hruska
CubicleSoft President

I've got great, time saving software that you will find useful.

http://cubiclesoft.com/

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to