Saving Facebook Profile picture from URL using cURL not working After March 27 ,2018

Im using the following code:

$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 returns the following:

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

But it is failing on:

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

I get:

enter image description here

So fails:

imagecreatefromstring(file_get_contents($xx)); 

It worked fine before March 27, but after graph api update its no longer working.

Upvotes: 2

Views: 824

Answers (2)

Aquiles Valenciano
Aquiles Valenciano

Reputation: 1

Only put the line: file_put_contents('file.jpg', fopen($obj->picture->data->url, 'r'));

Upvotes: 0

Parvinder Singh
Parvinder Singh

Reputation: 163

FB is looking for User-agent information.

$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);

It will solve your problem.

Upvotes: 2

Related Questions