保存用户的个人资料图片从Facebook的API - PHP SDK V.5
场景:我正在通过一个应用程序,我需要从Facebook下载用户的个人资料图片,应用特定的过滤器,然后重新上传和设置它作为个人资料图片,这是可能使用这个技巧。 'makeprofile = 1'保存用户的个人资料图片从Facebook的API - PHP SDK V.5
http://www.facebook.com/photo.php?pid=xyz&id=abc&makeprofile=1
问题: 所以我面临的问题是,同时通过API下载从接收到的URL的图像。我获得的图片URL是这样的:
$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'];
现在我想从获得的URL将图像保存到一个目录我的服务器上,这样我可以应用过滤器,并重新上传。 当我使用的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
我已经尝试了一些其他的方法很好,但不能此问题得到解决。任何帮助将不胜感激:)
NOTE:我通过Codeigniter和本地主机现在这样做。
所以我自己找到了解决方案,并决定回答,以便如果这可以帮助其他人面对同样的问题。
我们需要一些参数传递给的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
现在$ profile_picture有图片,我们可以在任何地方通过以下方式保存。
$path = 'path/to/img'; //E.g assets/images/mypic.jpg
file_put_contents($path, $profile_picture);
这一切:-)
可以使用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'];
其中$简档变量包含用户个人资料ID和 型paramete可以是方形,大,小,正常根据你的要求你想要的尺寸
现在$img
变量包含您的图像数据。使用file_put_contents()
$imagePath = 'path/imgfolder'; //E.g assets/images/mypic.jpg
file_put_contents($imagePath, $img);
Pankaj,我在使用file_put_contents()函数时出错,所以我的答案中提到的参数对我有用。谢谢你的回答,以及:-) –
你的问题节省服务器映像文件是this question一个潜在的重复。
我只是在这里重复elitechief21's answer:您不应该禁用SSL证书,因为这会在您的应用程序中创建一个安全漏洞。
但相反,你应该下载受信任的证书颁发机构(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));
你可以上传图片成功回到Facebook的? – Kyslik