Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
591 views
in Technique[技术] by (71.8m points)

php - why file_get_contents returning strange characters?

I am trying to parse http://www.desi-tashan.com/category/pakistan-tvs/aaj-tv/3-idiots/ with file_get_contents.

But it returns very unusual characters and symbols.

where as if I parse http://www.desi-tashan.com/ it works nicely. Could someone tell why is this happening?

Is there any encoding decoding involved?

The page seems to be made with wordpress..

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

the content you see is gzipped

you might be interested looking at gzdecode or zlib-decode (Please note that Zlib support in PHP is not enabled by default)

Your code might look like this

$url = 'http://www.desi-tashan.com/category/pakistan-tvs/aaj-tv/3-idiots/';
$content = file_get_contents($url);
$decoded_content = gzdecode($content); // or zlib_decode($content);

Another solution here on stackoverflow, which adds HTTP header Accept-Encoding in the request telling the server NOT to gzip.

However, it doesn't work on www.desi-tashan.com, the server is ignoring Accept-Encoding header, and always return gzipped content


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...