Jump to content

XML-powered Twitter feed functionality


redesigner

Recommended Posts

Hi all.  I'm using a cron job to pull the XML feed of my last five tweets, and using simplexml to parse and display the feed on my website.  Here's the PHP I'm using--it works just fine, and displays exactly as I want it to:

 

<?php
$xml = simplexml_load_file('crontwitter.xml');
foreach ($xml->status as $status) {
print "<p><span class=\"twittertext\">{$status->text}</span><br />";
print "<a class=\"twittertime\" href=\"http://twitter.com/#!/eaners/status/{$status->id}\" target=\"_blank\">";
echo date("j M Y, g:i A",strtotime($status->created_at));
print "</a></span></p>";
}
?>

I'd like to add a touch more "intelligence" to this script, though.  Namely, there are three things I'd also like to have this script do:

[*]I'd like to append "I" to any tweets that include the #fb hashtag, so that things display correctly (e.g., "I did such and such today" instead of just "did such and such today")

[*]I'd like to link to any other Twitter users mentioned (they would always be mentioned using the @username format)

[*]I'd like to link to a search for any other hash tag mentioned aside from #fb (they would always be included using the #tag format)

I guess I'm not sure how exactly to further parse the XML file.  I'll admit to being a relative newbie when it comes to PHP, so any assistance would be greatly appreciated!

Link to comment
https://forums.phpfreaks.com/topic/219401-xml-powered-twitter-feed-functionality/
Share on other sites

I'm not sure exactly what you mean and without the XML it would be guess work.. however

here are a few things that may help

  $text = $status->text; //get XML text as a standard variable (for ease of use)
  
  //if $text contains "#fb" append "I" to it
  if(stripos($text, '#fb')) $text = "I ".$text;
  
  //if $text contains "#fb" replace with "I"
  $text = str_ireplace('#fb', 'I', $text);
  
  //Output
  print "<p><span class=\"twittertext\">$text</span><br />";

That seems like it'll be a good start.  The XML file in question lives at http://www.ryanean.es/crontwitter.xml ... here's a dump of it from today:

 

<?xml version="1.0" encoding="UTF-8"?> 
<statuses type="array"> 
<status> 
  <created_at>Mon Nov 22 17:05:32 +0000 2010</created_at> 
  <id>6755168032923648</id> 
  <text>It can be difficult to work when a cat that thinks it's a dog insists on sitting directly between you and your iPad.</text> 
  <source><a href="http://twitterrific.com" rel="nofollow">Twitterrific</a></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></retweet_count> 
  <retweeted>false</retweeted> 
  <user> 
    <id>14820502</id> 
    <name>Ryan Eanes</name> 
    <screen_name>eaners</screen_name> 
    <location>Brooklyn, NY</location> 
    <description>Eccentric, entertaining and critical, or your money back.</description> 
    <profile_image_url>http://a0.twimg.com/profile_images/1164214368/image_normal.jpg</profile_image_url> 
    <url>http://www.ryanean.es</url> 
    <protected>false</protected> 
    <followers_count>273</followers_count> 
    <profile_background_color>F34D13</profile_background_color> 
    <profile_text_color>000000</profile_text_color> 
    <profile_link_color>530606</profile_link_color> 
    <profile_sidebar_fill_color>E9E8C6</profile_sidebar_fill_color> 
    <profile_sidebar_border_color>000000</profile_sidebar_border_color> 
    <friends_count>231</friends_count> 
    <created_at>Sun May 18 13:17:52 +0000 2008</created_at> 
    <favourites_count>161</favourites_count> 
    <utc_offset>-18000</utc_offset> 
    <time_zone>Eastern Time (US & Canada)</time_zone> 
    <profile_background_image_url>http://s.twimg.com/a/1290023717/images/themes/theme1/bg.png</profile_background_image_url> 
    <profile_background_tile>false</profile_background_tile> 
    <profile_use_background_image>false</profile_use_background_image> 
    <notifications>false</notifications> 
    <geo_enabled>true</geo_enabled> 
    <verified>false</verified> 
    <following>true</following> 
    <statuses_count>4080</statuses_count> 
    <lang>en</lang> 
    <contributors_enabled>false</contributors_enabled> 
    <follow_request_sent>false</follow_request_sent> 
    <listed_count>13</listed_count> 
    <show_all_inline_media>false</show_all_inline_media> 
  </user> 
  <geo/> 
  <coordinates/> 
  <place/> 
  <contributors/> 
