简体   繁体   English

如何对来自Facebook API的图像进行base64编码

[英]How to base64 encode an image from the facebook api

I am attempting to convert an image url provided by the facebook api into base64 format with cURL. 我正在尝试使用cURL将facebook api提供的图像URL转换为base64格式。

the api provides a url as such: api提供了这样的网址:

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

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. 问题在于,该URL仅与URL字符串中包含的oh,oe和gda参数一起使用,没有直接的img url。 Removing the params send you to a facebook error page. 删除参数会将您带到Facebook错误页面。

With the parameterized url my curl_exec is not getting correct image data. 使用参数化的url,我的curl_exec无法获取正确的图像数据。 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? 有没有一种方法可以从facebook获取base64数据,或者有什么我可以做的以给定参数化URL来访问纯图像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';
}

To address your specific problem, your script is likely failing because the required oh , oe , __gda__ parameters are getting separated during the GET request and therefore are not included in $_GET['url'] . 为了解决您的特定问题,您的脚本可能会失败,因为所需的ohoe__gda__参数在GET请求期间被分隔开,因此未包含在$_GET['url']

Make sure you're using a URL-encoded string so any unencoded & characters aren't handled as delimiters. 确保您使用的是URL编码的字符串,这样任何未编码的&字符都不会被当作分隔符。 Then just decode the string before passing it on to cURL. 然后只需解码字符串,然后再将其传递给cURL。

...

$url = urldecode($_GET['url']);

...

For anyone curious, you can still load any Facebook image from any one of their legacy CDNs without needing the new parameters: 对于任何好奇的人,您仍然可以从其任何旧版CDN加载任何Facebook图像,而无需使用新参数:

  1. https://scontent-a-iad.xx.fbcdn.net/hphotos-frc3/
  2. https://scontent-b-iad.xx.fbcdn.net/hphotos-frc3/
  3. https://scontent-c-iad.xx.fbcdn.net/hphotos-frc3/

Just append the original image filename to the URL et voila . 只需将原始图像文件名附加到URL 等即可

Disclaimer: I have no idea how long this little trick will work for so don't use it on anything important in production. 免责声明:我不知道这个小技巧将持续多久,所以不要在生产中的任何重要事情上使用它。

Maybe this won't help much but it seems that the original picture (ending with _o ) does not need gda nor oe oh parameters 也许这无济于事,但原始图片(以_o结尾)似乎不需要gdaoe oh参数

to get the original profile picture you can do: 要获取原始的个人资料图片,您可以执行以下操作:

var username_or_id = "name.lastname" //Example
get_url ("http://graph.facebook.com/$username_or_id/picture?width=9999")

hth hth

I had similar problem. 我有类似的问题。 My solution: 我的解决方案:

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

Where the URL is to Graph API: URL指向Graph API:

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

(You probably want to also check, if file_get_contents returns something) (您可能还想检查一下,如果file_get_contents返回某些内容)

You just need to add the CURLOPT_SSL_VERIFYPEER set to false as the url from facebook is https and not http. 您只需要添加设置为falseCURLOPT_SSL_VERIFYPEER ,因为来自Facebook的网址是https而不是http。 , or you could just as well request the url without ssl by replacing https with http. ,或者您也可以通过将https替换为http来请求不带ssl的网址。

Try the code below 试试下面的代码

    $url = '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';

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

    /***********************************************/
    // you need the curl ssl_opt_verifypeer
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
    /***********************************************/

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM