How to send a tweet using twitter 1.1 api and twitteroauth

I use the code below to get my tweets and echo json. It works great.

<?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 a similar code, but it does not work. I am not sure if the send syntax is correct. so please 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)); ?> 
+6
source share
4 answers

I used twitteroauth.php to post tweets when the new API broke it. You are using $connection->post correctly, but it seems the function no longer works. The easiest solution I found is to share twitteroauth.php files with J7mbo twitter-api-php for the new 1.1 API:

https://github.com/J7mbo/twitter-api-php

Here are his step-by-step instructions for using it. I think you will be pleasantly surprised to find that you can leave most of your code the same, just switch twitteroauth calls with its function calls in the appropriate places:

Simplest PHP example to retrieve user_timeline with Twitter API 1.1

It does not provide a specific example of tweet posting, but here is what you need for this 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 do not need to provide your username / password. An authentication token will handle everything.

+14
source

Just use an example:

 $connection->post('statuses/update', array('status' =>$message)); 
+5
source

Try it, you don’t need a username / password that you can publish through the API key, read the tutorial here.

http://designspicy.com/learn-how-to-post-on-twitter-using-php-and-oauth/

+1
source

The problem is that you need to add the encoding value:

Example

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

If you remember the library recommended by https://github.com/J7mbo/twitter-api-php

The way they encode parameters

-2
source

Source: https://habr.com/ru/post/947108/


All Articles