how to post tweet using twitter 1.1 api and twitteroauth
Asked Answered
R

4

7

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));
?>
Raila answered 12/6, 2013 at 11:54 Comment(0)
T
15

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.

Tribasic answered 31/7, 2013 at 3:46 Comment(0)
N
5

Just use the example:

$connection->post('statuses/update', array('status' =>$message));
Napier answered 5/9, 2013 at 10:38 Comment(2)
Do we get tweet id and time as a response?Idiocrasy
I want to pass another 'url' parameter with 'status' is it possibleRoybn
S
1

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/

Sextillion answered 15/10, 2014 at 7:53 Comment(0)
T
-2

The issue is about enconding the value need to be enconde :

Example

$status = $connection->post('https://api.twitter.com/1.1/statuses/update.json', array('status' => rawurlencode($message)));

If you check in the library recomended https://github.com/J7mbo/twitter-api-php

That the way they encode the params

Triforium answered 23/9, 2016 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.