使用cURL无法正常工作从URL保存Facebook个人资料图片2018年3月27日之后

时间:2018-03-28 07:55:00

标签: php facebook-graph-api

我使用以下代码:

$json_link = "https://graph.facebook.com/v2.12/me?fields=id,picture.width(300).height(280)&access_token=".$accessToken.""; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $json_link);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$output = curl_exec($ch);
curl_close($ch);
$obj = json_decode($output);
$xx=$obj->picture->data->url;
$src=imagecreatefromstring(file_get_contents($xx));

$obj返回以下内容:

stdClass Object ( 
    [id] => idxxxxx 
    [picture] => stdClass Object ( 
        [data] => stdClass Object ( 
            [height] => 480 
            [is_silhouette] =>
            [url] => lookaside.facebook.com/platform/profilepic/
            [width] => 415 
        ) 
    ) 
)

但它失败了:

$xx=$obj->picture->data->url;
$src=imagecreatefromstring(file_get_contents($xx));

我明白了:

enter image description here

失败了:

imagecreatefromstring(file_get_contents($xx)); 

它在3月27日之前运行良好,但在图表api更新后它不再有效。

2 个答案:

答案 0 :(得分:2)

FB正在寻找用户代理信息。

$User_Agent = 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';

$request_headers = array();
   $request_headers[] = 'User-Agent: '. $User_Agent;
   $request_headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  $r = curl_exec($ch);
  curl_close($ch);

它将解决您的问题。

答案 1 :(得分:0)

只放行:file_put_contents('file.jpg',fopen($ obj-> picture-> data-> url,'r'));