Solved

file_get_contents is returning back a corrupted characters from facbook feed.

Posted on 2014-01-14
10
1,201 Views
Last Modified: 2014-01-14
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.
0
Comment
Question by:Ashraf Hassanein
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39779511
Do you get the same garbled result when you use cURL instead of file_get_contents()?
0
 
LVL 58

Expert Comment

by:Gary
ID: 39779528
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 ^^^^^^
0
 

Author Comment

by:Ashraf Hassanein
ID: 39779572
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?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 110

Accepted Solution

by:
Ray Paseur earned 334 total points
ID: 39779594
Try using this cURL function and see if the returned data makes better sense.

// A FUNCTION TO RUN A CURL-GET CLIENT CALL TO A FOREIGN SERVER
function my_curl
( $url
, $timeout=5
, $error_report=TRUE
)
{
    $curl = curl_init();

    // HEADERS AND OPTIONS APPEAR TO BE A FIREFOX BROWSER REFERRED BY GOOGLE
    $header[] = "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    $header[] = "Cache-Control: max-age=0";
    $header[] = "Connection: keep-alive";
    $header[] = "Keep-Alive: 300";
    $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
    $header[] = "Accept-Language: en-us,en;q=0.5";
    $header[] = "Pragma: "; // BROWSERS USUALLY LEAVE THIS BLANK

    // SET THE CURL OPTIONS - SEE http://php.net/manual/en/function.curl-setopt.php
    curl_setopt( $curl, CURLOPT_URL,            $url  );
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6'  ); // ANCIENT HISTORY
    curl_setopt( $curl, CURLOPT_USERAGENT,      'Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0'  );
    curl_setopt( $curl, CURLOPT_HTTPHEADER,     $header  );
    curl_setopt( $curl, CURLOPT_REFERER,        'http://www.google.com'  );
    curl_setopt( $curl, CURLOPT_ENCODING,       'gzip,deflate'  );
    curl_setopt( $curl, CURLOPT_AUTOREFERER,    TRUE  );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, TRUE  );
    curl_setopt( $curl, CURLOPT_FOLLOWLOCATION, TRUE  );
    curl_setopt( $curl, CURLOPT_TIMEOUT,        $timeout  );

    // RUN THE CURL REQUEST AND GET THE RESULTS
    $htm = curl_exec($curl);

    // ON FAILURE HANDLE ERROR MESSAGE
    if ($htm === FALSE)
    {
        if ($error_report)
        {
            $err = curl_errno($curl);
            $inf = curl_getinfo($curl);
            echo "CURL FAIL: $url TIMEOUT=$timeout, CURL_ERRNO=$err";
            var_dump($inf);
        }
        curl_close($curl);
        return FALSE;
    }

    // ON SUCCESS RETURN XML / HTML STRING
    curl_close($curl);
    return $htm;
}

Open in new window

0
 
LVL 58

Assisted Solution

by:Gary
Gary earned 166 total points
ID: 39779606
Try this

<?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');

$json_object = gzdecode($json_object );

//Interpret data
$fbdata = json_decode($json_object);
?>
0
 

Author Comment

by:Ashraf Hassanein
ID: 39779630
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?
0
 

Author Comment

by:Ashraf Hassanein
ID: 39779641
Cathal now I am getting:


PHP Fatal error:  Call to undefined function gzdecode()


Is there a place to enable it in  php.ini?
0
 
LVL 58

Expert Comment

by:Gary
ID: 39779680
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/
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 334 total points
ID: 39779839
Just use cURL.  There are a lot of advantages of cURL over file_get_contents().  One of the most important is that if the remote resource "hangs" and does not respond immediately, file_get_contents() hangs, too.  And this adds to your script execution time, until you get a timeout that causes a run time failure.  With cURL, you can set the timeout you want, and if the remote resource hangs, your script will get control back as soon as the timeout period expires.  IIRC cURL errno = 28 when that happens.
0
 

Author Comment

by:Ashraf Hassanein
ID: 39780007
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
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question