file_get_contents正在从Facebook获取用户图片。直到最近才工作正常。现在,它在一分钟后总是超时,我的Apache2 error.log中出现此错误:
PHP警告:file_get_contents(https://graph.facebook.com/999999999/picture?width=200):无法打开流:连接超时
这是代码(我最近添加了$ context以查看它是否正常工作。它没有):
$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但它不起作用:
$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);
以上代码也可以获取以下图像文件:
上述代码无法获取以下图像文件:
有谁知道为什么我可以从某些网站获取图像文件而不是其他网站?
答案 0 :(得分:1)
出于我的目的,即从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会重定向到另一个网址。