【问题标题】:How To Load And Save Facebook Profile Picture Of User如何加载和保存用户的 Facebook 个人资料图片
【发布时间】:2012-09-14 14:44:36
【问题描述】:

我想要用户头像的直接链接

我有以下代码

    $img = file_get_contents("https://graph.facebook.com/" . $fid . "/picture?type=large");
    $file = dirname(__file__). "/" . $fid . ".jpg";
    file_put_contents($file, $img);

但是https://graph.facebook.com/" . $fid . "/picture?type=large 有一个重定向。我如何遵循重定向?有什么方法可以通过 file_get_contents 做到这一点?我知道我可以通过curl 做到这一点,但似乎很复杂,我收到safe_mode 开启的错误,我不知道如何关闭它。

谢谢你

【问题讨论】:

标签: php facebook facebook-graph-api


【解决方案1】:

您应该能够通过 file_get_contents 跟踪重定向,方法是为其提供第三个参数 $context - 在其中将 HTTP context option follow_location 设置为 1。

(虽然这应该已经是默认设置了,并且在我的测试中获取图像数据已经单独使用 file_get_contents。)

【讨论】:

    【解决方案2】:

    她是我正在使用的代码,它非常适合我。它还将图片保存到我的服务器,这样我就有了一个本地 URL(然后可以将其发布回同一用户的个人资料或另一个用户/页面/事件/等的墙上)您所要做的就是放置它在你的代码中 $user 有一个值,它应该可以正常工作。

    <?
    $uid = $user;
    
    
    function GetTheImage($linky) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 0);
        curl_setopt($ch, CURLOPT_URL, $linky);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        # ADDED LINE:
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }
    
    
    
    $userpicture = "http://graph.facebook.com/$uid/picture?type=large";
    
    $sourceurl = GetTheImage($userpicture);
    
    $save = fopen("/home/arose/public_html/mydomain.com/tmp/$uid-large.jpg", "w"); //this is name of new file that i save
    fwrite($save, $sourceurl);
    fclose($save);
    
    ?>
    
    <html>
    <head>
    
    
    </head>
    <body>
    
    <img src="./tmp/<? echo $uid; ?>-large.jpg" />
    
    </body>
    </html>
    

    【讨论】:

    • 在使用 $curl Follow 位置警告时总是收到此警告:curl_setopt() [function.curl-setopt]:启用安全模式或设置 open_basedir 时无法激活 CURLOPT_FOLLOWLOCATION
    • 在此处查看问题的答案:stackoverflow.com/questions/6918623/…
    【解决方案3】:
    <?php
    $fbid=$user_profile['id'];
     $pic = file_get_contents("https://graph.facebook.com/$fbid/picture?type=large");
     $filename = $fbid.".jpg";
    $path="images/".$filename;
     file_put_contents($path, $pic);
    ?>
    

    【讨论】:

      【解决方案4】:
              curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
      
      function curl_redir_exec($ch)
          {
              static $curl_loops = 0;
              static $curl_max_loops = 20;
              if ($curl_loops++ >= $curl_max_loops)
              {
                  $curl_loops = 0;
                  return FALSE;
              }
              curl_setopt($ch, CURLOPT_HEADER, true);
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
              $data = curl_exec($ch);
              @list($header, $data) = @explode("\n\n", $data, 2);
              $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
              if ($http_code == 301 || $http_code == 302)
              {
                  $matches = array();
                  preg_match('/Location:(.*?)\n/', $header, $matches);
                  $url = @parse_url(trim(array_pop($matches)));
                  if (!$url)
                  {
                      //couldn't process the url to redirect to
                      $curl_loops = 0;
                      return $data;
                  }
                  $last_url = parse_url(curl_getinfo($ch, CURLINFO_EFFECTIVE_URL));
                  if (!$url['scheme'])
                      $url['scheme'] = $last_url['scheme'];
                  if (!$url['host'])
                      $url['host'] = $last_url['host'];
                  if (!$url['path'])
                      $url['path'] = $last_url['path'];
                  $new_url = $url['scheme'] . '://' . $url['host'] . $url['path'] . (@$url['query']?'?'.$url['query']:'');
                  return $new_url;
              } else {
                  $curl_loops=0;
                  return $data;
              }
          }
      
          function get_right_url($url) {
              $curl = curl_init($url);
              curl_setopt($curl, CURLOPT_HEADER, false);
              curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
              return curl_redir_exec($curl);
          }
      
              $url = 'http://graph.facebook.com/' . $fbid . '/picture?type=large';
      
              $file_handler = fopen('img/avatar/'.$fbid.'.jpg', 'w');
              $curl = curl_init(get_right_url($url));
              curl_setopt($curl, CURLOPT_FILE, $file_handler);
              curl_setopt($curl, CURLOPT_HEADER, false);
              curl_exec($curl);
      
              curl_close($curl);
              fclose($file_handler);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多