Twitter API only gets twitter JSON on local server - php

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.

Related

Twitter API returning Error: 34 (404)

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);

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 APIs not working on hosting [Works fine on localhost]

I am working on an app to show the tweets done in 1 mile radius of given geolocation (Latitude and Longitude). This is my PHP code,
<?php
// $lat = $_GET['lat'];
$lat = 26.511740;
// $long = $_GET['long'];
$long = 80.234973;
require_once("twitteroauth/twitteroauth.php"); //Path to twitteroauth library
$notweets = 100;
$consumerkey = "XXXX";
$consumersecret = "XXXX";
$accesstoken = "XXXX-XXXX";
$accesstokensecret = "XXXX";
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/search/tweets.json?geocode=".$lat.",".$long.",5mi&result_type=recent&count=".$notweets);
// echo $tweets;
echo json_encode($tweets);
?>
I am using Wamp server (PHP V5.5.12) and my code is working fine on it. But when I host my application on some free hosting sites (I have tried hostinger.in and 000webhost.com), this script fails and just prints 'null'.
Please help me to solve this problem.
Thanks in advance.
I've tried in both, hostinger and 000webhost and several others. The reason they dont work is the library to connect to twitter uses php curl, and many free hosting have disabled curl, or outgoing connections or twitter refuses the curl connection when it comes from the ips from free hosting servers. For what I've read in internet it's probably because many hackers have been messing with twitter and hosting from free hosting accounts. So finding a free hosting with cpanel that works with Twitter API it's a challenge, i've tried over 20 and they dont work, some of them automatically deleted the account or the file or block the ftp access if you try to curl to twitter
Likely to be related to the libraries available to you.
Add error_reporting(E_ALL) to the top of the script.
Check that cURL is installed on the cheap hosting as I believe that's the only php library that twitteroauth requires.
What is your TwitterOAuth version? Your code seems to be so legacy and not compatible with latest version.
https://twitteroauth.com/
<?php
require "vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$lat = 26.511740;
$long = 80.234973;
$notweets = 100;
$ck = "XXXX";
$cs = "XXXX";
$ot = "XXXX-XXXX";
$os = "XXXX";
$to = new TwitterOAuth($ck, $cs, $ot, $os);
$tweets = $to->get('search/tweets', [
'geocode' => "$lat,$long",
'result_type' => 'recent',
'count' => $notweets,
]);
if (isset($tweets->errors[0]->message)) {
echo 'Error: ' . $tweets->errors[0]->message;
} elseif (!is_array($tweets)) {
echo 'Unknown Error';
} else {
echo '<pre>';
var_dump($tweets);
echo '</pre>';
}
Alternatively, you can use TwistOAuth instead of TwitterOAuth. I'm the author of this library. This library is almost compabible with TwitterOAuth, but support strict exception handling. Error reasons are always to be clear.
https://github.com/mpyw/TwistOAuth
<?php
require 'TwistOAuth.phar'; // Or 'vendor/autoload.php' for composer
$lat = 26.511740;
$long = 80.234973;
$notweets = 100;
$ck = "XXXX";
$cs = "XXXX";
$ot = "XXXX-XXXX";
$os = "XXXX";
try {
$to = new TwistOAuth($ck, $cs, $ot, $os);
$tweets = $to->get('search/tweets', [
'geocode' => "$lat,$long",
'result_type' => 'recent',
'count' => $notweets,
]);
echo '<pre>';
var_dump($tweets);
echo '</pre>';
} catch (TwistException $e) {
echo 'Error: ' . $e->getMessage();
}
Which code do you like?

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