Somewhere, Guildford, Surrey

PHP 4 Twitter Class

I say class, it’s actually just 1 function for PHP 4+ to allow sending of twitter messages really simply.

Based upon the PHP 5 twitter class by David Grudl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function twitter_send($message, $user, $pass)
	{
		$url = 'https://twitter.com/statuses/update.xml';
		$post = array('status' => $message);
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_USERPWD, $user.":".$pass);
		curl_setopt($curl, CURLOPT_HEADER, FALSE);
		curl_setopt($curl, CURLOPT_TIMEOUT, 20);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
 
		if ($post) {
			curl_setopt($curl, CURLOPT_POST, TRUE);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
		}
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // no echo, just return result
		$result = curl_exec($curl);
		$ok = curl_errno($curl) === 0 && curl_getinfo($curl, CURLINFO_HTTP_CODE) === 200;
		return $result;
	}

That’s it, and then just a call function to send a message:

twitter_send("Hello World!", "myusername","passwordhere");

Simple!

The function also returns XML from twitter, including data such as followers_count, current description, location, profile_image_url etc, if needed.

This entry was posted on Wednesday, January 28th, 2009 at 9:21 am and is filed under Code, Projects. 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 “PHP 4 Twitter Class”

  1. teeohhem says:

    Doesn’t seem to work any longer.

Leave a Reply