【问题标题】:Saving User's Profile Picture from facebook API - PHP SDK V.5从 facebook API 保存用户的个人资料图片 - PHP SDK V.5
【发布时间】:2016-06-21 23:12:19
【问题描述】:

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

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

问题: 所以我面临的问题是通过 API 从接收到的 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 执行此操作。

【问题讨论】:

  • 你能把图片成功上传回Facebook吗?

标签: php facebook-php-sdk facebook-sdk-4.0 save-image


【解决方案1】:

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

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

就是这样:-)

【讨论】:

    【解决方案2】:

    您的问题可能与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));
    

    【讨论】:

      【解决方案3】:

      你可以使用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 和 type 参数可以是 square , large , small , normal 根据你想要的尺寸而定

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

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

      【讨论】:

      • Pankaj,我在使用 file_put_contents() 函数时遇到错误,因此我的答案中提到的参数对我有用。也感谢您的回答:-)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多