使用php时在http响应中使用Stange字符串

时间:2014-11-14 03:16:04

标签: php httpresponse

我的PHP代码一直运行良好。突然我在响应数据中得到了非常奇怪的字符串。 每个响应数据都如下所示。其中“87,0”和“3e,0”来自哪里?我怎样才能摆脱它们?(它们出现在每个响应数据之前和之后。)

HTTP/1.1 200 OK
Date: Fri, 14 Nov 2014 01:57:40 GMT
Server: Apache
X-Powered-By: PHP/5.4.34
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

87
a:1:{i:0;s:116:"http://www.nytimes.com/2014/09/15/sports/basketball/united-states-wins-fiba-world-cup-title-in-a-rout-of-serbia.html";}
0

HTTP/1.1 200 OK
Date: Fri, 14 Nov 2014 01:57:40 GMT
Server: Apache
X-Powered-By: PHP/5.4.34
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html

3e
a:1:{i:0;s:44:"http://www.bbc.com/news/in-pictures-29204063";}
0


foreach($template_arr as $tkey => $template)
    {
        $keyword = self::getKeyword($template, $typename);
        $keyword = urlencode($typename);//encode because it is going to be send through url         
        $urlPre = self::getURLPre('news');
        $urlrule = self::getURLRule($template); //actually it is used in both siteSearch  and urlrule,but urlrule seems unnecessary

        $s = stream_socket_client("$sochost:$socportno", $errno, $errstr, $soctimeout, STREAM_CLIENT_ASYNC_CONNECT | STREAM_CLIENT_CONNECT);
        if ($s)
        {
            $sockets[$template] = $s;
            $out = "GET /DataAC/Search/Search_Bridge_G.php?keyword=$keyword&resultSize=$resultSize&urlPre=$urlPre&urlrule=$urlrule&template=$template";
            $out.= " HTTP/1.1\r\n";
            $out.= "Connection: Close\r\n";
            $out.= "Host:$sochost\r\n\r\n";
            fwrite($s, $out);
        }
        else
        {
            echo "$errstr ($errno) ,open sock on search keyword @ $keyword on template @ $tempalte erro when open search bridge.<br/>\n";
        }
    }

    while (count($sockets))
    {
        $read = $sockets;
        $w = null;
        $e = null;
        stream_select($read, $w, $e, $soctimeout);
        if (count($read))
        {
            /* stream_select generally shuffles $read, so we need to
            compute from which socket(s) we're reading. */
            foreach($read as $r)
            {
                $template = array_search($r, $sockets);
                $data = fread($r, $convenient_read_block);

                // A socket is readable either because it has
                //  data to read, OR because it's at EOF.

                if (strlen($data) == 0)
                {
                    //echo "Template " . $template . " closes at " . date('h:i:s') . "</br>";
                    $responseData = $result[$template];
                    echo "!!!!!!!!!!!!!!!$responseData!!!!!!!!!!!!!!!";
                    $responseArray=unserialize(substr($responseData,strpos($responseData,"\r\n\r\n")+4)); //get serialize array from respose data and unserialize it ,"+4" means  we don't need \r\n\r\n
                    //var_dump($responseArray);

                    $urlarr = $responseArray;

                    // assemble result array start
                    if (!empty($urlarr))
                    {
                        $template_url_arr[$template] = $urlarr[0];
                        $timenow = date('Y-m-d H:i:s', time());
                        echo "</br>######### Machine search : keyword:@" . $typename . " target url:" . $urlarr[0] . " timestamp: $timenow" . "#########</br>";
                    }
                    else
                    {
                        echo "</br> ######### Template url arr empty when query keyword @" . $keyword . " under template @" . $template . " #########</br>";                            
                    }
                    // assemble result array end
                    fclose($r);
                    unset($sockets[$template]);
                }
                else
                {
                    if(!isset($result[$template]))//init the array element or there will be a warning
                    $result[$template]= "";
                    $result[$template].= $data;
                }
            }
        }
        else
        {
            /* A time-out means that *all* streams have failed
            to receive a response. */
            echo "Time-out!\n";
            break;
        }
    }


script:Search_Bridge_G.php

require_once '../../global.php';
//require_once('UTIL/MyFileUtil.php');
//require_once('UTIL/MyDocUtil.php');
require_once 'DataAC/Search/GetGSearchLink.php';
error_reporting(E_ALL); 


$keyword=$_GET['keyword'];
$keyword=preg_replace('/\s+/', '%20', trim($keyword)); //replace ' ' with %20
$resultSize=$_GET['resultSize'];
$urlPre=$_GET['urlPre'];
$urlrule=$_GET['urlrule'];
$template=$_GET['template'];

$ant = new \DataGenerator\GetLinkG\GetGSearchLink($keyword,$resultSize,  $urlPre,$urlrule);
$urlarr = $ant->getFilteredURL($template);
/*
echo "</br>key:".$keyword."</br>";
echo "</br>rsize:".$resultSize."</br>";
echo "</br>urlpre:".$urlPre."</br>";
echo "</br>urlrule:".$urlrule."</br>";
echo "</br>templa:".$template."</br>";
*/
//var_dump($urlarr);
echo serialize($urlarr);

1 个答案:

答案 0 :(得分:1)

当HTTP服务器无法提前确定Content-Length:时,例如当输出由动态脚本而不是静态文件生成时,它将以块的形式发送输出(如{{ 1}}标题)。每个块的前缀都以十六进制数字表示。这些是&#34;奇怪的字符串&#34;你明白了具体细节可在以下网址找到:

RFC 7230: Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing