保存Facebook个人资料图片需要太长时间

时间:2013-12-21 06:01:59

标签: javascript php facebook facebook-graph-api

我有一个简单的圣诞图像编辑器网站,我需要保存用户的Facebook个人资料图片才能完成。它工作,但需要太长时间(15-30秒之间),我不知道为什么。

我使用javascript来处理登录内容,之后,我使用用户的ID来获取个人资料图片。我相信问题发生在那之后:

由于此url不是图像的真实路径,因此我必须先重定向它,然后保存它。这是我的PHP代码:

<?php


//just getting the file from the URL
$file = explode('/',$_GET["var1"]);
//if it's from facebook, not uploaded
    if( $file[1] != "uploads"){
    $saveto = "uploads/".$file[3].".jpg";


    $ch = curl_init(get_right_url("http:".$_GET["var1"]));
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
    $raw=curl_exec($ch);
    curl_close ($ch);
    if(file_exists($saveto)){
        unlink($saveto);
    }
    $fp = fopen($saveto,'x');
    fwrite($fp, $raw);

    fclose($fp);
    }



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



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


  ?>

我确定我做错了,上传像这样的小图片应该不会感到痛苦。非常感谢任何帮助,非常感谢。

1 个答案:

答案 0 :(得分:0)

固定。通过facebook的API获得图像的“真实链接”

https://developers.facebook.com/docs/reference/api/using-pictures/

卷毛推迟了很多......