2012-03-17 193 views
3

我的域名/网站的数据,我想给Facebook用户的个人资料图片保存到我的硬盘,它更像是报废用户的个人资料图片分贝时,他们注册的第一位。”如何保存用户的Facebook个人资料图片保存到使用PHP

例如这里的URL。

https://graph.facebook.com/ {ID} /图片

我想将其保存在特定目录下。 也,如果没有图片,我想要下载默认的占位符,也就是GIF。上面的url实际上只有一个占位符。

我是初学者在PHP,请解释一下我详细。

+0

我在Facebook上的版权政策方面的专家,但如果这是T&Cs的允许范围内(你和/或是否还需要询问用户),它可能是值得一试。 – ChrisW 2012-03-17 17:01:03

+0

看起来Facebook允许您通过公开可用的信息(如user_profile中的信息)来做你想做的事情:另外,当您下载或使用此类第三方服务时,他们可以访问您的公开个人资料,其中包括您的用户名或用户ID ,你的年龄范围和国家/语言,你的朋友列表,以及你与他们分享的任何信息。这些应用程序,网站或综合服务收集的信息受其自身条款和政策的约束。 https://www.facebook.com/policy.php中的'如何分享这些信息?'一节。 – Simon 2015-05-03 19:28:40

回答

8
<?php 
$image = file_get_contents('https://graph.facebook.com/100003027438870/picture'); // sets $image to the contents of the url 
file_put_contents('/path/image.gif', $image); // places the contents in the file /path/image.gif 
?> 
+0

非常感谢,我从来没有想过这是容易的。我试图根据各种论坛的建议来使用卷曲,但实际上从未奏效,谢谢。我相信,一旦用户授予权限,加载Facebook个人资料图片是合法的 – asm234 2012-03-18 19:06:13

+0

@noka请让我的答案接受:) – Tyilo 2012-03-18 19:16:24

+0

@Tylio,我做到了。谢谢。我不知道社区的标准,因为是新来这个:) – asm234 2012-03-19 09:21:13

0
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); 
相关问题