Apr
23
This little snippet was originally used in a small CodeIgniter library I wrote which would fetch my most recent tweet, the source of the tweet, and the amount of followers I currently had.
//Change the value of $user to your Twitter username $user = 'yourusername'; $ch = curl_init('http://twitter.com/users/show.xml?screen_name='.$user); //We are going to open/create a local file to write our fetched data to it. $fp = fopen('/'.$user.'.xml', "w");
//Set cURL options for our file, not to write the XML header curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, false); //Excecute cURL curl_exec($ch); //Lets close up these functions curl_close($ch); fclose($fp); //This is the location of the user.xml file $file = '/'.$user.'.xml'; //Here we call PHP's SimpleXMLElement Class to handle the .xml file $xml = new SimpleXMLElement($file, NULL, TRUE); //Simply declare that $path is an array that contains all of node $path = $xml->xpath('/user'); //create an array of the elements in the xml file we want to display foreach ($path as $node) { $status = array('time' => $node->status->created_at, 'text' => $node->status->text, 'source' => $node->status->source, 'followers' => $node->followers_count); } print_r($status);