使用cURL保存facebook个人资料图片

时间:2012-03-13 08:40:02

标签: php facebook curl

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

<?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);

&GT;

3 个答案:

答案 0 :(得分:7)

有重定向,因此您必须为curl

添加此选项
// 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);

答案 1 :(得分:2)

如果您无法处理重定向,请尝试改为:

https://graph.facebook.com/<USER ID>?fields=picture发出请求并解析响应,该响应将采用JSON格式,如下所示 - 例如对于Zuck,您会收到此回复:

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

然后直接发出curl请求以从该云存储URL中检索图像

答案 2 :(得分:1)

我设法这样做,完美无缺:

$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中添加“redirect = false”,以获取带有URL的JSON对象(例如:https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpa1..。)

CURLOPT_FOLLOWLOCATION已在PHP5.4中删除,因此它不再是一个选项。