</status> 
<status> 
  <created_at>Mon Nov 22 00:00:23 +0000 2010</created_at> 
  <id>6497183310290944</id> 
  <text>Um, old guy in first class? You don't have to TIP THE GATE AGENT. It isn't going to make the in-flight experience any better. #whatthecrap</text> 
  <source><a href="http://twitter.com/" rel="nofollow">Twitter for iPhone</a></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></retweet_count> 
  <retweeted>false</retweeted> 
  <user> 
    <id>14820502</id> 
    <name>Ryan Eanes</name> 
    <screen_name>eaners</screen_name> 
    <location>Brooklyn, NY</location> 
    <description>Eccentric, entertaining and critical, or your money back.</description> 
    <profile_image_url>http://a0.twimg.com/profile_images/1164214368/image_normal.jpg</profile_image_url> 
    <url>http://www.ryanean.es</url> 
    <protected>false</protected> 
    <followers_count>274</followers_count> 
    <profile_background_color>F34D13</profile_background_color> 
    <profile_text_color>000000</profile_text_color> 
    <profile_link_color>530606</profile_link_color> 
    <profile_sidebar_fill_color>E9E8C6</profile_sidebar_fill_color> 
    <profile_sidebar_border_color>000000</profile_sidebar_border_color> 
    <friends_count>231</friends_count> 
    <created_at>Sun May 18 13:17:52 +0000 2008</created_at> 
    <favourites_count>161</favourites_count> 
    <utc_offset>-18000</utc_offset> 
    <time_zone>Eastern Time (US & Canada)</time_zone> 
    <profile_background_image_url>http://s.twimg.com/a/1290023717/images/themes/theme1/bg.png</profile_background_image_url> 
    <profile_background_tile>false</profile_background_tile> 
    <profile_use_background_image>false</profile_use_background_image> 
    <notifications>false</notifications> 
    <geo_enabled>true</geo_enabled> 
    <verified>false</verified> 
    <following>false</following> 
    <statuses_count>4079</statuses_count> 
    <lang>en</lang> 
    <contributors_enabled>false</contributors_enabled> 
    <follow_request_sent>false</follow_request_sent> 
    <listed_count>13</listed_count> 
    <show_all_inline_media>false</show_all_inline_media> 
  </user> 
  <geo/> 
  <coordinates/> 
  <place/> 
  <contributors/> 
</status> 
<status> 
  <created_at>Sun Nov 21 23:57:04 +0000 2010</created_at> 
  <id>6496347918176257</id> 
  <text>is wondering why the gate agent here at the airport is wearing what appears to be an ill-fitting lab coat. Utterly bizarre. #fb</text> 
  <source><a href="http://twitter.com/" rel="nofollow">Twitter for iPhone</a></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></retweet_count> 
  <retweeted>false</retweeted> 
  <user> 
    <id>14820502</id> 
    <name>Ryan Eanes</name> 
    <screen_name>eaners</screen_name> 
    <location>Brooklyn, NY</location> 
    <description>Eccentric, entertaining and critical, or your money back.</description> 
    <profile_image_url>http://a0.twimg.com/profile_images/1164214368/image_normal.jpg</profile_image_url> 
    <url>http://www.ryanean.es</url> 
    <protected>false</protected> 
    <followers_count>274</followers_count> 
    <profile_background_color>F34D13</profile_background_color> 
    <profile_text_color>000000</profile_text_color> 
    <profile_link_color>530606</profile_link_color> 
    <profile_sidebar_fill_color>E9E8C6</profile_sidebar_fill_color> 
    <profile_sidebar_border_color>000000</profile_sidebar_border_color> 
    <friends_count>231</friends_count> 
    <created_at>Sun May 18 13:17:52 +0000 2008</created_at> 
    <favourites_count>161</favourites_count> 
    <utc_offset>-18000</utc_offset> 
    <time_zone>Eastern Time (US & Canada)</time_zone> 
    <profile_background_image_url>http://s.twimg.com/a/1290023717/images/themes/theme1/bg.png</profile_background_image_url> 
    <profile_background_tile>false</profile_background_tile> 
    <profile_use_background_image>false</profile_use_background_image> 
    <notifications>false</notifications> 
    <geo_enabled>true</geo_enabled> 
    <verified>false</verified> 
    <following>false</following> 
    <statuses_count>4078</statuses_count> 
    <lang>en</lang> 
    <contributors_enabled>false</contributors_enabled> 
    <follow_request_sent>false</follow_request_sent> 
    <listed_count>13</listed_count> 
    <show_all_inline_media>false</show_all_inline_media> 
  </user> 
  <geo/> 
  <coordinates/> 
  <place/> 
  <contributors/> 
