Solved

Seeking PHP script to parse Twitter xml file

Posted on 2011-02-10
4
412 Views
Last Modified: 2012-05-11
Hello,

I am trying to create a script, in PHP, to parse XML files of the type attached.

The script needs to build up a text string containing the text of all tweets (ie what is enclosed in the <text> tags) sent within the previous 24 hours only (as shown in the <created_at> tags). Each tweet should be separated by <br>. I've started to look into but not sure how to handle the date filtering. It also needs to count the number of these tweets.

Very grateful if someone could share a script that achieves this.

Thanks

<?xml version="1.0" encoding="UTF-8"?>
<statuses type="array">
<status>
  <created_at>Thu Feb 10 20:25:31 +0000 2011</created_at>
  <id>35796524743008256</id>
  <text>Can you have lunch at dinner?</text>
  <source>&lt;a href=&quot;http://twitter.com/&quot; rel=&quot;nofollow&quot;&gt;Twitter for iPhone&lt;/a&gt;</source>

  <truncated>false</truncated>
  <favorited>false</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <retweet_count>100+</retweet_count>
  <retweeted>false</retweeted>

  <user>
    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>
<status>

  <created_at>Wed Feb 09 20:30:11 +0000 2011</created_at>
  <id>35435312620511232</id>
  <text>@missblairotte merci</text>
  <source>&lt;a href=&quot;http://twitter.com/&quot; rel=&quot;nofollow&quot;&gt;Twitter for iPhone&lt;/a&gt;</source>

  <truncated>false</truncated>
  <favorited>false</favorited>
  <in_reply_to_status_id>35432493394046976</in_reply_to_status_id>
  <in_reply_to_user_id>28155812</in_reply_to_user_id>
  <in_reply_to_screen_name>missblairotte</in_reply_to_screen_name>
  <retweet_count>55</retweet_count>

  <retweeted>false</retweeted>
  <user>
    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>

</status>
<status>
  <created_at>Wed Feb 09 09:57:59 +0000 2011</created_at>
  <id>35276215518629888</id>
  <text>Stoked about my new apple iCharger  http://post.ly/1b1hU</text>
  <source>&lt;a href=&quot;http://posterous.com&quot; rel=&quot;nofollow&quot;&gt;Posterous&lt;/a&gt;</source>

  <truncated>false</truncated>
  <favorited>false</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <retweet_count>100+</retweet_count>
  <retweeted>false</retweeted>

  <user>
    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>
<status>

  <created_at>Tue Feb 08 07:34:52 +0000 2011</created_at>
  <id>34877807951740928</id>
  <text>Thank you for the bday wishes. Every year I wish for one more happy healthy year and haven't been let down yet!</text>
  <source>&lt;a href=&quot;http://www.uber-social.com&quot; rel=&quot;nofollow&quot;&gt;UberSocial App&lt;/a&gt;</source>

  <truncated>false</truncated>
  <favorited>false</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <retweet_count>100+</retweet_count>
  <retweeted>false</retweeted>

  <user>
    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>
<status>

  <created_at>Mon Feb 07 23:50:52 +0000 2011</created_at>
  <id>34761041351159808</id>
  <text>If you are single in Chicago and looking for something to do on VDay my friend is throwing an event http://www.allneedlove.com/</text>
  <source>&lt;a href=&quot;http://www.uber-social.com&quot; rel=&quot;nofollow&quot;&gt;UberSocial App&lt;/a&gt;</source>

  <truncated>false</truncated>
  <favorited>false</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <retweet_count>100+</retweet_count>
  <retweeted>false</retweeted>

  <user>
    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>
<status>

  <created_at>Mon Feb 07 03:46:24 +0000 2011</created_at>
  <id>34457926735368192</id>
  <text>Now the depression of no more football sets in. Please settle the player owner differences. I can't live w/o my NFL</text>
  <source>&lt;a href=&quot;http://www.uber-social.com&quot; rel=&quot;nofollow&quot;&gt;UberSocial App&lt;/a&gt;</source>

  <truncated>false</truncated>
  <favorited>false</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <retweet_count>100+</retweet_count>
  <retweeted>false</retweeted>

  <user>
    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>
