There are cases you need to update a special Twitter feed to inform your visitors about something that has happened to your site. Then, this PHP function might be very useful if you add it in your functions arsenal:
/* inform twitter about something */As you can see, it only takes a string argument; the message you want to send. It returns also a string, which is either a true/false value or an XML-formatted response, depending on the value of CURLOPT_RETURNTRANSFER.
function twit( $msg = "")
{
$username = "twitter-username";
$password = "twitter-password";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "http://twitter.com/statuses/update.xml");
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE); // change to FALSE for not reporting XML output
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_POSTFIELDS, "status=" . urlencode( $msg));
curl_setopt( $ch, CURLOPT_USERPWD, "$username:$password");
ob_start();
$response = curl_exec( $ch);
ob_end_clean();
curl_close( $ch);
return $response;
}
In order to use this function, you need to have installed the cURL extension of PHP. You can check if it's installed quickly with the following command:
$ php -i | grep -i curl
If cURL is installed, you can expect an output like this:
additional .ini files parsed => /etc/php5/cli/conf.d/curl.ini,
curl
cURL support => enabled
cURL Information => libcurl/7.16.4 OpenSSL/0.9.8e zlib/1.2.3.3 libidn/1.0
If not, the package you need to install is php5-curl.
0 comments:
Post a Comment