View Full Version : Warning: simplexml_load_file

01-22-2012, 06:11 PM
I've been working on this for 2 days and can't figure it out. All I want to do is show the last 5 tweets from a twitter account on my homepage. The code below works fine when I call the file by it self: http://www.mysite.com/gettwit.php

But when I try and SSI include it <!--#include virtual='gettwit.php' --> it gives me:

Warning: simplexml_load_file(http://twitter.com/statuses/user_timeline/kingjames.xml?count=5) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /hermes/web02/b1918/moo.mysitecom/gettwit.php on line 8

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://twitter.com/statuses/user_timeline/kingjames.xml?count=5" in /hermes/web02/b1918/moo.mysitecom/gettwit.php on line 8
could not connect

My homepage is .shtml and has other SSI working fine. I've tried using urlencode and it worked a few times then went back to giving this error. How does something work and then just stop working without editing the code?!!! What am I missing? Also how do I add an error handler so it shows "Cannot Connect" when it has problems? Thanks for any help. This is frustrating.


function getLastXTwitterStatus($userid,$x){
$url = 'http://twitter.com/statuses/user_timeline/'.$userid.'.xml?count='.$x.'';

///$url = urlencode($url);

$xml = simplexml_load_file($url) or die('could not connect');
echo '<ul style:text-align:left;>';
foreach($xml->status as $status){
$text = twitterify( $status->text );
echo '<li>'.utf8_decode($text).'</li>';
echo '</ul>';

function twitterify($ret) {
$ret = preg_replace("#(^|[\n ])([\w]+?://[\w]+[^ \"\n\r\t< ]*)#", "\\1<a href=\"\\2\" >\\2</a>", $ret);
$ret = preg_replace("#(^|[\n ])((www|ftp)\.[^ \"\t\n\r< ]*)#", "\\1<a href=\"http://\\2\" >\\2</a>", $ret);
$ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" >@\\1</a>", $ret);
$ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" >#\\1</a>", $ret);
return $ret;



01-22-2012, 06:53 PM
You already do show that it cannot connect. You need to reduce the value of your error reporting or add a suppression (@) to the simple_xml_load_file so it doesn't trigger the warning.

01-22-2012, 10:20 PM
Just a question. Did you choose that method over this one?
I personally use that one, and return a json object. Is the api.twitter.com a more up to date API?

01-23-2012, 03:10 AM
Yeah, I found this about an hour after I made this post and is working fine.

<div id="twitter_update_list"></div>
<script type="text/javascript" src="http://twitter.com/javascripts/blogger.js"></script>
<script type="text/javascript" src="http://twitter.com/statuses/user_timeline/kingjames.json?callback=twitterCallback2&count=5">

My next challenge is to randomly pick a username from a list and use that username and to have it print the username before the tweets like:

echo {userid}."tweets:";
Then show the tweets.