tags:

views:

39

answers:

1

I'm getting an error from this Twitter script that is causing the rest of the page to not load. Not sure why suddenly this is happening, where it was functioning properly for quite some time.

The script looks like this, and it pulls the users current status:

<?php  
$response = new SimpleXMLElement('http://twitter.com/users/show/tuscaroratackle.xml',NULL,TRUE);  
echo $response->status->text.'';  
?>

Here's another post that I was trying to figure out the answer to another bug which pointed me to this Twitter error.

You can see it here in the footer, or a screengrab of the output: http://cl.ly/33IZ.

+1  A: 

The relevant error (which is displayed in the footer of the page you linked to) is:

Warning: SimpleXMLElement::__construct(http://twitter.com/users/show/tuscaroratackle.xml) [simplexmlelement.--construct]: failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in /home5/tuscaror/public_html/footer.php on line 47

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: I/O warning : failed to load external entity "http://twitter.com/users/show/tuscaroratackle.xml" in /home5/tuscaror/public_html/footer.php on line 47

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home5/tuscaror/public_html/footer.php:47 Stack trace: #0 /home5/tuscaror/public_html/footer.php(47): SimpleXMLElement->__construct('http://twitter....', 0, true) #1 /home5/tuscaror/public_html/index.php(119): include('/home5/tuscaror...') #2 {main} thrown in /home5/tuscaror/public_html/footer.php on line 47

The first warning tells you what happened: "HTTP request failed! HTTP/1.1 400 Bad Request".

So, for some reason, your server is failing when making the HTTP request to twitter to retrieve the document "http://twitter.com/users/show/tuscaroratackle.xml". The return code is 400 Bad Request.

I just tried that same request from my web browser, and it worked fine, so either twitter was temporarily "out to lunch" (which does happen from time to time), or there is something unique about your server's network configuration. My first guess would be that somewhere up-stream from your server, someone has installed an HTTP proxy which is (for some unknown reason) blocking your request.


Here's what twitter has to say about it:

400 Bad Request: The request was invalid.  An accompanying error message 
will explain why. This is the status code will be returned during rate limiting.

Here is twitter's page on Rate Limiting. I suspect that this is your culprit. If you think otherwise, then you might try retrieving the document as a string and examining it before you try to parse it, so you can see what the message is.

This is quick and dirty, but it'll get the message so you can see what's going on:

$str = file_get_contents('http://twitter.com/users/show/tuscaroratackle.xml');
echo $str;

that may fail due to the 400 response code. if so, you'll need to use php curl to get the un-parsed response body.

good luck!

Lee
Many thanks for a very thorough and thoughtful response - I really appreciate you taking the time to explain it all. It can be so frustrating when you're trying to learn something new by trial and error (ie me with PHP and scripting!). FYI - it's been happening intermittently for me, so I guess rate limiting would certainly fit. I was also advised caching could resolve this, but on account of my skill level and for efficiency I might just opt for a simpler solution using the Twitter javascript widget. Thx again!
JAG2007
no problem! Glad I could help. ... caching really is quite easy. [Here's a simple example I just found](http://snipplr.com/view/13646/twitter-cache/). You could pretty easily take that code and change out `file_get_contents` for your usage of `SimpleXMLElement`. Whichever direction you take - good luck with it!
Lee