【问题标题】:Saving facebook profile picture takes too long保存 facebook 头像需要太长时间
【发布时间】:2013-12-21 06:01:59
【问题描述】:

我有一个简单的圣诞节图像编辑器网站,我需要保存用户的 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;
        }
    }


  ?>

我确定我做错了什么,上传一张像这样的小图片应该不会那么痛苦。如果有任何帮助,我将不胜感激,非常感谢。

【问题讨论】:

  • 您正在尝试将用户的个人资料图片下载到您的服务器???
  • 是的,所以我以后可以修改它。

标签: javascript php facebook facebook-graph-api


【解决方案1】:

已修复。使用 facebook 的 API 获得图像的“真实链接”

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

curl 耽误了很多时间......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    • 2020-05-02
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    相关资源
    最近更新 更多