Reputation: 1837
I should to display datas from a rss file from another server.
When the rss file is on my server, I can read it, but when I try to read the same file on another server, I obtain this :
Warning: simplexml_load_file(rssfile) [function.simplexml-load-file]: failed to open stream: Connection timed out in index.php on line 43
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "rssfile" in index.php on line 43
This is my code :
$actus = simplexml_load_file('rssfile');
foreach ($actus->channel->item as $actu)
{
echo $actu->title;
}
How to fix it ?
I think the problem comes from Symfony
Upvotes: 0
Views: 1107
Reputation: 31
I agree with VolkerK, the error comes from a bad url provided.
In addition, as you are using symfony, I advise you to go for sfFeed2Plugin that will do a lot more than the simplexml loader (validation, output methods, and even, if needed, serving).
Here's a quick usage example:
// define your source url
$source_url = 'http://feeds.feedburner.com/TechCrunch';
// fetch url
if($feed = sfFeedPeer::createFromWeb($source_url))
{
// get items
$items = $feed->getItems();
foreach($items as $item)
{
// do whatever you want with each item
echo $item->getTitle()."\n";
}
}
Upvotes: 0
Reputation: 96159
Since the error/warning message is "Connection timed out" let's take a closer look at the timeout options.
There's default_socket_timeout and the http context option timeout
which might have been set via stream_context_set_default.
Try this
$xmlsrc = 'http://some.host/a/path/foo.rss';
$actus = simplexml_load_file($xmlsrc);
if ( !$actus ) {
echo "simplexml_load_file() failed.<br />\n";
echo '$xmlsrc='; var_dump($xmlsrc); echo "<br />\n";
echo 'default_socket_timeout=', ini_get('default_socket_timeout'), "<br />\n";
$defaultOptions = stream_context_get_options(stream_context_get_default());
echo 'default options='; var_dump($defaultOptions); echo "<br />\n";
die;
}
foreach ($actus->channel->item as $actu) ...
in both scenarios (with/without symfony) on the same server. Do the (timeout) values differ?
Upvotes: 0
Reputation: 1
You can then read it with something like this ...
private static string LoadResource(string rname)
{
System.IO.Stream s = null;
try
{
s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(rname);
return new System.IO.StreamReader(s).ReadToEnd();
}
finally
{
if (s != null)
{
s.Dispose();
}
}
}
Upvotes: 0