简体   繁体   English

如何加载和保存用户的Facebook个人资料图片

[英]How To Load And Save Facebook Profile Picture Of User

I Want direct link of user's profile picture 我想要用户个人资料图片的直接链接

i Have Following Code 我有以下代码

    $img = file_get_contents("https://graph.facebook.com/" . $fid . "/picture?type=large");
    $file = dirname(__file__). "/" . $fid . ".jpg";
    file_put_contents($file, $img);

But https://graph.facebook.com/" . $fid . "/picture?type=large Have a Redirection. 但是https://graph.facebook.com/" . $fid . "/picture?type=large具有重定向。 How do i follow the redirection? 我如何遵循重定向? is there any way to do it via file_get_contents? 有什么办法可以通过file_get_contents做到这一点? I know i can do it via curl But It Seems complicated i got error that safe_mode is on and i don't know how to off it. 我知道我可以通过curl做到这一点,但似乎很复杂,我遇到了一个错误,那就是safe_mode已打开,我不知道如何关闭它。

Thanks You 谢谢

You should be able follow redirects with file_get_contents by giving it a third parameter $context – in which you set the HTTP context option follow_location to 1. 通过为它提供第三个参数$context您应该能够使用file_get_contents进行重定向。在该参数中,您将HTTP上下文选项 follow_location为1。

(Although this should already be the default, and in my test getting the image data worked with file_get_contents alone already.) (尽管这应该已经是默认设置,但在我的测试中,仅使用file_get_contents即可获取图像数据。)

Her is the code that I am using and it works perfect for me. 她是我正在使用的代码,它对我来说很完美。 It also saves the picture to my server so I then have a local url (which can then be posted either back to the same user's profile or to the wall of another user/page/event/etc.) All you have to do is place it in your code where $user has a value and it should work just fine. 它还将图片保存到我的服务器,因此我有了一个本地URL(然后可以将其发布回同一用户的个人资料或其他用户/页面/事件/等的墙上。)它在您的代码中$ user有值,它应该可以正常工作。

<?
$uid = $user;


function GetTheImage($linky) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 0);
    curl_setopt($ch, CURLOPT_URL, $linky);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    # ADDED LINE:
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}



$userpicture = "http://graph.facebook.com/$uid/picture?type=large";

$sourceurl = GetTheImage($userpicture);

$save = fopen("/home/arose/public_html/mydomain.com/tmp/$uid-large.jpg", "w"); //this is name of new file that i save
fwrite($save, $sourceurl);
fclose($save);

?>

<html>
<head>


</head>
<body>

<img src="./tmp/<? echo $uid; ?>-large.jpg" />

</body>
</html>
<?php
$fbid=$user_profile['id'];
 $pic = file_get_contents("https://graph.facebook.com/$fbid/picture?type=large");
 $filename = $fbid.".jpg";
$path="images/".$filename;
 file_put_contents($path, $pic);
?>
        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 save user's facebook profile picture to my domain/website data using php 如何在Facebook上获取用户的个人资料图片 - How to get user's profile picture on Facebook 如何使用PHP图表Api保存Facebook个人资料图片 - how to save facebook profile picture using php graph Api 如何在 php 中获取 Facebook 用户的个人资料图片 - How to get a Facebook user's profile picture in php 如何使用PHP访问Facebook用户的个人资料图片? - How do I access the profile picture of a facebook user in PHP? 如何从Facebook获取用户的个人资料图片? - How to fetch a user's profile Picture from Facebook? 如何将Facebook个人资料图片放入图片的坐标中? - How to put facebook profile picture into a coordinates of a picture? 如何从Facebook或instagram中提取某个用户的个人资料图片并将其上传到我们网站上该用户的个人资料页面? - How to extract the profile picture of some user from facebook or instagram and upload it to profile page of that user at our website?? 获取facebook用户的个人资料图片 - Get facebook user's profile picture 获取不是朋友的用户的facebook个人资料照片 - Get facebook profile picture of user that is not a friend
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM