Reputation: 46
I need the users image as image object within PHP.
The obvious choice would be to do the following:
$url = 'https://graph.facebook.com/'.$fb_id.'/picture?type=large';
$img = imagecreatefromjpeg($url);
This works on my test server, but not on the server this script is supposed to run eventually (allow_url_fopen is turned off there).
So I tried to get the image via curl:
function LoadJpeg($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
$fileContents = curl_exec($ch);
curl_close($ch);
$img = imagecreatefromstring($fileContents);
return $img;
}
$url = 'https://graph.facebook.com/'.$fb_id.'/picture?type=large';
$img = LoadJpeg($url);
This, however, doesn't work with facebook profile pictures.
Loading, for example, Googles logo from google.com using curl works perfectly.
Can someone tell me why or tell me how to achieve what I am trying to do?
Upvotes: 2
Views: 2379
Reputation: 331
You have to set
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
in this way you find the image without it you get a 302 response code without image because is in another position set in the field "url" of the response header.
Upvotes: 3
Reputation: 3323
allow_url_fopen
Spoof it like ...
// spoofing Chrome
$useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.13 (KHTML, wie z. B. Gecko) Chrome/13.0.782.215 Safari/525.13.";
$ch = curl_init();
// set user agent
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
// set the rest of your cURL options here
I do not have to mention this violates their TOS and might lead to legal problems, right? Also, make sure you follow their robots.txt
!
Upvotes: 1