T
T
Timur Kalimullin2016-06-29 20:06:09
PHP
Timur Kalimullin, 2016-06-29 20:06:09

How to download user avatar from graph.facebook?

Good afternoon,
the site organizes user registration through social networks, including facebook.
To upload the facebook user avatar to the site, I use the URL:
graph.facebook.com/100001039666189/picture?width=5...
from the data object I get the cdn url containing a link to the avatar
https://scontent.xx.fbcdn.net/v/ t1.0-1/p720x720/11...
any attempt to download a file via curl or file_get_contents fails. As I understand it, the response from the server is 403
An example of one of the attempts to download an image:

$ID = '100001039666189';

$ch = curl_init("http://graph.facebook.com/'.$ID.'/picture?width=500&height=500&redirect=false");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$content 	= curl_exec($ch);
$data 		= json_decode($content, true);
curl_close($ch);

$get = file_get_contents($data['data']['url']);
$f = fopen('fb_'.$ID.'.jpg', 'w+');
fputs($f, $get);
fclose($f);

In fact, the image array is formed by url using the CFile::MakeFileArray method, but it returns [type] => unknown
What could be the problem?
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IceJOKER, 2016-06-29
@IceJOKER

Some kind of absurdity, instead of immediately getting a picture, first you get json with a link, and then you download it with one function, then you write another one to a file O_O

$ID = '100001039666189';
$url= "http://graph.facebook.com/{$ID}/picture?width=500&height=500";
$data = file_get_contents($url);
file_put_contents('filename.filetpe', $data);

Isn't that easier?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question