Here's how to twitter with your own PHP applications!
To start, we need Curl installed on your server. Curl is a PHP library that allows communication via http, https, ftp, and many others.
Then you need to get the script was written Gareth Rushgrove, available here :
/ / Set username and password
; $ Username = 'username';
; $ Password = 'password';
/ / The message you want to send
; $ Message = 'is twittering from php using curl';
/ / The twitter API address
; $ Url = 'http://twitter.com/statuses/update.xml';
/ / Alternative JSON version
/ / $ Url = 'http://twitter.com/statuses/update.json';
/ / Set up and execute the curl process
; $ Curl_handle = curl_init ();
, CURLOPT_URL, "$url" ) ; curl_setopt ($ curl_handle, CURLOPT_URL, "$ url");
, CURLOPT_CONNECTTIMEOUT, 2 ) ; curl_setopt ($ curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
, CURLOPT_RETURNTRANSFER, 1 ) ; curl_setopt ($ curl_handle, CURLOPT_RETURNTRANSFER, 1);
, CURLOPT_POST, 1 ) ; curl_setopt ($ curl_handle, CURLOPT_POST, 1);
, CURLOPT_POSTFIELDS, "status=$message" ) ; curl_setopt ($ curl_handle, CURLOPT_POSTFIELDS "status = $ message");
, CURLOPT_USERPWD, "$username:$password" ) ; curl_setopt ($ curl_handle, CURLOPT_USERPWD, "$ username: $ password");
$curl_handle ) ; $ Buffer = curl_exec ($ curl_handle);
) ; curl_close ($ curl_handle);
/ / Check for success or failure
empty ( $buffer ) ) { if ( empty ($ buffer)) {
; echo 'message';
{ Else {}
; echo 'success';
}
?>
As you can see, it is very simple and easy to use PHP in any application as long as your host (if you're not on a dedicated server) allows you to use Curl.
Now, thanks to the documentation of Curl, disponnible here , we can customize our application twitter.
We define a function "twitter", which can be reused wherever you need it:
{
; $ Username = 'login';
; $ Password = 'password';
; $ Url = 'http://twitter.com/statuses/update.xml';
; $ Curl_handle = curl_init ();
, CURLOPT_URL, "$url" ) ; curl_setopt ($ curl_handle, CURLOPT_URL, "$ url");
, CURLOPT_CONNECTTIMEOUT, 2 ) ; curl_setopt ($ curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
, CURLOPT_RETURNTRANSFER, 1 ) ; curl_setopt ($ curl_handle, CURLOPT_RETURNTRANSFER, 1);
, CURLOPT_POST, 1 ) ; curl_setopt ($ curl_handle, CURLOPT_POST, 1);
, CURLOPT_POSTFIELDS, "status=$message" ) ; curl_setopt ($ curl_handle, CURLOPT_POSTFIELDS "status = $ message");
, CURLOPT_USERPWD, "$username:$password" ) ; curl_setopt ($ curl_handle, CURLOPT_USERPWD, "$ username: $ password");
$curl_handle ) ; $ Buffer = curl_exec ($ curl_handle);
) ; curl_close ($ curl_handle);
empty ( $buffer ) ) if ( empty ($ buffer))
; return 0;
else
; return 1;
}
Thus, it is easy to twitter any content just with the following line:
/ / Or
"le message" ) ; $ Res = twitter ("message");
$ Res variable is optional, but lets you know if everything went well ($ res = 1 if message, 0 otherwise). Then it remains for you to show a little imagination to:
- be informed in real time if the comments are posted to your blog,
- know the number of daily visitors to your site,
- you to send SMS your schedule,
- ...
Finally the last point, you may not want all the world can read the contents of your "twits" just think twitter configure private mode, menu settings and select Protect my updates.
Ps: If you wish to make an application such as time-use SMS and if you do not have your own server, you will not need a cron to run the script at regular intervals. You can find a free here and there .
Pps: I do not hesitate to share your creations. ;-)
Edit: Here is the documentation of the twitter API . To perform an action, use the correct url and the correct settings. For example, to delete a tweet, use the following URL http://twitter.com/statuses/destroy/ id. Xml (or id is the identifier of Twitter) and not http://twitter.com/ statuses / update.xml. Then you have to play with the following line to enter the settings according to the action (in my example there is no need):
Little more, here you will find PHP libraries ready-made. Here, with this you can do anything with twitter, all in PHP!





















10 users commented on this post
A little research:
PHP + Twitter + Google Calendar + SMS = http://dotjay.co.uk/2008/feb/p.....lendar-sms
Excellent topic
Thank you to you! And I would also like to thank you for having relayed the tip to the RSS vacuum.
;-)
Thank you for this interesting article:) Cute c on twitter:)
Thank you for the scripts, I test it tonight
@ +
I can not run the script when I replace username and password by a string from input text, why?
Hi, I 'fixed' a bit by adding a script utf8_encode () on the message as an accent 'ate' the following letters
$ Message = 'Tweet sent from XXX';
$ Username = 'qwerty';
$ Password = '123456 ';
$ Url = 'http://twitter.com/statuses/update.xml';
/ / $ Url = 'http://twitter.com/statuses/update.json';
$ 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 ='. utf8_encode ($ message));
curl_setopt ($ curl_handle, CURLOPT_USERPWD, $ username .':'.$ password);
$ Buffer = curl_exec ($ curl_handle);
curl_close ($ curl_handle);
echo (empty ($ buffer))? 0: 1;
(It is ridiculous to delete the contents of tags, an htmlentities () is enough)
Thank you, it's rated! ;-)
Pingback and Trackback
Leave a Comment