Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSS-Feed returns an empty string

Tags:

php

rss

I have a news portal that displays RSS Feeds Items. Approximately 50 sources are read and it works very well.

Only with a source I always get an empty string. The RSS Validator of W3C can read the RSS feed. Even my program Vienna 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 ';
}
like image 448
teccrow Avatar asked Dec 14 '15 10:12

teccrow


2 Answers

The server serving that feed expects a User Agent to be set. You apparently don't have a User Agent set in your php.ini, nor do you set it in the call to file_get_contents.

You can either set the User Agent for this particular request through a stream 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.

like image 117
Gordon Avatar answered Nov 07 '22 17:11

Gordon


blog http://blog.bosch-si.com/feed/ required some header to fetch content from the website, better use curl for the same.

See below solution:

<?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;
like image 30
Chetan Ameta Avatar answered Nov 07 '22 17:11

Chetan Ameta