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
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

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

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 ^^^^^^
Avatar of Ashraf Hassanein
Ashraf Hassanein

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Ashraf Hassanein

ASKER

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?
Avatar of Ashraf Hassanein

ASKER

Cathal now I am getting:


PHP Fatal error:  Call to undefined function gzdecode()


Is there a place to enable it in  php.ini?
Avatar of Gary
Gary
Flag of Ireland image

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/
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Ashraf Hassanein

ASKER

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
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo