Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php-cURL响应头与浏览器响应头不同_Php_Curl_User Agent_Httpresponse - Fatal编程技术网

php-cURL响应头与浏览器响应头不同

php-cURL响应头与浏览器响应头不同,php,curl,user-agent,httpresponse,Php,Curl,User Agent,Httpresponse,我想使用cURL发送请求并检索响应头 使用浏览器时,响应标题如下所示: HTTP/1.0 302 Moved Temporarily Content-Type: text/html; charset=utf-8 Cache-Control: no-cache Location: "Correct URL" Expires: Fri, 01 Jan 1990 00:00:00 GMT Date: Tue, 30 Oct 2012 08:32:24 GMT Server: Google Fronte

我想使用cURL发送请求并检索响应头

使用浏览器时,响应标题如下所示:

HTTP/1.0 302 Moved Temporarily
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Location: "Correct URL"
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Date: Tue, 30 Oct 2012 08:32:24 GMT
Server: Google Frontend
Content-Length: 0
HTTP/1.1 302 Found
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Location: "Wrong URL"
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Date: Tue, 30 Oct 2012 09:12:14 GMT
Server: Google Frontend
Content-Length: 0
但当我使用cURL发送请求时,响应头如下所示:

HTTP/1.0 302 Moved Temporarily
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Location: "Correct URL"
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Date: Tue, 30 Oct 2012 08:32:24 GMT
Server: Google Frontend
Content-Length: 0
HTTP/1.1 302 Found
Content-Type: text/html; charset=utf-8
Cache-Control: no-cache
Location: "Wrong URL"
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Date: Tue, 30 Oct 2012 09:12:14 GMT
Server: Google Frontend
Content-Length: 0
我想知道是什么导致响应返回不同的URL。这是一个小php示例,我尝试了很多示例,但都没有成功

<?php
    $url = "url";
    $ch = curl_init( $url );
    curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
    curl_setopt( $ch, CURLOPT_HEADER, true );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $ch, CURLOPT_COOKIEJAR, "cookie.txt" );
    curl_setopt( $ch, CURLOPT_COOKIEFILE, "cookie.txt" );
    curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.4) Gecko/20091030 Gentoo Firefox/3.5.4" );
    list( $header, $contents ) = preg_split( '/([\r\n][\r\n])\\1/', curl_exec( $ch ), 2 );
    curl_close( $ch );

    $header_text = preg_split( '/[\r\n]+/', $header );
    foreach ( $header_text as $headers ) {
        echo $headers . "</br>";
    }
?>

通过浏览器发送的请求与通过curl发送的请求(几乎可以肯定是在HTTP头中)之间存在一些差异,这导致了响应的差异


您应该从浏览器捕获请求(为了方便起见,可能使用类似Fiddler的HTTP代理),并将其头与来自curl请求的头进行比较。您将发现的一个(或多个)差异就是您看到这些差异的原因。

从浏览器捕获请求(为了方便起见,可能使用类似Fiddler的HTTP代理),并将其标题与您的curl请求的标题进行比较。你会发现的一个(或多个)差异就是不同反应的原因;可能浏览器在将数据输出给用户之前正在重写数据?我认为您的浏览器正在使用代理,而cURL没有。这是正确的吗?你确定你的url格式正确吗?@Jon:你完全正确,我不知道我怎么会错过这个。现在一切都正常了,我已经修复了差异(
curl\u setopt($ch,CURLOPT\u HTTPHEADER,array('Host:xxx','Referer:xxx'));
)。我想你发表评论作为回答,这样我就可以结束这个问题了。再次感谢(大家:D)。