tags:

views:

1372

answers:

4

Im using simplexml to get the twitter profile avatar url from the xml status page.

this is the code im using

<?
$username = twitter;
$xml = simplexml_load_file("http://twitter.com/users/".$username.".xml");
echo $xml->user->profile_image_url;
?>

The xml page loads when i visit it, but for some reason nothing is being echoed. No errors. Nothing.

When i visit it in a browser, i get this:

<?xml version="1.0" encoding="UTF-8"?>
<user>
  <id>783214</id>
  <name>Twitter</name>
  <screen_name>twitter</screen_name>
  <location>San Francisco, CA</location>
  <description>Always wondering what everyone's doing.</description>    
  <profile_image_url>http://a1.twimg.com/profile_images/75075164/twitter_bird_profile_normal.png&lt;/profile_image_url&gt;
  <url>http://twitter.com&lt;/url&gt;.....
  (the rest is long and irrelevant to the question)

The data is there, why wont it echo?

+6  A: 

After loading the XML document, the root element user is represented by the SimpleXMLElement object saved in $xml. Therefore $xml->user does not exist.

This one should work:

<?
$username = "twitter";  // <-- You did not use quotes here?! Typo?
$xml = simplexml_load_file("http://twitter.com/users/".$username.".xml");
echo $xml->profile_image_url;  // <-- No $xml->user here!
?>
Ferdinand Beyer
So whenever i load an XML file like this, count the first node out?
Patrick
+1 for beating me to it with a better explanation :)
Splash
For the SimpleXML API, yes. The root node is represented by the object returned by simplexml_load_*().
Ferdinand Beyer
+2  A: 

It is because the root element (in this case, <user>) is implied - you do not have to specify it.

Try this:

echo $xml->profile_image_url;
Splash
A: 

Twivatar

The easiest way to get the twitter avatar is to use Twivatar, which was built by Remy Sharp

Simply use the following url: <img src="http://twivatar.org/[screen_name]" />

From the docs:

Alternatively you can specify the size image you want from:

  • mini (24x24)
  • normal (48x48 - default)
  • bigger (73x73)
  • original

    <img src="http://twivatar.org/[screen_name]/[size]" />

Jon Winstanley
A: 

hey guys - tried out twivatar but I was getting an error 500 error????

anyway here is the code I ended up using - how it is helpful

<?php
if (isset($_REQUEST['user']))

//if "user" is filled out, get the user's twitter picture 

  {
$username = $_REQUEST['user'];
}else{
$username = 'aplusk';

//else lets use ashton kutcher's picture just for the F of it 

}  // <-- You did not use quotes here?! Typo?
$xml = simplexml_load_file("http://twitter.com/users/".$username.".xml");
$timage = $xml->profile_image_url;  // <-- No $xml->user here!
header('location: ' . $timage);
?>

Now it is extremely easy to call this script from anywhere in your application simply print ...

<img src="http://mywebapp.org/timage.php?user=&lt;php echo $data['twitter_screen_name'];?>" />
freshfitteds