How to base64 encode an image from the facebook api

后端 未结 4 1586
北恋
北恋 2021-01-23 06:21

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

the api provides a url as such:

https://fbcdn-sphotos-         


        
4条回答
  •  清酒与你
    2021-01-23 07:01

    You just need to add the CURLOPT_SSL_VERIFYPEER set to false as the url from facebook is https and not http., or you could just as well request the url without ssl by replacing https with http.

    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 = 'Embedded Image';
           echo $base64;
        }
    } 
    catch ( \ErrorException $e ) {
        echo 'fail';
    }
    

提交回复
热议问题