Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何保存用户';使用php将facebook个人资料图片添加到我的域/网站数据_Php_Facebook_Curl_Profile_Image - Fatal编程技术网

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

如何保存用户';使用php将facebook个人资料图片添加到我的域/网站数据,php,facebook,curl,profile,image,Php,Facebook,Curl,Profile,Image,我想将用户的facebook个人资料图片保存到我的磁盘上,这更像是在用户首次注册时将其个人资料图片数据库销毁 例如,这里是url {id}/图片 我想把它保存在一个特定的目录下。 另外,如果没有图片,我还想下载默认的占位符,即GIF。上面的url实际上只有一个占位符 我是php初学者,请详细解释。 <?php $image = file_get_contents('https://graph.facebook.com/100003027438870/picture'); // sets $

我想将用户的facebook个人资料图片保存到我的磁盘上,这更像是在用户首次注册时将其个人资料图片数据库销毁

例如,这里是url

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

我不是Facebook版权政策方面的专家,但可能值得检查T&Cs是否允许这样做(和/或您是否也需要询问用户)。Facebook似乎允许您使用公开信息(如用户档案中的信息)做您想做的事。此外,当您下载或使用此类第三方服务时,他们可以访问您的公共档案,其中包括您的用户名或用户ID、您的年龄范围和国家/语言、您的朋友列表以及您与他们共享的任何信息。这些应用程序、网站或集成服务收集的信息受其自身条款和政策的约束。从“这些信息是如何共享的?”一节中,非常感谢,我从未想过这会如此简单。我试图根据各种论坛上的建议使用curl,但实际上从未奏效,谢谢。我相信一旦用户允许,加载facebook个人资料图像是合法的。@Tylio,我做到了。谢谢我不知道社区标准,因为我是新来的:)@Tylio它以前工作得很好,直到现在,但是它开始失败,并说无法打开流…@asm234您需要确保图像的路径存在。例如,如果要保存到
/pathname/image.gif
,请确保
/pathname
存在。@Tylio yes path存在,正如我前面所说,它过去工作正常。。道路和一切都是一样的。。。但却突然开始失败。。比如说不能打开流,它在$image=file_get_contents(')上失败;
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);