使用PHP后台任务获取HTTP 1.1错误400

时间:2019-07-08 06:33:54

标签: php http get http-1.1

我正在创建一个GET请求,以在PHP中运行后台任务。

该代码在我的开发计算机上正常运行,但在生产环境中失败。

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

关于回显fgets($ socketcon,128);我收到以下错误。

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

我的代码

    $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);
    }

1 个答案:

答案 0 :(得分:0)

设法对CURL进行1毫秒的超时操作,从而有效地使其在后台运行。

$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);