2011-11-14 141 views
0

的file_get_contents返回的URL为空字符串:当它是obviosly不是空http://thepiratebay.org/search/aPHP的GET请求返回空

。 也试过袅袅,我的继承人代码

$ch = curl_init(); 
$cookieFile = 'cookies.txt'; 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_HEADER, true); 
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); 
curl_setopt($ch, CURLOPT_TIMEOUT, 30); 
curl_setopt($ch, CURLOPT_TIMEOUT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'); 

$url = 'http://thepiratebay.org/search/a'; 
curl_setopt($ch, CURLOPT_URL,$url); 

$html = curl_exec ($ch); 
var_dump($html); 
$html = file_get_contents($url); 
var_dump($html); 

curl_close ($ch); unset($ch); 

输出为:

string(143) "HTTP/1.1 200 OK 
X-Powered-By: PHP/5.3.8 
Content-type: text/html 
Content-Length: 0 
Date: Mon, 14 Nov 2011 20:27:01 GMT 
Server: lighttpd 

" 
string(0) "" 

如果我更改URL为“http://thepiratebay.org/search”删去了2个字符一切正常,并我得到了很好的回应。

什么想法?

回答

2

问题是,您正尝试使用CURLOPT_TIMEOUT设置用户代理字符串。尝试使用CURLOPT_USERAGENT,这应该可以解决您的问题。如果您更愿意使用file_get_contents,则可以使用stream_context_createini_set同时执行此操作。

所有三种技术的示例可在http://www.seopher.com/articles/how_to_change_your_php_user_agent_to_avoid_being_blocked_when_using_curl获得。

+0

lolz,愚蠢的错误,ty,这是CURLOPT_USERAGENT,我结束了在python中写这个:) – Legion

0

file_get_contents可能无法在您的安装中打开URL。见fopen_wrappers。或者,由于file_get_contents调用中的用户代理,thepiratebay.org可能会阻止您。尝试通过context。或者可能它没有返回任何东西,因为您没有像那样在您的file_get_contents调用中发送任何cookie。底线,即使URL是相同的,请求是不同的。