curl error HTTP状态码0--来自服务器的空回复

时间:2014-04-21 00:06:59

标签: php curl

我正在尝试废弃一个网站,但它总是说Empty Reply from server 任何人都可以看code并告诉我我做错了什么?

以下是code

function spider($url){
        $header = array(
            "Host" => "www.example.net",
            //"Accept-Encoding:gzip,deflate,sdch",
            "Accept-Language:en-US,en;q=0.8",
            "Cache-Control:max-age=0",
            "Connection:keep-alive","Content-Length:725","Content-Type:application/x-www-form-urlencoded",
            'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'
            ,"X-Requested-With:XMLHttpRequest"
        );
        $cookie = "cookie.txt";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0); // return headers 0 no 1 yes
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return page 1:yes
        curl_setopt($ch, CURLOPT_TIMEOUT, 200); // http request time-out 20 seconds
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects, need this if the URL changes
        curl_setopt($ch, CURLOPT_MAXREDIRS, 2); //if http server gives redirection response
        curl_setopt($ch, CURLOPT_USERAGENT,
        "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7");
        curl_setopt($ch, CURLOPT_COOKIEJAR, realpath( $cookie)); // cookies storage / here the changes have been made
        curl_setopt($ch, CURLOPT_COOKIEFILE, realpath( $cookie));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // false for https
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS,"view=ViewDistrict&param=7&uniqueid=1397991494188&PHPSESSID=f134vrnv7glosgojvf4n1mp7o2&page=http%3A%2F%2Fwww.example.com%2Fxhr.php");
        curl_setopt($ch, CURLOPT_REFERER, $url);
        curl_setopt($ch, CURLOPT_REFERER, "http://www.example.com/");
        $data = curl_exec($ch); // execute the http request
        $info = curl_getinfo($ch);
        curl_close($ch); // close the connection

        return $data;
}

以下是function call

echo spider("http://www.example.net/");

修改

Array ( [url] => http://www.example.net/ [content_type] => text/html [http_code] => 301 [header_size] => 196 [request_size] => 840 [filetime] => -1 [ssl_verify_result] => 0 [redirect_count] => 1 [total_time] => 61.359 [namelookup_time] => 0 [connect_time] => 0.281 [pretransfer_time] => 0.281 [size_upload] => 0 [size_download] => 0 [speed_download] => 0 [speed_upload] => 0 [download_content_length] => 178 [upload_content_length] => 0 [starttransfer_time] => 60.593 [redirect_time] => 0.766 [certinfo] => Array ( ) [redirect_url] => ) Empty reply from server

现在这是标题 我也更新了我的post data 下雪了 curl_setopt($ch, CURLOPT_POSTFIELDS,"view=ViewDistrict&param=7&uniqueid=".time(). rand(101,500)."&PHPSESSID=f134vrnv7glosgojvf4n1mp7o2&page=http%3A%2F%2Fexample.com%2Fxhr.php");

并且还从"X-Requested-With:XMLHttpRequest"

中删除了headers

2 个答案:

答案 0 :(得分:0)

您是否尝试过删除此标题?

X-Requested-With:XMLHttpRequest

答案 1 :(得分:0)

我的猜测是你的问题在这一行:

curl_setopt(
    $ch,
    CURLOPT_POSTFIELDS,
    "view=ViewDistrict&param=7&uniqueid=1397991494188&PHPSESSID=f134vrnv7glosgojvf4n1mp7o2&page=http%3A%2F%2Fwww.example.com%2Fxhr.php"
);

请注意,您传递了PHPSESSID的值。我猜你复制了&粘贴访问该网站的URL,对吧?当你访问该网站时,该会话ID可能是有效的,但它现在有效的可能性非常小。如果服务器不喜欢会话ID,那么它很可能不会给你任何数据。