2012-03-17 172 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); 
相關問題