how to post tweet using twitter 1.1 api and twitteroauth
Solution 1
I was using twitteroauth.php to post tweets myself when the new API broke it. You are using the $connection->post
correctly, but it appears that function does not work anymore. The easiest solution I found was to swap out the twitteroauth.php with J7mbo's twitter-api-php file for the new 1.1 API:
https://github.com/J7mbo/twitter-api-php
Here's his step-by-step instructions for using it. I think you'll be pleasantly surprised to find you can leave most of your code the same, just switch the twitteroauth calls with his function calls at the appropriate places:
Simplest PHP example for retrieving user_timeline with Twitter API version 1.1
He doesn't provide the specific example of posting a tweet, but here's what what you need for that function:
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';
$postfields = array(
'status' => 'Hi how are you' );
echo $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
With the new twitter API, you won't need to provide your username/password. The authentication token will handle everything.
Solution 2
Just use the example:
$connection->post('statuses/update', array('status' =>$message));
Solution 3
Try it you won't need to username/password you can post via API key read the tutorial here.
http://designspicy.com/learn-how-to-post-on-twitter-using-php-and-oauth/
Susheel Singh
I'm 32 year old *AEM/full Stack web developer. I Love to share knowledge & learn new things from others. Presently I am good at Java, servlet, JSP, struts, HP Teamsite/Livesite, Adobe Experience Manager, Perl, PHP, Xslt, Html, Css, Javascript, Jquery, Angular JS, Grunt js Wishlist: Java frameworks, KnockOut js, Node.js, backbone js, react js etc. Email: [email protected] Facebook: facebbook LinkedIn: linkedIn
Updated on November 27, 2020Comments
-
Susheel Singh over 3 years
I use the below code to retrieve my tweets and echo json. This works fine.
<?php session_start(); require_once('includes/twitter/twitteroauth.php'); $twitteruser = "xxxxxx"; $notweets = 3; $consumerkey = "xxxxxxx"; $consumersecret = "xxxxxx"; $accesstoken = "xxxxxxxx"; $accesstokensecret = "xxxxxx"; $connection = new TwitterOAuth($consumerkey, $consumersecret, $accesstoken, $accesstokensecret); $tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets); echo json_encode($tweets); ?>
Now i want to send a tweet using the similar code but it doesnot work. I am not sure if the sending syntax is correct. so please someone help me.
<?php session_start(); require_once('includes/twitter/twitteroauth.php'); //Path to twitteroauth library $twitteruser = "xxxxxx"; $notweets = 3; $consumerkey = "xxxxxxx"; $consumersecret = "xxxxxx"; $accesstoken = "xxxxxxxx"; $accesstokensecret = "xxxxxx"; // start connection $connection = new TwitterOAuth($consumerkey, $consumersecret, $accesstoken, $accesstokensecret); //message $message = "Hi how are you"; //send message $status = $connection->post('https://api.twitter.com/1.1/statuses/update.json', array('status' => $message)); ?>