Yousaf Hassan
Yousaf Hassan

Reputation: 494

Saving User's Profile Picture from facebook API - PHP SDK V.5

Scenario: I am working on an app through which i need to download the user's profile picture from Facebook, apply a specific filter and then re-upload and set it as profile picture, which is possible using this trick. 'makeprofile=1'

http://www.facebook.com/photo.php?pid=xyz&id=abc&makeprofile=1 

Problem: So the problem i am facing is while downloading the picture from the received URL via API. I am obtaining the picture URL this way:

$request = $this->fb->get('/me/picture?redirect=false&width=9999',$accessToken); // 9999 width for the desired size image

// return object as in array form
$pic = $request->getGraphObject()->asArray();

// Get the exact url
$pic = $pic['url'];

Now i want to save the image from obtained URL to a directory on my server, so that i can apply the filter and re-upload it. When i use file_get_contents($pic) it throws following error

file_get_contents(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed 

I have tried a few other methods as well but could not get this fixed. Any help would be appreciated :)

NOTE: I am doing this through Codeigniter and on localhost for now.

Upvotes: 2

Views: 2664

Answers (3)

Vy Hong
Vy Hong

Reputation: 51

Your question is a potential duplicate of this question.

I'm just gonna repeat elitechief21's answer here: you should NOT disable SSL certificate because that creates a security hole in your application.

But rather, you should download a list of trusted certificate authorities (CA) (e.g. curl.pem) and use it together with your file_get_contents, like this:

$arrContextOptions=array(
    "ssl"=>array(
        "cafile" => "/path/to/bundle/cacert.pem",
        "verify_peer"=> true,
        "verify_peer_name"=> true,
    ),
);
$profile_picture = @file_get_contents($picture_url, false, stream_context_create($arrContextOptions));

Upvotes: 3

Pankaj Monga
Pankaj Monga

Reputation: 179

you can use file_get_connects()

$json = file_get_contents('https://graph.facebook.com/v2.5/'.$profileId.'/picture?type=large&redirect=false');
$picture = json_decode($json, true);
$img = $picture['data']['url'];

where $profileId variable contain user profile id and type paramete can be square , large , small, normal according to your requirement which size you want

Now $img variable contain your image data. Save image file on server using file_put_contents()

$imagePath = 'path/imgfolder';  //E.g assets/images/mypic.jpg
file_put_contents($imagePath, $img); 

Upvotes: 0

Yousaf Hassan
Yousaf Hassan

Reputation: 494

So i found the solution myself and decided to answer so that if that can help someone else facing the same problem.

We need to pass a few parameters to the file_get_contents() function

$arrContextOptions=array(
                "ssl"=>array(
                    "verify_peer"=>false,
                    "verify_peer_name"=>false,
                ),
);
$profile_picture = @file_get_contents($profile_picture, false, stream_context_create($arrContextOptions));
// Use @ to silent the error if user doesn't have any profile picture uploaded

Now the $profile_picture has the picture, we can save it anywhere in the following way.

$path = 'path/to/img';  //E.g assets/images/mypic.jpg

file_put_contents($path, $profile_picture); 

That's all :-)

Upvotes: 1

Related Questions