2012-03-13 194 views
0

我试图用CURL在Facebook上保存用户个人资料图片。当我使用下面的代码时,我保存了一个jpeg图像,但它有零个字节。但如果我交换的url值为https://fbcdn-profile-a.akamaihd.net/hprofile-ak-snc4/211398_812269356_2295463_n.jpg,这是http://graph.facebook.com/'。 $ user_id。 '/ picture?type = large重定向浏览器,图像保存没有问题。我在这里做错了什么?使用cURL保存facebook个人资料图片

<?php 

$url = 'http://graph.facebook.com/' . $user_id . '/picture?type=large'; 


$file_handler = fopen('pic_facebook.jpg', 'w'); 
$curl = curl_init($url); 
curl_setopt($curl, CURLOPT_FILE, $file_handler); 
curl_setopt($curl, CURLOPT_HEADER, false); 

curl_exec($curl); 

curl_close($curl); 
fclose($file_handler); 

?>

+0

这可能有所帮助:http://stackoverflow.com/questions/5429964/how-to-save-users-profile-pic-in-facebook-using-php – 2012-03-13 08:47:51

回答

7

有一个重定向,所以你必须增加该选项用于卷曲

// safemode if off: 
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 

,但如果你有安全模式,如果在的话:

// safemode if on: 
<?php 
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/' . $user_id . '/picture?type=large'; 

    $file_handler = fopen('pic_facebook.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); 
+0

谢谢你的回答! Hovwewer,它似乎不适合我。这可能是我搞砸了。出现此错误:警告:curl_setopt()[function.curl-setopt]:当启用safe_mode或在/home/web81206/domains/utvecklingtesttco.se/public_html/fb/index.php中设置了open_basedir时,无法激活CURLOPT_FOLLOWLOCATION在线83 有什么建议吗? – user1009453 2012-03-13 09:04:13

+0

darn。尝试这个功能,而不是:http://au.php.net/manual/ro/function.curl-setopt.php#71313 – deadrunk 2012-03-13 09:15:48

+0

不,不能,这不工作,这是一个很简单的事情,我想要做的,但我似乎没有找到任何有效的代码。多么奇怪的事情!无论如何感谢您的帮助! – user1009453 2012-03-13 09:44:43

1

设置

CURLOPT_FOLLOWLOCATION

,使其遵循301/302重定向读取最终位置的图像文件。 即

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
+0

谢谢你的回答! Hovwewer,它似乎不适合我。这可能是我搞砸了。出现此错误:警告:curl_setopt()[function.curl-setopt]:当启用safe_mode或在/home/web81206/domains/utvecklingtesttco.se/public_html/fb/index.php中设置了open_basedir时,无法激活CURLOPT_FOLLOWLOCATION在线83 – user1009453 2012-03-13 09:00:22

+0

尝试使用'file_get_contents'来代替,这将自动执行此部分。 – DhruvPathak 2012-03-13 10:12:24

2

如果可以的话't处理重定向,请尝试这个:

请求https://graph.facebook.com/<USER ID>?fields=picture a nd解析响应,它将采用JSON格式,看起来像这样 - 例如为Zuck你会得到这样的响应:

{ 
    "picture": "http://profile.ak.fbcdn.net/hprofile-ak-snc4/157340_4_3955636_q.jpg" 
} 

然后让你的卷曲的请求直接从云存储URL

+0

好的想法,我也会尝试这个解决方案,即使我通过@deadrunk给了另一个很好的解决方案 – user1009453 2012-03-13 11:51:10

0

我设法做这种方式获取图像,工作完全正常:

$data = file_get_contents('https://graph.facebook.com/[App-Scoped-ID]/picture?width=378&height=378&access_token=[Access-Token]'); 
$file = fopen('fbphoto.jpg', 'w+'); 
fputs($file, $data); 
fclose($file); 

您只需要一个应用程序访问令牌(APPID。'|'。APPSECRET),并且您可以指定宽度和高度。

您还可以添加“重定向=假”的URL,获取一个JSON对象与URL(例如:https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpa1..

CURLOPT_FOLLOWLOCATION已在PHP5.4被删除,所以it's不真的是一个选择了。

相关问题