Twitter API returning Error: 34 (404) - php

I am trying to returning trending topics in the USA from the twitter API using TwitterAuth0 however I am getting, {"errors":[{"message":"Sorry, that page does not exist","code":34}]} as a result. My code is as following:
session_start();
require_once("twitteroauth-master/autoload.php"); //Path to twitteroauth library
use Abraham\TwitterOAuth\TwitterOAuth;
$id = "23424977";
$consumerkey = "XXX";
$consumersecret = "XXX";
$accesstoken = "XXX";
$accesstokensecret = "XXX";
function getToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/trends/place.json?id=".$id);
echo json_encode($tweets);
My actual credentials are in the keys/secrets, currently my callback url on twitter apps is the page of the feed I am returning, is this correct? (So the acutal page to see the feed)
Any help on this error is appreciated.

Figured out the issue:
Get piece needed to be:
$tweets = $connection->get("trends/place", [ "id" => 23424977 ]);
instead of:
$tweets = $connection->get("https://api.twitter.com/1.1/trends/place.json?id=".$id);

Related

Getting a list of tweets using a hashtag in PHP

I'm attempting to use Twitter's API, which to me seems hugely overcomplicated. I want to simply get a list of tweets that include a hashtag ('#test' for now), along with the username.
This is what I have so far using Abraham's TwitterOAuth:
<?php
$token = 'MY_TOKEN';
$token_secret = 'MY_TOKEN_SECRET';
$consumer_key = 'MY_CONSUMER_KEY';
$consumer_secret = 'MY_CONSUMER_SECRET';
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$connection = new TwitterOAuth( $consumer_key, $consumer_secret, $token, $token_secret );
$connection->host = "https://api.twitter.com/1.1/";
$connection->ssl_verifypeer = TRUE;
$connection->content_type = 'application/x-www-form-urlencoded';
$tweets = $connection->get('https://api.twitter.com/1.1/search/tweets.json?q=%23test&result_type=recent');
echo $tweets;
?>
This produces the error Object of class stdClass could not be converted to string in the echo $tweets; line.
How do I go about fixing this?
EDIT:
I have changed the code to $tweets = $connection->get('search/tweets', ["q" => "#test", "result_type" => "recent"]); and var_dump($tweets); which seems to work. Now I just have to figure out how to parse this.

Twitter API - Get Statuses not Finding Any User Page

The title pretty much sums it up: I can't find any pages using Get Statuses with the Twitter API.
Here's my code.
<?php
session_start();
require "abraham/twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$twitteruser = "IliaVED"; //User name I'm looking for
$id = "486654831"; //Corresponding id
$notweets = 30; //how many tweets you want to retrieve
$consumerkey = "xxx";
$consumersecret = "xxx";
$accesstoken = "xxx";
$accesstokensecret = "xxx";
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($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);
echo $tweets;
?>
I tried using the ID instead of the screen name and it still doesn't find it.
You can clearly see that the user exists:https://twitter.com/IliaVED
I tried with different users and it does the same thing..
This is the error I get:
{"errors":[{"message":"Sorry, that page does not exist","code":34}]}
You're missing = after screen_name in URL GET parameters part. Use:
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=" . $twitteruser . "&count=".$notweets);
EDIT: Actually, you are using the library in wrong way, try with:
$tweets = $connection->get("statuses/user_timeline", ["screen_name" => $twitteruser, "count" => $notweets]);
Library itself has base URL and adds it to the path you provide and handles array of URL GET parameters.

Twitter API only gets twitter JSON on local server

I've been using this script to get tweets using twitter OAUTH on a local version of my website. When I upload it to my live web server it doesn't work.
It returns a JSON prettified list of twitter data based on a string that I provide.
Could anyone tell me why it isn't returning the JSON response on the live server?
<?php
session_start();
require_once("twitteroauth-master/twitteroauth/twitteroauth.php"); //Path to twitteroauth library
$notweets = 6;
$consumerkey = "**************************";
$consumersecret = "**************************";
$accesstoken = "**************************";
$accesstokensecret = "**************************";
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
return $connection;
}
$search_term = urlencode("pizza");
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=$search_term&count=$notweets&result_type=recent&include_rts=false");
header('Content-Type: application/json');
echo json_encode($tweets, JSON_PRETTY_PRINT);
?>
SOLVED
I updated my php.ini file to display errors and was given the following message.
Use of undefined constant JSON_PRETTY_PRINT
I was then able to solve my problem. Thanks to all who commented and I learnt how to track down errors appropriately in a live PHP / server environment.

Abraham Twitter API 1.1 Oauth library only returning "null"

I've been trying to get the latest status back from a user's twitter feed using Abraham Williams' Twitter Oauth Library (https://github.com/abraham/twitteroauth) I followed the tutorial found at http://www.webdevdoor.com/php/authenticating-twitter-feed-timeline-oauth/ and created the get_tweet.php file as my index. When I run this on my website an all white page, with "null" in the top left corner is displayed.
As far as I am to understand my Oauth keys I have are valid, I am using 000webhost.com to host my website, my webserver is using PHP 5.2.17 and cURL is enabled, From the tutorial and sample files I have been using my index file should be correct, my website can be found at http://authortryndaadair.site90.net where the results of this call is being dispayed.
I have been able to troubleshoot a small amount, but I am unsure of what else could try to get this Api call working. Any help in solving this problem would be much appreciated.
Below is the contents of the index file substituting for get_tweet1.1.php:
session_start();
// Path to twitteroauth library
require_once("twitteroauth/twitteroauth/twitteroauth.php");
$twitteruser = "JaneSmith";
$notweets = 10;
$consumerkey = "123456";
$consumersecret = "789123";
$accesstoken = "456789";
$accesstokensecret = "1023456";
function getConnectionWithAccessToken($cons_key, $cons_secret,
$oauth_token, $oauth_token_secret) {
$connection = new TwitterOAuth($cons_key, $cons_secret,
$oauth_token, $oauth_token_secret);
return $connection;
}
$connection = getConnectionWithAccessToken($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);
Assuming you are running the latest TwitterOAuth code the get line should look like this.
$tweets = $connection->get(
"statuses/user_timeline",
array("screen_name" => $twitteruser, "count" => $notweets)
);

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));
?>
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.
Just use the example:
$connection->post('statuses/update', array('status' =>$message));
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/
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

Categories