Avatar of Ashraf Hassanein
Ashraf Hassanein asked on

file_get_contents is returning back a corrupted characters from facbook feed.

I have the following PHP code which is working fine, but all of the sudden it does not work:
<?php
 header('Content-Type: text/html; charset=utf-8');
$page_id = 'cnninternational';
$access_token = 'xxxxxxx';
//Get the JSON
$json_object = file_get_contents('https://graph.facebook.com/'.$page_id.'/posts?access_token='.$access_token.'&format=json');
//Interpret data
$fbdata = json_decode($json_object);
?>

Open in new window


I started to trace and I found that when I var_dump($json_object) I am getting something like this:

       ‚è5×À4#_rß.²¹ÏÀû-«‹hÎ
M´‹”VÎuK6W•Ð
po6¥üv¢p+¡¿S …QáîÀ&âcÛ6:­„6[¡LÖKØãv¡[f-WFNÅ1Y2+¡]t[0°§5„>Nñ¿À;]¤zŽ‰®›T\¿Ù:K¿Å:ÐçÚd»¸®Þ:sŠ‘Œ‘Ì- eP“tá¿åì" õÖj6³Óf¿¿/¿”ÌÇU¿ÉDGC§¦ ûµyÚE÷­ÑÍ6½.òpß
ÊøõNmY—Vívëþ\ÍC¹ƒpäSl
ºœ¯¶*¿\;&çûíÁ¤¿Ç4É´‚Ð×÷¿·ßœ”Á0œ¿Î¿^aé…6¾¶èU¯ ¿T.¾ªŸò-Ïjd·øÏ*»å`  ¿Wèt„¿²­ÝUåë÷S©H‡[Ì4hü•:³¿¦JK:eM]’®2;é É
#”^® &@¿W0½¬'ØÇ3¿y·MicŠé·"[£%~)—I±ˆ¸¡¼8‹\ò5Fu Ý
ÝP‰ÀLö|Zë¿ å5¡áòVI`œc@ ‘L¬";§tÍag?¶¨z‡p˜½˜‘o«°²W [ ’É_Á·‘Û@‹±S%®–o‚Ï?úÆx¥j¿‡â•i=ç­tZ™n›·rCe)·“k¬K‘À¥¡Ó¿¨+ÆwIÕœ|·.Ç5
"_m¿aC¶C2* «—4ð+áÈÃÖmÛÔz1 ‘ñ¤ù’-€±\ÅG€¨f¨¼êAN§ñ¢ÛYî=c,ú$w¿ú[ø å€æà‚ȪXÏj£©{¤%¢
ÀUØ]hç‰Ë0Ò¿B\19P„1{)º%'@_¸ñ¿Ïo­=:®ìgx »Ì
                                                                                                                                                                                
