Seeking PHP script to parse Twitter xml file

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

dannydddAsked:
Who is Participating?
 
stilliardConnect With a Mentor Commented:
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
 
Lukasz ChmielewskiCommented:
0
 
dannydddAuthor Commented:
Roads Roads, thanks. I'm looking for a bit more hand holding and code to solve my specific problem if poss
0
 
dannydddAuthor Commented:
solved the problem perfectly- thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.