简体   繁体   English

从Facebook API保存用户的个人资料图片 - PHP SDK V.5

[英]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. 场景:我正在开发一个应用程序,通过该应用程序,我需要从Facebook下载用户的个人资料图片,应用特定的过滤器,然后重新上传并将其设置为个人资料图片,这可以使用此技巧。 ' makeprofile=1 ' ' 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. 问题:所以我遇到的问题是通过API从收到的URL下载图片。 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. 现在我想将获取的URL中的图像保存到我服务器上的目录中,以便我可以应用过滤器并重新上传它。 When i use file_get_contents($pic) it throws following error 当我使用file_get_contents($ pic)时,它会抛出以下错误

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. 注意:我现在通过Codeigniter和localhost执行此操作。

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. 我只想在这里重复elitechief21的答案 :你不应该禁用SSL证书,因为这会在你的应用程序中造成安全漏洞。

But rather, you should download a list of trusted certificate authorities (CA) (eg curl.pem ) and use it together with your file_get_contents, like this: 但是,您应该下载可信证书颁发机构(CA)列表(例如curl.pem )并将其与file_get_contents一起使用,如下所示:

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

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 我们需要将一些参数传递给file_get_contents()函数

$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. 现在$ profile_picture有图片,我们可以通过以下方式将其保存在任何地方。

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

file_put_contents($path, $profile_picture); 

That's all :-) 就这样 :-)

you can use file_get_connects() 你可以使用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 其中$ profileId变量包含用户配置文件ID和类型参数可以是方形,大,小,正常根据您的要求您想要的大小

Now $img variable contain your image data. 现在$img变量包含您的图像数据。 Save image file on server using file_put_contents() 使用file_put_contents()将图像文件保存在服务器上

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM