I recently started using PEAR’s Services_Twitter package (still in beta) written by Joe Stump (from Digg) and David Jean Louis. My natural choice would have been Zend Framework’s Zend_Service_Twitter, but I wasn’t aware that it was out of the incubator by the time I started.
Using and integrating the Services_Twitter package was very easy, and didn’t require much more than copying and pasting the code from the class documentation:
require_once 'Services/Twitter.php';
$username = 'You_Username';
$password = 'Your_Password';
try {
$twitter = new Services_Twitter($username, $password);
$msg = $twitter->statuses->update("I'm coding with PEAR right now!");
print_r($msg); // Should be a SimpleXMLElement structure
} catch (Services_Twitter_Exception $e) {
echo $e->getMessage();
}
However, once I requested the “source” option from twitter (that nifty thing that says where the tweet was sent via), things got complicated. Apparently, when the Twitter.php factory builds an instance of an API driver, it instantiates a new object, and doesn’t pass it the current factory options, so this didn’t work:
$twitter = new Services_Twitter($username, $password);
$twitter->setOptions('source','myapp');
$msg = $twitter->statuses->update("I'm coding with PEAR right now!");
print_r($msg); // Should be a SimpleXMLElement structure
The status was updated, but with no source. A short debug and look through the source revealed that factory behavior (not passing the current options to the newly instantiated statuses object). I don’t really see the rationale behind this, so I filed a bug report.
What did work, was instantiating a Twitter_Statuses object, and working with it directly, like this:
include_once "Services/Twitter/Statuses.php";
$twitter = new Services_Twitter_Statuses($username, $password);
$twitter->setOptions('source','myapp');
$msg = $twitter->update("I'm coding with PEAR right now!");
Now you can Tweet away with source!