如何对来自Facebook API的图像进行base64编码 [英] How to base64 encode an image from the facebook api

查看:93
本文介绍了如何对来自Facebook API的图像进行base64编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用cURL将facebook api提供的图像URL转换为base64格式。

I am attempting to convert an image url provided by the facebook api into base64 format with cURL.

该api提供了这样的URL:

the api provides a url as such:

https://fbcdn-sphotos-g-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-9/p180x540/72099_736078480783_68792122_n.jpg?oh=f3698c5eed12c1f2503b147d221f39d1&oe=54C5BA4E&__gda__=1418090980_c7af12de6b0dd8abe752f801c1d61e0d



<可与网址字符串中包含的oh,oe和 gda 参数一起使用,没有直接的img网址。删除参数会将您发送到Facebook错误页面。

The issue is that the url only works with the oh, oe and gda parameters included in the url string, there is no direct img url. Removing the params send you to a facebook error page.

使用参数化的URL,我的curl_exec无法获取正确的图像数据。有没有办法从facebook上获取base64数据,或者有什么我可以做的以给定参数化url来访问纯图像url?

With the parameterized url my curl_exec is not getting correct image data. Is there a way to get the base64 data from facebook, or is there something I can do to get access the pure image url given the parameterized url?

这就是我的解码脚本看起来像这样:

This is what my decode scrip looks like:

header('Access-Control-Allow-Origin: *');

  $url = $_GET['url'];

  try {
        $c = curl_init($url);
        curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 3);

        $result = curl_exec($c);
        curl_close ($c);

        if(false===$result) {
           echo 'fail';
        } else {
           $base64 = "data:image/jpeg;charset=UTF-8;base64,".base64_encode($result);
           echo $base64;
}
} catch ( \ErrorException $e ) {
    echo 'fail';
}


推荐答案

我遇到了类似的问题。我的解决方案:

I had similar problem. My solution:

$url = urldecode($url);
return base64_encode(file_get_contents($url));

URL是图形API:

Where the URL is to Graph API:

https://graph.facebook.com/$user_id/picture?width=160

(您可能还想检查一下,如果file_get_contents返回某些内容)

(You probably want to also check, if file_get_contents returns something)

这篇关于如何对来自Facebook API的图像进行base64编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