Mark Dalby

Fighting Ignorance since 1986 (It’s taking longer than I thought).

Advertisement

The below function will allow you to “Twit” to Twitter from your website. Your server must have CURL support for this to work.
The function also limits the content to 140 chars and appends  “…” to strings that are longer.
<?php
# @purpose                             To post content to twitter
# @param        $content        The content to post to twitter. (140 Chars Maximum)
# @param        $username     Your Twitter username.
# @param        $password      Your Twitter password.
# @returns      True or False
# @changelog    14 / 03 / 2009    Function declared complete.
# @docend
function twit($content, $username, $password)
{
// The message you want to send.
$message = substr($content, 0, strrpos(substr($content, 0, 140), ‘ ‘)) . ‘…’;
// The twitter API address
$url = ‘http://twitter.com/statuses/update.xml’;
// Execute the query.
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, “$url”);
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, “status=$message”);
curl_setopt($curl_handle, CURLOPT_USERPWD, “$username:$password”);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);
// check for success or failure
if (empty($buffer)) {
return false;
} else {
return true;
}
}
?>

The below function will allow you to “Twit” to Twitter from your website. Your server must have CURL support for this to work.

The function also limits the content to 140 chars and appends  “…” to strings that are longer.

<?php

# @purpose                             To post content to twitter

# @param        $content        The content to post to twitter. (140 Chars Maximum)

# @param        $username     Your Twitter username.

# @param        $password      Your Twitter password.

# @returns      True or False

# @changelog    14 / 03 / 2009    Function declared complete.

# @docend

function twit($content, $username, $password)

{

// The message you want to send.

$message = substr($content, 0, strrpos(substr($content, 0, 140), ‘ ‘)) . ‘…’;

// The twitter API address

$url = ‘http://twitter.com/statuses/update.xml’;

// Execute the query.

$curl_handle = curl_init();

curl_setopt($curl_handle, CURLOPT_URL, “$url”);

curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);

curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($curl_handle, CURLOPT_POST, 1);

curl_setopt($curl_handle, CURLOPT_POSTFIELDS, “status=$message”);

curl_setopt($curl_handle, CURLOPT_USERPWD, “$username:$password”);

$buffer = curl_exec($curl_handle);

curl_close($curl_handle);

// check for success or failure

if (empty($buffer)) {

return false;

} else {

return true;

}

}

?>

Comments

There are 4 comments for this post.

  1. Max on October 4, 2009 12:40 pm

    A genius! Thank you so much for it. It’s now a part of my website.

  2. Mark Dalby Featured Uncategorized PHP Post to Twitter and append content to 140 chars. » Auto Post Script on October 5, 2009 11:35 pm

    [...] Just another WordPress weblog The below function will allow you to “Twit” to Twitter from your website. Your server must have CURL support for this to work. The function also limits the content to 140 chars and appends “…” to strings that are longer. <?php # @purpose To post content to twitter # @param $content The content to post to twitter. (140 Chars Maximum) # @param $username Your Twitter username. # @param $password Your Twitter password. # @returns True or False # @changelog 14 / 03 / 2009 Function declared complete. # @docend function twit($content, $username, $password) { // The message you want to send. $message = substr($content, 0, strrpos(substr($content, 0, 140), ‘ ‘)) . ‘…’; // The twitter API address $url = ‘http://twitter.com/statuses/update.xml’; // Execute the query. $curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, “$url”); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_handle, CURLOPT_POST, 1); curl_setopt($curl_handle, CURLOPT_POSTFIELDS, “status=$message”); curl_setopt($curl_handle, CURLOPT_USERPWD, “$username:$password”); $buffer = curl_exec($curl_handle); curl_close($curl_handle); // check for success or failure if (empty($buffer)) { return false; } else { return true; } } ?> There is one comment for this post. A genius! Thank you so much for it. It’s now a part of my website. Sign up to receive latest news Download Free Article Spinner Thanks. [...]

  3. Max on October 14, 2009 6:52 pm

    How to use the Consumer Key and Consumer Secret codes to let appear “from Vareside.com” below every tweet posted via Vareside.com?

  4. admin on October 15, 2009 9:04 am

Write a Comment