PHP Curl returning inconsistent data for XML feed
I'm working on a XML reader and am running into a odd issue with a few feeds. Using CURL or even file_get_contents the feeds load as binary data more often than real data. Whenever I load the feed in a browser it looks fine.
The specific feed is http://www.winnipegsun.com/home/rss.xml
The code I am using is
$string = file_get_contents("http://www.winnipegsun.com/h开发者_如何学Come/rss.xml");
var_dump( $string );
The response is gzipped:
If you look at the HTTP headers: Content-Encoding: gzip
Unzip it with PHP:
gzinflate(substr($string, 10));
http://php.net/manual/en/function.gzinflate.php
Hope that helps... cheers
You should be able to send an empty Accept-Encoding
header to the server and then it should not send the content gzipped 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 am not sure the webserver is configured correctly though, because it wouldnt respond to that with the uncompressed feed, even when adding Cache Control headers telling to it not 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.
精彩评论