Search code examples
phptwittertwitter-oauth

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.


Solution

  • 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