【问题标题】:How do I access the profile picture of a facebook user in PHP?如何在 PHP 中访问 facebook 用户的个人资料图片?
【发布时间】:2011-12-03 14:56:12
【问题描述】:

我正在开发一个将使用用户个人资料图片的应用。我正在尝试将用户的个人资料图片保存在我的服务器上以进行操作。

当我使用 https://graph.facebook.com/[uid]/picture?type=large 时,它不起作用,因为 Facebook 在提供图片之前会进行某种重定向

谁能帮我解决这个问题?我正在使用 PHP。

【问题讨论】:

  • 你用什么下载图片?尝试修改它以遵循重定向。
  • 显示用于调用该 URL 的 PHP。你在使用 facebook API 框架吗?

标签: php facebook facebook-graph-api profile


【解决方案1】:

如果我正确理解您的问题,您可能正在使用 file_get_contents 来获取图像,但您只是收到了一个文本响应,因为 file_get_contents 不会自动遵循文本响应包含的重定向?

一个快速而肮脏的解决方案是自己解析文本响应以找到实际图像的 url:

@file_get_contents('https://graph.facebook.com/'.$userid.'/picture?type=large');
foreach ($http_response_header as $rh) if (substr($rh, 0, 10)=='Location: ') $imgurl=substr($rh, 10);

那么你应该有可以根据需要处理的实际图像 url。

【讨论】:

    【解决方案2】:

    您可以使用graph API to retrieve the direct URL 作为用户的头像。
    https://graph.facebook.com/{user_id}/picture

    【讨论】:

      【解决方案3】:
      <?php
      # You can do:
      
      $userid = SOMEID;
      
      $piclink = 'https://graph.facebook.com/'.$userid.'/picture?type=';
      
      #then a option to choose the type, 
      #there is the small, normal, square and large type.
      #So lets say you choose the large size:
      
      $pictype = 'large';
      
      $userpicture = $piclink . pictype;
      
      # ----------------------------------
      # Now that we have a picture link you can do various stuff like:
      
      echo '<img src="';
      echo $userpicture;    # This will output the updated user picture with no problem
      echo '" />';          
      
      ?>
      

      现在,如果您想要将图像保存到数据库中,请查看此

      http://www.phpriot.com/articles/images-in-mysql

      然后在存储图像时使用 $userpicture 来获取它。

      【讨论】:

        猜你喜欢
        • 2020-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多