如何使用PHP将用户的Facebook个人资料图片保存到我的域名/网站数据?

4
我想将Facebook用户的个人资料图片保存到我的磁盘上,更像是在用户注册时抓取用户个人资料图片数据库。
例如,这里是URL链接: https://graph.facebook.com/{id}/picture 我想将其保存到一个特定的目录下。另外,如果没有图片,我也想下载默认的占位符,这是一个GIF文件。上述URL实际上只有一个占位符。
我是PHP初学者,请详细解释一下。

我不是Facebook版权政策的专家,但检查一下是否在条款和条件内允许这样做(以及是否还需要询问用户)可能是值得的。 - ChrisW
看起来Facebook允许您使用公开可用信息(例如用户资料中的信息)进行所需操作。此外,当您下载或使用这些第三方服务时,它们可以访问您的公共个人资料,其中包括您的用户名或用户ID、年龄范围和国家/语言、朋友列表以及您与他们分享的任何信息。这些应用程序、网站或集成服务收集的信息受其自己的条款和政策约束。请参阅https://www.facebook.com/policy.php中“如何共享此信息?”一节。 - Simon
2个回答

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

非常感谢,我从来没有想到它如此简单。我试图根据各种论坛的建议使用curl,但实际上从未成功,谢谢。我相信在用户授权后加载Facebook个人资料图片是合法的。 - asm234
@Tylio,我做到了。谢谢。因为我是新手,所以我不知道社区标准 :) - asm234
@Tylio 直到现在它一直运行良好,但是最近开始出现故障并显示无法打开流... - asm234
@asm234 你需要确保图像的路径存在。例如,如果你要保存到 /pathname/image.gif,请确保 /pathname 存在。 - Tyilo
@Tylio 是的,路径存在,就像我之前说的一样,它曾经运行得很好...路径和一切都还是一样的...但突然开始失败了...并且说无法打开流,它在 $image = file_get_contents('https://graph.facebook.com/id/picture'); 失败。 - asm234
@Tylio,问题已经解决,之前的解决方案仍然适用...它突然间开始出现问题...我猜测可能是某个Facebook API的问题...不过我添加了异常捕获来避免任何问题,谢谢! - asm234

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接