PHP Curl returning inconsistent data for XML feed

I am working on an XML reader and am facing an odd issue with multiple feeds. Using CURL or even file_get_contents, feed is loaded as binary data more often than real data. Whenever I load a feed in a browser, it looks fine.

Specific feed http://www.winnipegsun.com/home/rss.xml

The code I use

$string = file_get_contents("http://www.winnipegsun.com/home/rss.xml"); var_dump( $string ); 
+4
source share
2 answers

Gzipped answer:

If you look at the HTTP headers: Content-Encoding: gzip

Unzip it using PHP:

 gzinflate(substr($string, 10)); 

http://php.net/manual/en/function.gzinflate.php

Hope this helps ... cheers

+3
source

You should be able to send an empty Accept-Encoding server to the server, and then it should not send gzipped content or return a Not Acceptable response:

 $string = file_get_contents( "http://www.winnipegsun.com/home/rss.xml", FALSE, stream_context_create( array( 'http' => array( 'method' => "GET", 'headers' => 'Accept-Encoding:\r\n' ) ) ) ); var_dump($string); 

I'm not sure that the web server is configured correctly, because it would not respond to it with an uncompressed channel, even if you add the Cache Control headers telling it not to send a cached response. Oddly enough, just doing

 $string = file_get_contents("http://www.winnipegsun.com/home/rss.xml?".time()); 

worked out of the box. And you can also send a POST request.

0
source

All Articles