Posting to Twitter using PHP
I haven’t had much to post about lately so I’ll just share a simple bit of code to update your Twitter status and shrink any urls in the post. Maybe someone will find it useful.
I downloaded a small Twitter class from here but the download link doesnt seem to work anymore so leave a comment if you want the script. There are also loads of other free PHP twitter scripts so just do a search.
The site I was building just needed to post the name and url of an item to twitter. This bit of code will first shrink the URL using TinyURL’s api, then post to twitter.
Note: I wrote this for a Symfony 1.0 project. I haven’t used sfWebBrowser in 1.2 so im not sure if it will work, but you could replace it with cURL or even just file_get_contents()
$curTwitter = new twitter("USERNAME", "PASSWORD"); $b = new sfWebBrowser(); $my_url = 'http://www.YOUR-DOMAIN.com/'; $b->get('http://tinyurl.com/api-create.php?url='.$my_url); $tinyurl = $b->getResponseText(); $curTwitter->setStatus($this->getRequestParameter('name'). ' ' . $tinyurl );
Update: Forgot to mention, sfWebBrowser is a plugin for anyone who didnt know.
Follow me on twitter