<status>

  <created_at>Fri Feb 04 18:32:14 +0000 2011</created_at>
  <id>33593690559758336</id>
  <text>Zach Wahls, you make me proud brother Iowan http://www.youtube.com/watch?v=FSQQK2Vuf9Q&amp;feature=youtube_gdata_player</text>
  <source>&lt;a href=&quot;http://www.uber-social.com&quot; rel=&quot;nofollow&quot;&gt;UberSocial App&lt;/a&gt;</source>

  <truncated>false</truncated>
  <favorited>false</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <retweet_count>100+</retweet_count>
  <retweeted>false</retweeted>

  <user>
    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>
<status>

  <created_at>Fri Feb 04 17:47:56 +0000 2011</created_at>
  <id>33582539771748352</id>
  <text>I'm selling a house my father an I built together in Los Angeles on gilt. All profits go to The @DNAfoundation http://&#252;b.com/rrq</text>
  <source>&lt;a href=&quot;http://www.uber-social.com&quot; rel=&quot;nofollow&quot;&gt;UberSocial App&lt;/a&gt;</source>

  <truncated>false</truncated>
  <favorited>false</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <retweet_count>100+</retweet_count>
  <retweeted>false</retweeted>

  <user>
    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>
<status>

  <created_at>Fri Feb 04 02:07:37 +0000 2011</created_at>
  <id>33345905130356736</id>
  <text>Solitary triumph starts and ends with loneliness.</text>
  <source>&lt;a href=&quot;http://www.uber-social.com&quot; rel=&quot;nofollow&quot;&gt;UberSocial App&lt;/a&gt;</source>

  <truncated>false</truncated>
  <favorited>false</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <retweet_count>100+</retweet_count>
  <retweeted>false</retweeted>

  <user>
    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>
<status>

  <created_at>Thu Feb 03 17:51:50 +0000 2011</created_at>
  <id>33221134488965120</id>
  <text>&#8220;@Gizmodo:  James Cameron (@JimCameron) is stuck in an aquarium! He's answering tweets to promote #Sanctum&#8221;</text>
  <source>&lt;a href=&quot;http://twitter.com/&quot; rel=&quot;nofollow&quot;&gt;Twitter for iPhone&lt;/a&gt;</source>

  <truncated>false</truncated>
  <favorited>false</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <retweet_count>100+</retweet_count>
  <retweeted>false</retweeted>

  <user>
    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>
<status>

  <created_at>Thu Feb 03 17:07:49 +0000 2011</created_at>
  <id>33210056048644096</id>
  <text>my man @SupaCreative put together a pretty hot beat http://www.youtube.com/watch?v=v3uyGvtumuU bravo</text>
  <source>web</source>
  <truncated>false</truncated>
  <favorited>false</favorited>

  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <retweet_count>65</retweet_count>
  <retweeted>false</retweeted>
  <user>
    <id>19058681</id>

  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>
<status>
  <created_at>Thu Feb 03 02:17:27 +0000 2011</created_at>
  <id>32985989056045056</id>

  <text>RT: @dnafoundation We collaborated w/ @jackvartanian 2 create a necklace 4 V-Day &amp; 50% of sales R donated 2 DNA http://ow.ly/3OrUq</text>
  <source>web</source>
  <truncated>false</truncated>
  <favorited>false</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>

  <in_reply_to_screen_name></in_reply_to_screen_name>
  <retweet_count>98</retweet_count>
  <retweeted>false</retweeted>
  <user>
    <id>19058681</id>
  </user>
  <geo/>

  <coordinates/>
  <place/>
  <contributors/>
</status>
<status>
  <created_at>Wed Feb 02 22:20:52 +0000 2011</created_at>
  <id>32926453838528512</id>
  <text>I FANCY the new UI on THE FANCY http://www.thefancy.com/kutcher</text>

  <source>web</source>
  <truncated>false</truncated>
  <favorited>false</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <retweet_count>98</retweet_count>

  <retweeted>false</retweeted>
  <user>
    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>

</status>
<status>
  <created_at>Wed Feb 02 22:18:38 +0000 2011</created_at>
  <id>32925890056818688</id>
  <text>laughing at Cat Reitman breaking it down http://www.youtube.com/user/catherinereitman?feature=mhum#p/a/u/0/dmFlmMoAPvQ</text>
  <source>web</source>
  <truncated>false</truncated>

  <favorited>false</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <retweet_count>75</retweet_count>
  <retweeted>false</retweeted>
  <user>

    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>
