continue processing php after sending http response

后端 未结 12 1132
旧时难觅i
旧时难觅i 2020-11-22 13:06

My script is called by server. From server I\'ll receive ID_OF_MESSAGE and TEXT_OF_MESSAGE.

In my script I\'ll handle incoming text and ge

12条回答
  •  清歌不尽
    2020-11-22 13:28

    in case of php file_get_contents use, connection close is not enough. php still wait for eof witch send by server.

    my solution is to read 'Content-Length:'

    here is sample :

    response.php:

     

    Note the "\n" in response to close line, if not the fget read while wait eof.

    read.php :

     'world'
    );
    $content = http_build_query($vars);
    
    fwrite($fp, "POST /response.php HTTP/1.1\r\n");
    fwrite($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
    fwrite($fp, "Content-Length: " . strlen($content) . "\r\n");
    fwrite($fp, "Connection: close\r\n");
    fwrite($fp, "\r\n");
    
    fwrite($fp, $content);
    
    $iSize = null;
    $bHeaderEnd = false;
    $sResponse = '';
    do {
        $sTmp = fgets($fp, 1024);
        $iPos = strpos($sTmp, 'Content-Length: ');
        if ($iPos !== false) {
            $iSize = (int) substr($sTmp, strlen('Content-Length: '));
        }
        if ($bHeaderEnd) {
            $sResponse.= $sTmp;
        }
        if (strlen(trim($sTmp)) == 0) {
            $bHeaderEnd = true;
        }
    } while (!feof($fp) && (is_null($iSize) || !is_null($iSize) && strlen($sResponse) < $iSize));
    $result = trim($sResponse);
    

    As you can see this script dosent wait about eof if content length is reach.

    hope it will help

提交回复
热议问题