Integrate Twitter using PHP
Chris Missal, August 09, 2008
Last night I signed up for a Twitter account. While browsing through the site and trying to figure out where everything was, I clicked the API link and just thought: "Huh, that could be fun."
When I woke up this morning I decided to see if I could quickly add my Twitter status to my blog. It was super easy, check it out:
<?php
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://twitter.com/statuses/user_timeline/dumpsterdoggy.xml');
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
if (empty($buffer)) { ?>
<ul>
<li>Twitter status unavailable.</li>
</ul>
<?php
} else {
$dom = new DOMDocument;
$dom->loadXML($buffer);
if(!$dom) { ?>
<ul>
<li>Twitter status unavailable.</li>
</ul>
<?php
} else {
$s = simplexml_import_dom($dom);
?>
<ul>
<?php
foreach($s->xpath('//status') as $status) {
echo " <li><a href=\"http://twitter.com/DumpsterDoggy/statuses/$status->id\">$status->text</a></li>\n";
}
?>
</ul>
<?php
}
}
?>
I didn't search for a solution to do this. I just kind of came up with my own. If you know of any other ways, possibly and quite probably easier, I'd be happy if you posted them here.
Filed Under: PHP













