3
votes

Hi I m trying to create app that uses user's profile pic in it. So I write code that reads profile pic from facebook and save it on my server. I use following code

function GetImageFromUrl($link){
   $ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch,CURLOPT_URL,$link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($ch);
curl_close($ch);
return $result;
}

$userpicpath = "http://graph.facebook.com/$uid/picture?type=normal";

$sourcecode = GetImageFromUrl($userpicpath);
$savefile = fopen("$uid-normal.jpg", "w"); //this is name of new file that i save
fwrite($savefile, $sourcecode);
fclose($savefile);

Here $uid is id of user.

The above code doesn't work properly.

But when I copy the $userpicpath(ie http://graph.facebook.com/$uid/picture?type=normal) in browser and press enter it will return me new path to image in address bar and shows me proper image that I want. If i passed this new path in address bar to my function it saves image file that I want.

Why this is happening? How i get that second image path and pass it to my function in program. Please Help me.

Thanks.

4

4 Answers

3
votes

Facebook uses a redirect to allow for easy embedding on websites. It accomplishes this by sending a HTTP 302 redirect header. Since I see you are using a CURL, I have written my example based on a guide I have found online. I also posted how to send the user agent via cURL. Here's my getFBResponse() function for you, and can be used to replace $userpicpath's assignment. Try this: $userpicpath = getFBRedirect();

// Only calling the head
curl_setopt($ch, CURLOPT_HEADER, true); // header will be at output
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'HEAD'); // HTTP request is 'HEAD'

$content = curl_exec ($ch);

// The response should be:
/*
HTTP/1.1 302 Found
Location: http://www.iana.org/domains/example/
*/
// So splitting on "Location: " should give an array of index 2, the URL you want being the second index (1)
$theUrl = split($content, "Location: ");
return $content[1];

}

2
votes

Try setting the User-Agent header for your request. FB and many other services often refuse to serve request with no User-Agent set.

EDIT: It can be done like this:

curl_setopt($ch,CURLOPT_HTTPHEADER,array('User-Agent: AnythingYouLikeHere'));

EDIT 2: The part about redirects is also true. To let cURL automatically take care of redirect handling, you can do this:

curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
0
votes

Sounds like a redirection. Those can not be handled by PHP, you'll need to find out the real address of the picture to use GetImageFromUrl

0
votes

You can tell CURL to follow the redirect returned by the Facebook server by setting the CURLOPT_FOLLOWLOCATION option; the below should work as desired:

function GetImageFromUrl($link) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_URL, $link);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    # ADDED LINE:
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
}