Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.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 - Fatal编程技术网

使用PHP和Curl获得网页的不同结果

使用PHP和Curl获得网页的不同结果,php,curl,Php,Curl,如果我使用此代码获取网页- $url="http://sourceforge.net/projects/freetype/files/"; $html = @file_get_contents($url) or die("Could not access file: $url"); 然后使用以下内容搜索页面: $dom = new DOMDocument; @$dom->loadHTML($html); $xpath = new DOMXPath($dom); $nodes = $xpa

如果我使用此代码获取网页-

$url="http://sourceforge.net/projects/freetype/files/";
$html = @file_get_contents($url) or die("Could not access file: $url");
然后使用以下内容搜索页面:

$dom = new DOMDocument;
@$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//a[contains(@href,'download?source=files')]");
foreach($nodes as $href) {
if (fnmatch("*.tar.xz", $href->getAttribute('href'))) {
    echo $href->getAttribute('href'), PHP_EOL;
} elseif (fnmatch("*.tar.bz2", $href->getAttribute('href'))) {
    echo $href->getAttribute('href'), PHP_EOL;
} elseif (fnmatch("*.tar.gz", $href->getAttribute('href'))) {
    echo $href->getAttribute('href'), PHP_EOL;
} elseif (fnmatch("*.tgz", $href->getAttribute('href'))) {
    echo $href->getAttribute('href'), PHP_EOL;
} elseif (fnmatch("*.zip", $href->getAttribute('href'))) {
    echo $href->getAttribute('href'), PHP_EOL;
} else {
    echo $href->getAttribute('title'), PHP_EOL;
}
}
我得到的结果是:

/freetype2/2.5.5/freetype-2.5.5.tar.bz2:  released on 2014-12-30 21:42:44 UTC
这是正确的

如果我使用这个curl代码得到相同的页面-

function getPage($url, $proxy) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT   6.0; en-US; rv:1.9.0.6) Gecko/ 2009011913 Firefox/3.0.6');
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
// $result contains the output string
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
然后使用与上面相同的DOM代码搜索页面,我得到-

/freetype2/2.5.5/ft255.zip:  released on 2014-12-30 21:42:56 UTC
这是不正确的。在浏览器中使用“查看页面源代码”的页面上没有ft255.zip的实例,结果中的时差表明我们检索到了不同的页面,或者可能只是更多的html


curl代码有什么问题,或者如果没有什么明显的问题,我应该如何调试它?

浏览器类型是什么。zip适用于pc。gz适用于linux/mac


您可以假装自己想要什么

Sourceforge根据提供的UserAgent为您的操作系统提供最适合的归档类型:

使用Windows UA,您可以获得.zip文件:

$ curl -s -A "Mozilla/5.0 ;Windows NT 6.3; WOW64; Trident/7.0; rv:11.0; like Gecko" http://sourceforge.net/projects/freetype/files/ | grep "/freetype2/2.5.5/"
<a href="/projects/freetype/files/latest/download?source=files" title="/freetype2/2.5.5/ft255.zip:  released on 2014-12-30 21:42:56 UTC">
使用Linux UA,您可以获得一个.tar.bz2:

$ curl -s -A "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36" http://sourceforge.net/projects/freetype/files/ | grep "/freetype2/2.5.5/"
<a href="/projects/freetype/files/latest/download?source=files" title="/freetype2/2.5.5/freetype-2.5.5.tar.bz2:  released on 2014-12-30 21:42:44 UTC">

所以这两个结果都是正确的,但是您得到了zip,因为您在CURLOPT_UserAgent中提供了一个Windows UserAgent。

谢谢您的回答。用户代理确实是个问题。正如你所建议的, 我把它改成-

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/37.0 (X11; U; Linux 3.19.3 i686, en) Gecko/20150410 Firefox/37.0.2');

现在我返回了tar.gz版本。

查看这两种情况下的完整响应。可能web服务器正在发送不同的响应,即它尝试发送为用户的浏览器或操作系统等定制的页面。您可能希望阅读帮助页面。