</status> 
<status> 
  <created_at>Sun Nov 21 23:10:33 +0000 2010</created_at> 
  <id>6484640038653952</id> 
  <text>has seen at least a half-dozen children under the age of five at the Continental President's Club today. IS NOTHING SACRED?! #fb</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></retweet_count> 
  <retweeted>false</retweeted> 
  <user> 
    <id>14820502</id> 
    <name>Ryan Eanes</name> 
    <screen_name>eaners</screen_name> 
    <location>Brooklyn, NY</location> 
    <description>Eccentric, entertaining and critical, or your money back.</description> 
    <profile_image_url>http://a0.twimg.com/profile_images/1164214368/image_normal.jpg</profile_image_url> 
    <url>http://www.ryanean.es</url> 
    <protected>false</protected> 
    <followers_count>274</followers_count> 
    <profile_background_color>F34D13</profile_background_color> 
    <profile_text_color>000000</profile_text_color> 
    <profile_link_color>530606</profile_link_color> 
    <profile_sidebar_fill_color>E9E8C6</profile_sidebar_fill_color> 
    <profile_sidebar_border_color>000000</profile_sidebar_border_color> 
    <friends_count>231</friends_count> 
    <created_at>Sun May 18 13:17:52 +0000 2008</created_at> 
    <favourites_count>161</favourites_count> 
    <utc_offset>-18000</utc_offset> 
    <time_zone>Eastern Time (US & Canada)</time_zone> 
    <profile_background_image_url>http://s.twimg.com/a/1290023717/images/themes/theme1/bg.png</profile_background_image_url> 
    <profile_background_tile>false</profile_background_tile> 
    <profile_use_background_image>false</profile_use_background_image> 
    <notifications>false</notifications> 
    <geo_enabled>true</geo_enabled> 
    <verified>false</verified> 
    <following>true</following> 
    <statuses_count>4077</statuses_count> 
    <lang>en</lang> 
    <contributors_enabled>false</contributors_enabled> 
    <follow_request_sent>false</follow_request_sent> 
    <listed_count>13</listed_count> 
    <show_all_inline_media>false</show_all_inline_media> 
  </user> 
  <geo/> 
  <coordinates/> 
  <place/> 
  <contributors/> 
