Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Tweet in Twitter using PHP

Tags:

php

twitter

How can I tweet in twitter from my website? I am using a PHP script. Whatever tweets I send from my website should update my twitter account. I use the following code, but it is not updating in my twitter account:

// Set username and password
$username='myusername';
$password='*********';
// The message you want to send
$message = 'Nice to c all again.Have a nice day..';
// 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();
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)) {
    echo 'Try again';
} else {
    echo 'success';
}

This script is returning a success message, but when I check my twitter account no tweets are found.

What could be the problem?

like image 224
Senthil Avatar asked Jan 12 '11 05:01

Senthil


People also ask

How do you tweet a website on Twitter?

Type your Tweet (up to 280 characters) into the compose box at the top of your Home timeline, or select the Tweet button in the navigation bar. You can include up to 4 photos, a GIF, or a video in your Tweet. Select the Tweet button to post the Tweet to your profile.

Can I tweet using Twitter API?

With this API, you can: Retweet other tweets. Retweet and edit tweets.

Is Twitter REST API free?

Essential. With Essential access, you can now get access to Twitter API v2 quickly and for free!


1 Answers

You are trying to send tweets using Basic Authentication (user name and password). This is no longer allowed. There are many examples of this online, but Twitter turned it off last August. You now have to use OAuth to do authentication.

like image 66
Adam Green Avatar answered Sep 16 '22 11:09

Adam Green