Search code examples
phphttpgethttp-1.1

GET HTTP 1.1 error 400 with PHP background task


I have a GET request being created to run a background task in PHP.

The code is working on my development machine but fails on a production environment.

GET http://localhost:8080/hms/controllers/background/bday.php HTTP 1.1 Host: localhost Connection: Close HTTP/1.1 

On echoing echo fgets($socketcon, 128); I get the error below.

400 Bad Request Date: Mon, 08 Jul 2019 05:47:01 GMT Server: Apache/2.4.33 
(Win32) OpenSSL/1.0.2o PHP/5.6.36 Vary: accept-language,accept-charset 
Accept-Ranges: bytes Connection: close Content-Type: text/html; charset=utf- 
8 Content-Language: en Expires: Mon, 08 Jul 2019 05:47:01 GMT

My code

    $host = 'localhost';
    $remote_house = 'http://localhost:'.APACHEPORT.'/hms/controllers/background';

    $socketcon = fsockopen($host, APACHEPORT);
    if($socketcon) {   
        $socketdata = "GET $remote_house/".$scriptName." HTTP 1.1\r\nHost: $host\r\nConnection: Close\r\n\r\n";  
        fwrite($socketcon, $socketdata); 
        fclose($socketcon);
    }

Solution

  • Managed to do the same with CURL with 1 millisecond timeout which effectively makes it run in the background.

    $curl = curl_init();
        curl_setopt_array($curl, array(
            CURLOPT_RETURNTRANSFER => 1,
            CURLOPT_URL => $remote_house.$scriptName,
            CURLOPT_USERAGENT => 'User Agent X',
            CURLOPT_TIMEOUT_MS => 1
        ));
        $resp = curl_exec($curl);
        curl_close($curl);