如何保存保存用户';s的facebook个人资料图片到我的域/网站数据使用php


How to save save user's facebook profile picture to my domain/website data using php

我想把用户的facebook个人资料图片保存到我的磁盘上,这更像是在用户第一次注册时删除他们的个人资料图片数据库。'

例如,这里是url。

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

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

我是php的初学者,请详细解释一下。

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