PHP file_get_contents&curl可以从一些网站获取图像文件,但不能从其他网站获取图像文件。为什么?
PHP/Apache2中的file_get_contents正在从Facebook获取用户图片。直到最近它一直工作正常。现在,它总是超时一分钟后,这个错误在我的Apache2 error.log中:PHP file_get_contents&curl可以从一些网站获取图像文件,但不能从其他网站获取图像文件。为什么?
PHP的警告:的file_get_contents(https://graph.facebook.com/999999999/picture?width=200):未能打开流:连接超时
这里代码(我最近增加了$背景下,看它是否使工作它没有。):
$context = stream_context_create(array('https' => array('header'=>'Connection: close\r\n')));
$fbprofileimage = file_get_contents('https://graph.facebook.com/'.$id.'/picture?width=100',false,$context);
我试着卷曲它不工作:
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://graph.facebook.com/'.$id.'/picture?width=100');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'StockBet');
$fbprofileimage = curl_exec($curl_handle);
curl_close($curl_handle);
我发现file_get_contents & curl可以与一些网站一起使用,但不能与其他网站一起使用。
以下工作:
$context = stream_context_create(array('https' => array('header'=>'Connection: close\r\n')));
$fbprofileimage = file_get_contents('https://fm.cnbc.com/applications/cnbc.com/resources/img/editorial/2017/08/03/104629909-GettyImages-630953738-bitcoin.240x160.jpg?v=1501760634',false,$context);
$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'https://fm.cnbc.com/applications/cnbc.com/resources/img/editorial/2017/08/03/104629909-GettyImages-630953738-bitcoin.240x160.jpg?v=1501760634');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'StockBet');
$fbprofileimage = curl_exec($curl_handle);
curl_close($curl_handle);
上面的代码可以得到下面的图像文件,以及:
- https://b.thumbs.redditmedia.com/SSGv_d2P6ymZVIq8Bm5IVqpcz6WZioCfbRh5rxMPcTc.jpg(reddit的)
- https://try.alexa.com/wp-content/uploads/2017/01/alexa-logo.png
-
https://ir.ebaystatic.com/rs/v/fxxj3ttftm5ltcqnto1o4baovyl.png
网(eBay) - https://images-na.ssl-images-amazon.com/images/G/01/Gateway/Unrec/T2/Amazon_GW_DesktopShoveler_5_200x200.CB503347722.png
- https://bitcointalk.org/useravatars/avatar_7110.jpg https://try.alexa.com/wp-content/uploads/2017/01/alexa-logo.png
上述代码无法得到以下图像文件:
- https://www.google.ca/images/branding/googlelogo/2x/googlelogo_color_120x44dp.png
- https://s.yimg.com/uu/api/res/1.2/7rUl71NcpOwqZeJexHuZhA--/Zmk9c3RyaW07aD0xOTM7cHlvZmY9MDtxPTk1O3c9MjIwO3NtPTE7YXBwaWQ9eXRhY2h5b24-/http://media.zenfs.com/en-US/homerun/aol_com_127/ce17fc9999cd894bf3a3dac1416b3230(雅虎)
- https://c.disquscdn.com/next/a0cd712/marketing/assets/img/brand/disqus-logo-blue.svg(Disqus)
- https://pbs.twimg.com/profile_images/464794058229964800/uwlkErTI_bigger.png(微博)
- https://scontent-yyz1-1.xx.fbcdn.net/v/t45.1600-4/c0.31.284.149/p284x149/21337242_6083114387812_5783480223513182208_n.png?oh=3c78c007377ee96b844e91843dfdfddb&oe=5A183896 (脸谱)
- https://www.ibm.com/cloud-computing/images/ca-en-watson-2-09112017-600x260-p1v1.jpg
有谁知道为什么我可以从一些网站而不是其他人得到的图像文件?
对于我而言,这是从Facebook获取用户的照片,我把它通过使用下面的代码工作:
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // need confirmed
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // need confirmed. I think this is key, as Facebook redirects to another URL and we need to follow
//curl_setopt($ch, CURLOPT_ENCODING,""); // not needed confirmed
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1); // need confirmed
curl_setopt($ch, CURLOPT_TIMEOUT,1); // need confirmed
curl_setopt($ch, CURLOPT_FAILONERROR,true); // not needed confirmed
//curl_setopt($ch, CURLOPT_VERBOSE, true); // not needed confirmed
//curl_setopt($ch, CURLINFO_HEADER_OUT, true); // not needed confirmed
curl_setopt($ch, CURLOPT_HEADER, true); // need confirmed
$fbprofileimage = curl_exec($ch);
if (curl_errno($ch)){
echo 'Retreive Base Page Error: ' . curl_error($ch);
}
else {
//$info = rawurldecode(var_export(curl_getinfo($ch),true));
// Get the cookies:
$skip = intval(curl_getinfo($ch, CURLINFO_HEADER_SIZE));
//$responseHeader= substr($fbprofileimage,0,$skip);
$fbprofileimage= substr($fbprofileimage,$skip); // need confirmed
//echo "HEADER: $responseHeader\n"; // causes error
//echo "\n\nINFO: $info\n\nDATA: $fbprofileimage"; // causes error
}
我认为是关键的是:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
因为当获得https://graph.facebook.com/999999999/picture?width=200(其中999999999是用户的ID)时,Facebook将重定向到另一个URL。
谢谢!这确实是 curl_setopt($ ch,CURLOPT_FOLLOWLOCATION,true); –
您是否尝试过使用CURLOPT_VERBOSE记录curl请求?您还可以在curl_close之前使用curl_getinfo来查看是否报告了任何HTTP错误。 如果您省略用户代理或引用标头,那么这些网站很可能会阻止输出。 –
@RubenVincenten感谢您的回复。我为它达成目的。看到我下面发布的答案。 – Curt