PHP: file_get_contents return garbage code - Help please

Started on the old forum in October 2012 · view the original in the archive · old links redirect here

10 replies · 2,337 views · started 14 years ago · 1 min read

4 participants
Tien Nguyen Van Newbie · 4 posts #1

Dear all,

I am trying to make a code to get HTML source from "http://vnexpress.net" (a news site in my country). I am using file_get_contents as below:

<? $homepage = file_get_contents('http://vnexpress.net'); echo $homepage; ?>

I also tried to use CURL and set request header information to be similar with chrome browser but not effect. My php always return a garbage code like below: 😠

‹í½I–%&/mÊ{JõJ×àt¡€$ؐ@ìÁˆÍæ’ìiG#)«ÊeVe]f@Ì흼÷Þ{ï½÷Þ{ï½÷º;N'÷ßÿ?\fdlöÎJÚÉž!€ªÈ?|?"ÿ®O¿|(>J¹ý=žçÙŒý±{¼ÈÛŒpnWÛù/Z—Ÿ}tR-Û|Ùn¿¹^å¥Sù볏Úü]{0Óé<«›¼ýì«7϶>Jïý؏9PËl‘öQ]Mª¶ñ^?{ñôô÷=ûòùó/¿‹Wi¯ù,o¦u±Â¸¼wÞÌÿÑ¿my‘¶Å2]γå<ý…Ùbu˜.þã¿÷Ï-è“ÿøïùËÛôû“ÿ‰¿é?þ{ÿÒi:ýÿž¿~ÅŸþõm:ÿGÿ"zñ¢øÿÞ?œ¾¡÷Ûÿøïý«§éOÒd›¾È ª¥æÿ ·#ˆ—ÿñßû§ïþÑ¿4ÿÇïŸ]ŒÒ·u9«¨ãQºšÿ£ñ-×€NßÌ+¢Ùü÷þñÓQ:e—ôç9Jÿÿž¿wDÈ>ü“—£ôòûC—„ÚßšÒ:«¨¿¿çï$í?ú—,ÐäoÇJ™¶hËüx˸ryúnUçM“n§ÿØŸ„~ÓÉ?úWô.5‘¡U˲XæéÞþüñ]y':˜”·ùõUUÏüi±G?Yä-5K_äWMú4+ÊëÑ)5«×£/öèŒÞ©—y;ú"»È~€O>ÏÚü*»={ùfô²ªÛ¬Y¤Gþ„Œ€.‘ƒýG2Æýè¿·2 Cþf4€Âh±ÎðåräHÕþ~ûË—òò4-ÿÑ¿h1úö?ú¥/xÆ^ÿ£Q‘~þþ-KH½üÕÓÑ·iÊþÁÑ?ö'q£¿ço§iøÉ§'£<ÖÑ문¨–Òë‹‹Õè'Oߌ^ÌÿÑ¿d™ÎèŸÑsê3øgÓK¿x¡(þM¸Âk†‘f4NzgVäÔûš‡B£~}A#z’-G¯¨ÍORÛ7kú±D³iúœ°ùv¶¬ ú7}A?5üHñ“È)âÇ·×ùè)5̵§Ù?^"3‡Ÿ)!ôg}A? ú˜·9ÿHõ§Œ¿Qúüçɲ¯^>{uúúÛïìììà…>£µíï?%Ö¸¨êëß¿˜ù¯°Jõé¼’Õm1-óþ©ö@\ø6*óò³šöºÌ›yž·¥ó:?ÿ죻¯ò¦Z×ÓüîIµXTË»³ü<[—íxÚ¿·¤Ë¨¨0þ›Fùc?–ŠÚÂÿ=ÐY .'ìÍ[ÙjU4ÒMwë¦ù„Ô,}óćóÕëיׯïþü.´õÅöt¾Ó‹è}q‡øÿcÑyÚ #÷ÓÙe&Ÿ”–ôî:» ¯üO›zJðŸ“:«¯ÍXõϽñO7=¾+miˆ©å³á¾nÝ×O.óF7Ïʬ™‘ÏŠ¬ÛžŸ:{÷Š˜=oÚ°Ëov®.I|µ,¾Ñ.,ïïìÜ=®ë/òå:„O„â‡HG´{Ͼ^˧Œä]fuúòøóÓßÿõÙ›ÓÏvÃɾ?=}õÙî!÷â>?{|öôË/N¿9;ùýÏ^ÚÏÉ¡ £X^|QÍòÝ;™ü´á?ä÷Û!o>íJ'£O)t@ÿßÜ·Â@ÿ‹ì¥ŸÉŸù™ô{ßg¬ñçxµnæ[ßûø÷§qO§ÕzÙ^ÕU[M«2ý=Rmx÷nÓ”§äoú“çï¤Ÿ¤/ªê¢Ì·É¾–×dššñ´ZÜ¥ž~ºù˜;Å€êÓvs‘·:˜æÉõ›ì‚ÍÜ ë{;ß?L›ñ«©Á âòq±lòº}’ŸWu¾uAna#äû%w¶øŸ+é–+i¨ÄÌÅòÓ¤ºêëíÝñ§ã{ãE±$4=¾–9!ßRþì¸ðP÷¿q‚Ábš¿]µ4d>³Ðù/ùc´ ]*ÒDJ }¹ÌwÙuˆ, ÑhQöí½žmùF‚ÌtIfYÍo‡2¯ów×Ç%(]½»ÛÐþ˜TïØ9€/ÀÝIç>ü>ú·†|¹;&sÌSÒM« ¿OªÙ5¨'(È¿‹åjmð˜³YNAQ1£Èl]ƒužÔÕñÍw‹YKÛeV®©ñ._~=¯®ž¨^Ô$Üäaé{ä_ñ‹?öxV\rCúùº,f9éJòŸžçç<èÿÙGûþv¯Hv-+ýíÐûnUIˆú¨..æ„Kµ:¬.ózû¼¬® N‡ÓuÝTõ£UEm^‚ÜÛYY\èK‡«l6#cðhgõîÐÂKÏ‹wùìw-+Kje¿É&MU®Ûü0½û*"ÿ4%g˜>£n¿u÷°¤1&]’鏌þO €ôÁ8-· Iþt²ñ˜Ü¤i€f=Y$¬uÞ®ëez2ϧoÖlË:b¯ùÕ1¹S$ØLYÒ=âÞ Ô1ɱDîcJÿÜ]RÆIÍ$ý‘f%ô/éŸ8‡ð7؝—LV¶Dš6óM0øËÛ7ÿñßû×Rr2€”õûë”a¤ŒÒâ#“j/éïËå3ü¹GÚZRânV]ÑôõŠ4Ú²@ÀDâd›A:Oè/óFF”AÑ/:†ö]+äHÏiN ö¤5«å”rooOcQžÄ“ÄÊ‘žnÕî²Î1óéÏ£)r²N–^Ÿ~úìéÁéɃígÇ>ÝÞ?~p²}ðìôÞöÓ'Ÿîܲòðä””žŒêâ6¯s¼´Ÿ¬Û×úÊã»à,þÝ2¦ûåwÝÞþ^qžRZ(=;M?ý>xô$þ>;ýÔØ›'&uÄ:Ùèèþ‹xƒ5ôŽQ¹Ä8¤ÑÉ2S2;«/ ²èô«¾Ä¿‹OI…ø¦ó|ö(Ý»O¡§šþ?;zü»~/_Ίóïoo3ª„²¡5MÉ1}øc×%ÿ„?}ä7!&¡#\àƒPñcòÐ¥9ÿCï˜æ#²þò%l± †–håb…žŸÓ‡"ýï¾Hù´µ¢>Í· ™ úDÐ ÌçsšD4•/†À¾B |JT‘»{@3FŸÐ·¤wèŸhøM½ÉåI¤Q Ë#¿]  ÅLªS¾åÀ)-Í/-•|ôû³8nQ]”“ùÛQEVZX‘’×§Ü죣SüàÓXäuqÿ§‘Ùy»]×S?‹›;ÿéj

My code is perfect with other site but always fail with "vnexpress.net". Hope you give me some advises. Thank you so much.;(

Ghost Senior · 366 posts #2

works fine for me. at first it didnt. but then i added the correct php tagging <?php ?> and i got all the content fine. although it was in a different language, but worked for me.

<?php
 $homepage = file_get_contents('http://vnexpress.net');
 echo $homepage;
 ?>
<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
Tien Nguyen Van Newbie · 4 posts #3

Still not effect

Ghost wrote: works fine for me. at first it didnt. but then i added the correct php tagging <?php ?> and i got all the content fine. although it was in a different language, but worked for me.

<?php
 $homepage = file_get_contents('http://vnexpress.net');
 echo $homepage;
 ?>

Dear Ghost. Thank you about your answer.

I modified from <? to <?php but still not effect. I am using HostGator, Chrome. Can you help telling me what 's the browser you are using and php.ini configurations on your host? Any way, thank you.

Ghost Senior · 366 posts #4

i cant get to my hosts php.ini file. when i do try to get it i get a forbidden warning and a php error report log saying its off limits.

the browser im using is opera 12.02

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
Tien Nguyen Van Newbie · 4 posts #5

Refresh many times to view "vnexpress.net"

Ghost wrote: i cant get to my hosts php.ini file. when i do try to get it i get a forbidden warning and a php error report log saying its off limits.

the browser im using is opera 12.02

OK friend. Just one more question: Did you refresh the result many time? Because the first time you get always success but always fail later. Thank you.

Ghost Senior · 366 posts #6

yeah i tried to refresh a few times. it was a little slow on getting the content as there is alot of it. but it got it everytime.

i have also just tried this using my localhost which i used for development erroring reports etc. and it worked fine there also.

heres the php.ini file from my localhost. maybe looking through that will help you out.

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
Attachments: php.ini.zip
softwarefreak Senior · 349 posts #7

Hi, that's not garbage, but gzip encoded data

If u run this code

<?
print_r(get_headers("http://vnexpress.net/"));

You'll see this

Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Server: Caching-NOC-V1.0
    [2] => Date: Sat, 13 Oct 2012 02:20:33 GMT
    [3] => Content-Type: text/html
    [4] => Connection: close
    [5] => Cache-Control: max-age=0
    [6] => Expires: Sat, 13 Oct 2012 02:20:33 GMT
    [7] => Set-Cookie: linkPDA=0; expires=Tue, 11-Dec-2012 17:00:00 GMT; path=/
    [8] => Content-Encoding: gzip
    [9] => Vary: Accept-Encoding
    [10] => Cache-Control: vne-hk-34,cached
)

NOTICE:

Content-Encoding: gzip

Solution:

<?
$file = file_get_contents("http://vnexpress.net/");
echo gzinflate(substr($file,10,-8)); 

It'll print html now There're many more ways to do the same

I need some facebook likes, can you please help me 🤝 http://facebook.com/softwarefreakin I noticed social media is really powerful 🧙 Well DONE is better than well SAID

Tien Nguyen Van Newbie · 4 posts #8

Oh, You are my GOD

softwarefreak wrote: Hi, that's not garbage, but gzip encoded data

If u run this code

<?
print_r(get_headers("http://vnexpress.net/"));

You'll see this

Array
(
    [0] => HTTP/1.1 200 OK
    [1] => Server: Caching-NOC-V1.0
    [2] => Date: Sat, 13 Oct 2012 02:20:33 GMT
    [3] => Content-Type: text/html
    [4] => Connection: close
    [5] => Cache-Control: max-age=0
    [6] => Expires: Sat, 13 Oct 2012 02:20:33 GMT
    [7] => Set-Cookie: linkPDA=0; expires=Tue, 11-Dec-2012 17:00:00 GMT; path=/
    [8] => Content-Encoding: gzip
    [9] => Vary: Accept-Encoding
    [10] => Cache-Control: vne-hk-34,cached
)

NOTICE:

Content-Encoding: gzip

Solution:

<?
$file = file_get_contents("http://vnexpress.net/");
echo gzinflate(substr($file,10,-8)); 

It'll print html now There're many more ways to do the same

Your reply touched my heart because it is so professional 😍 I tested and it can load any website like a boss. Special thank you softwarefreak. I visited your site and LIKE your facebook page, also FOLLOW your twitter stream.

HOPE ALL BEST (😊)

ps: also thank to Ghost...

softwarefreak Senior · 349 posts #9

@tien, LOL, there's the catch with a signature after my posts 😛

I need some facebook likes, can you please help me 🤝 http://facebook.com/softwarefreakin I noticed social media is really powerful 🧙 Well DONE is better than well SAID

modfiles Developer · 200 posts #10

does that mean that sites that are gzip or has a header like that will display a garbled output when grabbing contents?

just tested on my gzip page but it didn't return something like that don't know if we're using same gzip functions

softwarefreak Senior · 349 posts #11

modfiles wrote: does that mean that sites that are gzip or has a header like that will display a garbled output when grabbing contents?

just tested on my gzip page but it didn't return something like that don't know if we're using same gzip functions

When you use the PHP function file_get_contents() to fetch a remote webpage, it doesn't send any http headers by default, until you dont use stream_content_create() along with the request, on the otherhand the remote server checks if the user agent supports compression over http (HTTP_ACCEPT_ENCODING) or not, since no headers were sent, the server shouldn't have replied with gzip encoded data, But they are doing so.🤪

I need some facebook likes, can you please help me 🤝 http://facebook.com/softwarefreakin I noticed social media is really powerful 🧙 Well DONE is better than well SAID

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close