¿¹ '¾z
ýÃøwƒ ¿
œ6U²ó æüƒT¡ï…8 ¿ÜÛ&ˆÛú_èâÃî0ƒ¨u}nÊóU{wß`—²ƒØÍñWÛmÛùÉP %"?’óI¿Á¼–f³:S¤8¾é–¶)¿@F¯¿€bÃDJK+JA›0ß™L
À®Ž   ÔT¢®1q¡aæ–@ù)¦>$Àñôa6E‚2¯.ÁbË>4þöS‰³ålûA:•´Ãîñÿ÷#“³%ãèeòPÂæ±Ó«¾å0dÊ
                                                                                                                                                                                
                                                                                                                                                                                
                                                                                                                                                                                
Ñ.€Î@G‚
} ðMWŒ#õx.Ò 3©¥\ Q
£¿ 0Âöú·8ÿðo¼¨»«hžÙj¬v^‹»mo¬ÚâX—n"ÿ™[,óC¼À
r5q:/T—«*AØx#U/„åP>|žqY¼ztŒbÈê_Ä¿øÜCIþù§ëãë­ç…ýÖ1ža%pÛæ¿_¿_ÜaוÿLàU2⯨ä—}I<‘Üôƒªlt¿GöÇ_‹¤q¿>ê}<½8~+Fë6¯aÁng†žÔC^1 ÌNÙYå):PI,®žä²¾ê
”ä ¿Ùs@É ¹\cQÿ1Jíu(zyC
í;ðj†
¼ZøÿQ3—¿.ø.HkÂzŸóÚ@sØ¿q¡wQw4å²Cæó©¶59ë*“i^G—xÁ†_çþœ~Cï¢Ýw™ãóÌs9kT~þÎ,½
ºÜo@uLË6]¿ë8¯ÚÑS
í8uRÔT%ëq¿¤ù&µ¿¿Jä©Ìhw"Wy?“úkmƒTÃç
                                                                                                                                                                                
                                                                                                                                                                                
ÇDˆ¿1LŽ >ðÕWõXÏC#N:Í
÷¤˜Î'Y´$ï)&ƒ@Ÿ>-!„j%ºŸ:f?Ëå,ÒÖ6iº´XkË(Á)¶Rn¤¥

Open in new window



However when I access the URL:
https://graph.facebook.com/cnninternational/posts?access_token=xxxxx&format=json

Open in new window


I get something like this

{
   "data": [
      {
         "id": "18793419640_246085255559206",
         "from": {
            "category": "Tv network",
            "name": "CNN International",
            "id": "18793419640"
         },
         "message": "If you\u2019re afraid of heights, you might want to avoid stepping into this \u201cvoid\u201d at the top of the French Alps: http://cnn.it/1kzMekv",
         "picture": "http://external.ak.fbcdn.net/safe_image.php?d=AQB_ttzD_Vr9LPOB&w=154&h=154&url=http\u00253A\u00252F\u00252Fi2.cdn.turner.com\u00252Fcnn\u00252Fdam\u00252Fassets\u00252F131225115813-void-tease-story-top.jpg",
         "link": "http://cnn.it/1kzMekv",
         "name": "The eighth wonder of the world? 'Step into The Void'",
         "caption": "edition.cnn.com",
         "description": "If you're scared of heights, then this is not for you.",
         "icon": "http://static.ak.fbcdn.net/rsrc.php/v2/yq/r/SC2ZmEkfI-X.png",
         "privacy": {
            "value": ""
         },
         "type": "link",
         "status_type": "shared_story",
         "created_time": "2014-01-14T15:09:29+0000",
         "updated_time": "2014-01-14T15:22:39+0000",
         "shares": {
            "count": 15
         },
         "likes": {
            "data": [
               {
                  "id": "100000320486234",
                  "name": "Cris Pereira"
               },
               {
                  "id": "1133601526",
                  "name": "Diego Hernandez"
               },
               {
                  "id": "100004287573273",
                  "name": "Nawa Nazeer"
               },

Open in new window


  I have no idea what went wrong the code was working perfectly and all of the sudden it started to do so.
PHPScripting LanguagesWeb Languages and Standards

Avatar of undefined
Last Comment
Ashraf Hassanein

8/22/2022 - Mon
Ray Paseur

Do you get the same garbled result when you use cURL instead of file_get_contents()?
Gary

Possibly/probably by the looks of it it is being gzipped by the server.

Not something I would have come across before but maybe this will help
http://www.php.net/manual/en/function.gzdecode.php#106397

Of course you could go with the curl option ^^^^^^
ASKER
Ashraf Hassanein

Same corrupted output with Curl, here is my code:
<?php
$page_id = 'cnninternational';
$access_token = 'xxxxxxxxxxxxxxxx';
$feed = 'https://graph.facebook.com/'.$page_id.'/posts?access_token='.$access_token.'&format=json';
   $headers = array(
               "Content-Type: application/x-www-form-urlencoded; charset: UTF-8"
            );
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $feed);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $putput = curl_exec($ch);
                                                                                                                                                                                
    curl_close($ch);
var_dump($output);
?>

Open in new window



The second assumption if the server is gzipped, then how of the sudden is doing so?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ASKER CERTIFIED SOLUTION
Ray Paseur

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
Ashraf Hassanein

Now it worked, and when I removed the
curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );

Open in new window


From the code I got a corrupted output again so it looks it is something related to gzip as cathal mentioned, but now can I change the coding of file_get_contents?
ASKER
Ashraf Hassanein

Cathal now I am getting:


PHP Fatal error:  Call to undefined function gzdecode()


Is there a place to enable it in  php.ini?
Gary

PHP was not compiled with it then
I haven't used this but it seems to get around the problem and let you use the 'function'
http://include-once.org/p/upgradephp/
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
Ashraf Hassanein

The gzdecode retrieved the text but as a text stream, not as a json
I am trying the curl and it is working
I do not what happened and all of the sudden I got back gzip format that is really annoying
Thanks for all for the great support