PHP 4 Twitter Class

This now no longer works, due to the introduction of oAuth being a requirement for external messages sent to twitter

PHP Twitter oAuth

Old content below:

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

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.

4 Comments to “PHP 4 Twitter Class”

  1. teeohhem says:

    Doesn’t seem to work any longer.

  2. teeohhem,
    Alas the Lords of Twitter have deemed it unsuitable for their empire to exist with the simplicity of basic authentication and have forced one and all to go down the often problematic route of OAuth. Depending on the project it may be entirely less useful to use OAuth when you can just link the user to a prefilled twitter post page.
    Linking to http://twitter.com/home?status=I+quite+like+pretty+goats (or whatever) would invite the clicker to send a message revealing their innermost thoughts about those of a goaty persuasion. If you want anything any more complex than that it’s OAuth for you I’m afraid me laddo.

  3. Joel says:

    Thanks for the reply Simon, yup better edit this post now to let people know…

Leave a Reply

Message