Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to post tweet using twitter 1.1 api and twitteroauth

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));
?>
like image 259
Susheel Singh Avatar asked Jun 12 '13 11:06

Susheel Singh


People also ask

Can I tweet using Twitter API?

First, log in to the Twitter developer's portal with the account from which you want to post a tweet. Then click on the "Developer Portal" link in the upper right corner. Once you enter it, click on "Projects & Apps" -> "Overview" -> "Create App".

Can I still use Twitter API v1 1?

The v1. 1 search/tweets and the Twitter API v2 recent search endpoint support both OAuth 1.0a User Context and OAuth 2.0 App-Only. Therefore, if you were previously using the standard v1. 1 search endpoint you can continue using the same authentication method if you migrate to the Twitter API v2 version.

How do I tweet on tweetbot?

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.


2 Answers

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.

like image 117
ideonexus Avatar answered Oct 12 '22 12:10

ideonexus


Just use the example:

$connection->post('statuses/update', array('status' =>$message));
like image 20
Noelchan Avatar answered Oct 12 '22 11:10

Noelchan