2

场景:我正在开发一个应用程序,我需要通过该应用程序从 Facebook 下载用户的个人资料图片,应用特定的过滤器,然后重新上传并将其设置为个人资料图片,这可以使用这个技巧。' makeprofile=1 '

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 

我也尝试了其他一些方法,但无法解决这个问题。任何帮助,将不胜感激 :)

注意:我现在通过 Codeigniter 和 localhost 执行此操作。

4

3 回答 3

2

所以我自己找到了解决方案并决定回答,以便如果这可以帮助其他面临同样问题的人。

我们需要向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); 

就这样 :-)

于 2016-03-09T13:17:36.650 回答
2

你的问题是这个问题的潜在重复。

我将在这里重复elitechief21 的回答:您不应该禁用 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));
于 2016-03-27T01:31:16.117 回答
0

您可以使用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'];

其中$profileId变量包含用户配置文件 ID 和 类型参数可以是 square , large , small , normal 根据您的要求您想要的大小

现在$img变量包含您的图像数据。使用将图像文件保存在服务器上file_put_contents()

$imagePath = 'path/imgfolder';  //E.g assets/images/mypic.jpg
file_put_contents($imagePath, $img); 
于 2016-03-09T13:59:51.670 回答