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;
}
}
?>
A genius! Thank you so much for it. It’s now a part of my website.
[...] 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. [...]
How to use the Consumer Key and Consumer Secret codes to let appear “from Vareside.com” below every tweet posted via Vareside.com?
@Max have a read of this.. http://www.codingthewheel.com/archives/codingthetweet