skip to Main Content

I’m trying to post tweets with the Twitter API v2 but have an error. I’m using the TwitterAPIExchange.php library, this works fine when I’m trying to search for Tweets for example with GET. But no luck with POST, see code below also the Manage Tweets doc from Twitter: https://developer.twitter.com/en/docs/twitter-api/tweets/manage-tweets/api-reference/post-tweets

Maybe I’m missing some parameters after the end point.

header('Content-Type:application/json');

include ($_SERVER["DOCUMENT_ROOT"] . "/TwitterAPIExchange.php");
 
$settings = array(
    'oauth_access_token' => "xxxx",
    'oauth_access_token_secret' => "xxxx",
    'consumer_key' => "xxxx",
    'consumer_secret' => "xxxx"
    
);

$url = "https://api.twitter.com/2/tweets";
$requestMethod = 'POST';

$postfields = array('status' => 'This is the text to Tweet');

$twitter = new TwitterAPIExchange($settings);
$response = $twitter->buildOauth($url, $requestMethod)->setPostfields($postfields)->performRequest();

Errors:

{
errors: [
{
parameters: { },
message: "Requests with bodies must have content-type of application/json."
}
],
title: "Invalid Request",
detail: "One or more parameters to your request was invalid.",
type: "https://api.twitter.com/2/problems/invalid-request"
}

2

Answers


  1. Chosen as BEST ANSWER

    I've used the AbrahamTwitterOAuth library instead and was able to send a tweet from the tutorial on this page: https://artisansweb.net/tweet-twitter-php/


  2. You may try to use this, it is simple to configure
    https://github.com/hermanbenedict/Post-to-Twitter

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search