Twitter Programing Example
I cut my Twitter programming teeth by writing FeedTwit- a program that can send Twitter replies as text messages. Now, I am helping write a book on programming with the Twitter API.
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:
- Is this a good example illustrating how to use the Twitter API in retrieving different kinds of messages?
- Are the remarks clear and helpful?
- Is this example simple yet somewhat usable?
- Any other comments and suggestions are welcome
Just leave your comments below…
Comments (2)
dizzysoft software

Step one: never use tables :)
MIA CULPA!
I repent of my use of tables on this example page!