</status> 
<status> 
  <created_at>Sun Nov 21 22:28:16 +0000 2010</created_at> 
  <id>6473998447673344</id> 
  <text>@jrpettet Same here. It scares me. Will have to hit the treadmill hard when I get back from Thanksgiving.</text> 
  <source>web</source> 
  <truncated>false</truncated> 
  <favorited>false</favorited> 
  <in_reply_to_status_id>6471299132039168</in_reply_to_status_id> 
  <in_reply_to_user_id>14825977</in_reply_to_user_id> 
  <in_reply_to_screen_name>jrpettet</in_reply_to_screen_name> 
  <retweet_count></retweet_count> 
  <retweeted>false</retweeted> 
  <user> 
    <id>14820502</id> 
    <name>Ryan Eanes</name> 
    <screen_name>eaners</screen_name> 
    <location>Brooklyn, NY</location> 
    <description>Eccentric, entertaining and critical, or your money back.</description> 
    <profile_image_url>http://a0.twimg.com/profile_images/1164214368/image_normal.jpg</profile_image_url> 
    <url>http://www.ryanean.es</url> 
    <protected>false</protected> 
    <followers_count>274</followers_count> 
    <profile_background_color>F34D13</profile_background_color> 
    <profile_text_color>000000</profile_text_color> 
    <profile_link_color>530606</profile_link_color> 
    <profile_sidebar_fill_color>E9E8C6</profile_sidebar_fill_color> 
    <profile_sidebar_border_color>000000</profile_sidebar_border_color> 
    <friends_count>231</friends_count> 
    <created_at>Sun May 18 13:17:52 +0000 2008</created_at> 
    <favourites_count>161</favourites_count> 
    <utc_offset>-18000</utc_offset> 
    <time_zone>Eastern Time (US & Canada)</time_zone> 
    <profile_background_image_url>http://s.twimg.com/a/1290023717/images/themes/theme1/bg.png</profile_background_image_url> 
    <profile_background_tile>false</profile_background_tile> 
    <profile_use_background_image>false</profile_use_background_image> 
    <notifications>false</notifications> 
    <geo_enabled>true</geo_enabled> 
    <verified>false</verified> 
    <following>true</following> 
    <statuses_count>4076</statuses_count> 
    <lang>en</lang> 
    <contributors_enabled>false</contributors_enabled> 
    <follow_request_sent>false</follow_request_sent> 
    <listed_count>13</listed_count> 
    <show_all_inline_media>false</show_all_inline_media> 
  </user> 
  <geo/> 
  <coordinates/> 
  <place/> 
  <contributors/> 
</status> 
<status> 
  <created_at>Sun Nov 21 22:16:07 +0000 2010</created_at> 
  <id>6470942507139072</id> 
  <text>Attention spans are dwindling & grades are declining, thanks to overstimulation from technology. I've seen it first-hand. http://is.gd/hyjj5</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></retweet_count> 
  <retweeted>false</retweeted> 
  <user> 
    <id>14820502</id> 
    <name>Ryan Eanes</name> 
    <screen_name>eaners</screen_name> 
    <location>Brooklyn, NY</location> 
    <description>Eccentric, entertaining and critical, or your money back.</description> 
    <profile_image_url>http://a0.twimg.com/profile_images/1164214368/image_normal.jpg</profile_image_url> 
    <url>http://www.ryanean.es</url> 
    <protected>false</protected> 
    <followers_count>274</followers_count> 
    <profile_background_color>F34D13</profile_background_color> 
    <profile_text_color>000000</profile_text_color> 
    <profile_link_color>530606</profile_link_color> 
    <profile_sidebar_fill_color>E9E8C6</profile_sidebar_fill_color> 
    <profile_sidebar_border_color>000000</profile_sidebar_border_color> 
    <friends_count>231</friends_count> 
    <created_at>Sun May 18 13:17:52 +0000 2008</created_at> 
    <favourites_count>161</favourites_count> 
    <utc_offset>-18000</utc_offset> 
    <time_zone>Eastern Time (US & Canada)</time_zone> 
    <profile_background_image_url>http://s.twimg.com/a/1290023717/images/themes/theme1/bg.png</profile_background_image_url> 
    <profile_background_tile>false</profile_background_tile> 
    <profile_use_background_image>false</profile_use_background_image> 
    <notifications>false</notifications> 
    <geo_enabled>true</geo_enabled> 
    <verified>false</verified> 
    <following>true</following> 
    <statuses_count>4075</statuses_count> 
    <lang>en</lang> 
    <contributors_enabled>false</contributors_enabled> 
    <follow_request_sent>false</follow_request_sent> 
    <listed_count>13</listed_count> 
    <show_all_inline_media>false</show_all_inline_media> 
  </user> 
  <geo/> 
  <coordinates/> 
  <place/> 
  <contributors/> 
</status> 
</statuses>

I'll work with my existing script a bit later to see if I can tweak it.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.