简体   繁体   English

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

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

i want to save the user's profile picture of facebook to my disk, it is more like scrapping user's profile picture db when they registered first.' 我想将用户的个人资料图片保存到我的磁盘上,这更像是在用户首次注册时删除用户的个人资料图片。

for example here is the url. 例如,这里是网址。

https://graph.facebook.com/ {id}/picture https://graph.facebook.com/ {id} / picture

i want to save it under a specific directory. 我想将它保存在特定目录下。 also, if there is not picture, i want to download the default placeholder as well, which is GIF. 另外,如果没有图片,我也想下载默认占位符,即GIF。 the above url is actually having a placeholder only. 上面的url实际上只有一个占位符。

i am beginner in php, please explain me in detail. 我是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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用PHP在Facebook中保存用户的个人资料照片 - how to save user's profile pic in facebook using php 如何加载和保存用户的Facebook个人资料图片 - How To Load And Save Facebook Profile Picture Of User 如何使用PHP图表Api保存Facebook个人资料图片 - how to save facebook profile picture using php graph Api 如何返回存储在我的文件中的用户个人资料图片(保存在/ tmp文件夹中以将其放置在此处以进行临时提取) - How to return User's profile picture that is stored in my filestyem (is /tmp folder save to put it there for temporary extraction) 如何在 php 中获取 Facebook 用户的个人资料图片 - How to get a Facebook user's profile picture in php 我应该怎么获得的Facebook用户的好友列表,并在PHP他们的个人资料图片的使用图形API? - How should i get Facebook user friend list and their profile picture's using Graph API in PHP? 如何在Facebook上获取用户的个人资料图片 - How to get user's profile picture on Facebook 如何使用php保存图片? - how to save picture using php? 如何通过facebook社交插件获取已注册我网站的用户的个人资料图片 - How can i get the profile picture of a user who have registered my website via the facebook social plugin 如何使用facebook php SDK访问facebook个人资料图片? - How to access facebook profile picture using facebook php SDK?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM