Tuesday, April 13, 2010

Update Twitter With PHP

There are many API's to interact with Twitter using a variety of languages. I tried twitterlibphp by Justin Poliey, giving him a vote of confidence just for using git. Plus the documentation is clear. I was up and running within 10 minutes with only three lines of code! Thanks Justin!

This code will update my status:
<?
require_once("twitterlibphp/twitter.lib.php");
$twitter = new twitter("screen_name","password");
$xml = $twitter->updateStatus("test at " . time(),'556');
//$xml = $twitter->destroyStatus(12120224745);
echo $xml;
?>
The reply you'll get (by default) looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<status>
  <created_at>Tue Apr 13 20:05:23 +0000 2010</created_at>
  <id>12121728569</id>
  <text>test at 1271189121</text>
  <source>&lt;a href=&quot;http://apiwiki.twitter.com/&quot; rel=&quot;nofollow&quot;&gt;API&lt;/a&gt;</source>
  <truncated>false</truncated>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <favorited>false</favorited>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <user>
    <id>132618355</id>
    <name>UGA Status Page</name>
    <screen_name>uga_status</screen_name>
    <location></location>
    <description></description>
    <profile_image_url>http://s.twimg.com/a/1271107021/images/default_profile_2_normal.png</profile_image_url>
    <url></url>
    <protected>false</protected>
    <followers_count>0</followers_count>
    <profile_background_color>9ae4e8</profile_background_color>
    <profile_text_color>000000</profile_text_color>
    <profile_link_color>0000ff</profile_link_color>
    <profile_sidebar_fill_color>e0ff92</profile_sidebar_fill_color>
    <profile_sidebar_border_color>87bc44</profile_sidebar_border_color>
    <friends_count>0</friends_count>
    <created_at>Tue Apr 13 18:43:15 +0000 2010</created_at>
    <favourites_count>0</favourites_count>
    <utc_offset>-18000</utc_offset>
    <time_zone>Eastern Time (US &amp; Canada)</time_zone>
    <profile_background_image_url>http://s.twimg.com/a/1271107021/images/themes/theme1/bg.png</profile_background_image_url>
    <profile_background_tile>false</profile_background_tile>
    <notifications>false</notifications>
    <geo_enabled>false</geo_enabled>
    <verified>false</verified>
    <following>false</following>
    <statuses_count>1</statuses_count>
    <lang>en</lang>
    <contributors_enabled>false</contributors_enabled>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>

To delete that new status update, run:
<?
require_once("twitterlibphp/twitter.lib.php");
$twitter = new twitter("screen_name","pass");
$xml = $twitter->destroyStatus(12121728569);
echo $xml;
?>

The return XML looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<status>
  <created_at>Tue Apr 13 20:05:23 +0000 2010</created_at>
  <id>12121728569</id>
  <text>test at 1271189121</text>
  <source>&lt;a href=&quot;http://apiwiki.twitter.com/&quot; rel=&quot;nofollow&quot;&gt;API&lt;/a&gt;</source>
  <truncated>false</truncated>
  <in_reply_to_status_id></in_reply_to_status_id>
  <in_reply_to_user_id></in_reply_to_user_id>
  <favorited>false</favorited>
  <in_reply_to_screen_name></in_reply_to_screen_name>
  <user>
    <id>132618355</id>
    <name>UGA Status Page</name>
    <screen_name>uga_status</screen_name>
    <location></location>
    <description></description>
    <profile_image_url>http://s.twimg.com/a/1271107021/images/default_profile_2_normal.png</profile_image_url>
    <url></url>
    <protected>false</protected>
    <followers_count>0</followers_count>
    <profile_background_color>9ae4e8</profile_background_color>
    <profile_text_color>000000</profile_text_color>
    <profile_link_color>0000ff</profile_link_color>
    <profile_sidebar_fill_color>e0ff92</profile_sidebar_fill_color>
    <profile_sidebar_border_color>87bc44</profile_sidebar_border_color>
    <friends_count>0</friends_count>
    <created_at>Tue Apr 13 18:43:15 +0000 2010</created_at>
    <favourites_count>0</favourites_count>
    <utc_offset>-18000</utc_offset>
    <time_zone>Eastern Time (US &amp; Canada)</time_zone>
    <profile_background_image_url>http://s.twimg.com/a/1271107021/images/themes/theme1/bg.png</profile_background_image_url>
    <profile_background_tile>false</profile_background_tile>
    <notifications>false</notifications>
    <geo_enabled>false</geo_enabled>
    <verified>false</verified>
    <following>false</following>
    <statuses_count>0</statuses_count>
    <lang>en</lang>
    <contributors_enabled>false</contributors_enabled>
  </user>
  <geo/>
  <coordinates/>
  <place/>
  <contributors/>
</status>

No comments:

Post a Comment