<status>
  <created_at>Sun Jan 30 21:53:38 +0000 2011</created_at>

  <id>31832434383257600</id>
  <text>@YellAdam yeah, but if I knew the right way to say it I wouldn't be going to google translate....</text>
  <source>web</source>
  <truncated>false</truncated>
  <favorited>false</favorited>
  <in_reply_to_status_id>31832100051099648</in_reply_to_status_id>

  <in_reply_to_user_id>72017663</in_reply_to_user_id>
  <in_reply_to_screen_name>YellAdam</in_reply_to_screen_name>
  <retweet_count>100+</retweet_count>
  <retweeted>false</retweeted>
  <user>
    <id>19058681</id>

  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>
<status>
  <created_at>Sun Jan 30 21:46:35 +0000 2011</created_at>
  <id>31830662386290688</id>

  <text>this just in... Google translate blows.  I want to invent a social translation platform where we can wiki edit evolve translation.</text>
  <source>web</source>
  <truncated>false</truncated>
  <favorited>false</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <in_reply_to_screen_name></in_reply_to_screen_name>

  <retweet_count>100+</retweet_count>
  <retweeted>false</retweeted>
  <user>
    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>

  <place/>
  <contributors/>
</status>
<status>
  <created_at>Sun Jan 30 19:25:42 +0000 2011</created_at>
  <id>31795204847575040</id>
  <text>@Sanctioning_Agt lol</text>
  <source>web</source>

  <truncated>false</truncated>
  <favorited>false</favorited>
  <in_reply_to_status_id>31795003978162178</in_reply_to_status_id>
  <in_reply_to_user_id>17452868</in_reply_to_user_id>
  <in_reply_to_screen_name>Sanctioning_Agt</in_reply_to_screen_name>
  <retweet_count>19</retweet_count>

  <retweeted>false</retweeted>
  <user>
    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>

</status>
<status>
  <created_at>Sun Jan 30 19:22:50 +0000 2011</created_at>
  <id>31794486006779905</id>
  <text>@jkozuch the voice of the people is being heard. free press demands government transparency. bottom up media implodes closed networks</text>
  <source>web</source>
  <truncated>false</truncated>

  <favorited>false</favorited>
  <in_reply_to_status_id>31793080587784192</in_reply_to_status_id>
  <in_reply_to_user_id>677753</in_reply_to_user_id>
  <in_reply_to_screen_name>jkozuch</in_reply_to_screen_name>
  <retweet_count>16</retweet_count>
  <retweeted>false</retweeted>

  <user>
    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>
<status>

  <created_at>Sun Jan 30 19:12:58 +0000 2011</created_at>
  <id>31792000751636480</id>
  <text>Annoying=&quot;social media experts&quot; on the news explaining social medias role in #Egypt Democratization of media will shape the future, period</text>
  <source>web</source>
  <truncated>false</truncated>

  <favorited>false</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <retweet_count>100+</retweet_count>
  <retweeted>false</retweeted>
  <user>

    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>
<status>
  <created_at>Sun Jan 30 06:12:40 +0000 2011</created_at>

  <id>31595631478833153</id>
  <text>hey @Ludacris the people have spoken and they like you in #nostringsattached</text>
  <source>&lt;a href=&quot;http://www.uber-social.com&quot; rel=&quot;nofollow&quot;&gt;UberSocial App&lt;/a&gt;</source>
  <truncated>false</truncated>

  <favorited>false</favorited>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <retweet_count>100+</retweet_count>
  <retweeted>false</retweeted>
  <user>

    <id>19058681</id>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>
</statuses>

Open in new window

0
Comment
Question by:dannyddd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34866566
0
 

Author Comment

by:dannyddd
ID: 34866619
Roads Roads, thanks. I'm looking for a bit more hand holding and code to solve my specific problem if poss
0
 
LVL 6

Accepted Solution

by:
stilliard earned 500 total points
ID: 34866685
Have you tried php's simplexml ?
something like this should work...
<?php
date_default_timezone_set('Europe/London');

$result = '';
$xml = simplexml_load_file('XML FILE URL');

$statuses = $xml->status;
$aDayAgo = mktime(date("H")-24, date("i"), date("s"), date("m"), date("d"), date("y"));
foreach($statuses as $status) {
	
	if($aDayAgo > strtotime($status->created_at)) break;
	$result .= $status->text . "<br>\n";
}

echo $result; 
?>

Open in new window

0
 

Author Closing Comment

by:dannyddd
ID: 34867084
solved the problem perfectly- thanks
0

Featured Post

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question