5

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 ';
}
3
  • 1
    Take a look at how to debug file_get_contents() Commented Dec 14, 2015 at 10:52
  • Are there any errors in your log? Make sure error logging is enabled. Commented Dec 14, 2015 at 10:54
  • 2
    u have to use User-Agent , that /feed/ script checks for it. Commented Dec 14, 2015 at 11:02

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

more effective :) perfect answer
2

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.