Twitter Programing Example

I cut my Twitter programming teeth by writing FeedTwit- a program that can send Twitter RSS feeds as direct messages and text messages.

Of course, I’d be happy to do some Twitter programming for you as well.

Meanwhile, I’d love your input on this simple example. Try it out yourself and tell me what you think:

<?php
function curlRequest($url, $post) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
// this is not the recommended way to login- use OAUTH instead
curl_setopt($curl, CURLOPT_USERPWD, "username:password");
curl_setopt($curl, CURLOPT_HEADER, FALSE);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
// we are not using the $post variable yet, but stay tuned!
if ($post) {
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
}
return curl_exec($curl);
}

?>
<html>
<body>
<h1>Getting Twitter Account Messages</h1>
<table width=”100%”>
<tr>
<th width=”33%”>Updates</th>
<th width=”33%”>Replies</th>
<th width=”33%”>DMs</th>
</tr>
<tr>
<td>
<?php
// change “user_timeline” to “friends_timeline” to see the people followed by this user
// add “?count=200″ to the end of this URL to get up to 200 statuses at a time (the most you can get at once)
// you don’t have to get the information by XML, but that is simplest to work with
$contents= curlRequest(“http://twitter.com/statuses/user_timeline.xml”, FALSE);
$xml = new SimpleXMLElement($contents);
// view the contents of this XML to see what other information you can retrieve besides just the text of the status
?>
<ul>
<?php
foreach($xml->status as $status) {
echo “<li>$status->text</li>”;
}
?>
</ul>
</td>
<td>
<?php
// add “?count=200″ to the end of this URL to get up to 200 statuses at a time (the most you can get at once)
$contents= curlRequest(“http://twitter.com/statuses/mentions.xml”, FALSE);
$xml= new SimpleXMLElement($contents);
?>
<ul>
<?php
foreach($xml->status as $status) {
echo “<li>$status->text</li>”;
}
?>
</ul>
</td>
<td>
<?php
// add “?count=200″ to the end of this URL to get up to 200 statuses at a time (the most you can get at once)
$contents= curlRequest(“http://twitter.com/direct_messages.xml”, FALSE);
$xml= new SimpleXMLElement($contents);
?>
<ul>
<?php
// notice: we are no longer getting the “status” but the “direct_message”
foreach($xml->direct_message as $message) {
echo “<li>$message->text</li>”;
}
?>
</ul>
</td>
</tr>
</table>
</body>
</html>

Here are a couple questions I am particularly interested in:

  1. Is this a good example illustrating how to use the Twitter API in retrieving different kinds of messages?
  2. Are the remarks clear and helpful?
  3. Is this example simple yet somewhat usable?
  4. Any other comments and suggestions are welcome

Just leave your comments below…

This entry was posted on Tuesday, November 10th, 2009 at 8:25 pm and is filed under Twitter. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “Twitter Programing Example”

  1. Andrew Mager Says:

    Step one: never use tables :)

  2. the zim Says:

    MIA CULPA!

    I repent of my use of tables on this example page!

Leave a Reply