RSS feed returns an empty string

I have a news portal that displays RSS feed items. About 50 sources read and work very well.

Only with the source I always get an empty string. RSS Validator W3C can read RSS feed. Even my Vienna program receives data.

What can I do?

Here is my simple code:

$link = 'http://blog.bosch-si.com/feed/'; $response = file_get_contents($link); if($response !== false) { var_dump($response); } else { echo 'Error '; } 
+5
source share
2 answers

The server serving this channel expects the user agent to be installed. You apparently do not have the User Agent installed in your php.ini , and you do not install it in the file_get_contents call.

You can set the User Agent for this particular request through the context:

 echo file_get_contents( 'http://blog.bosch-si.com/feed/', FALSE, stream_context_create( array( 'http' => array( 'user_agent' => 'php' ) ) ) ); 

Or globally for any http calls:

 ini_set('user_agent', 'php'); echo file_get_contents($link); 

Both will give you the desired result.

+4
source

blog http://blog.bosch-si.com/feed/ requires a certain header to retrieve content from a website, better use curl for it.

See solution below:

 <?php $link = 'http://blog.bosch-si.com/feed/'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $link); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Host: blog.bosch-si.com', 'User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36')); $result = curl_exec($ch); if( ! $result) { echo curl_error($ch); } curl_close($ch); echo $result; 
+2
source

Source: https://habr.com/ru/post/1238102/


All Articles