Concatenating twitter api get request while using variables - php

I have a lat and long saved like this,
$lat_value = $_REQUEST['latitude'];
$lon_value = $_REQUEST['longitude'];
When adding the variables to the $getfield String like this,
$getfield = '?q=test&geocode='.$lat_value.','.$lon_value.',1mi&count=100';
I keep getting this error,
"You must provide valid coordinates, IP address, query, or attributes." }
If it's a syntax error could someone please explain why this does not work to me.
Full request and Response code,
<?php
error_reporting(E_ALL);
$lat_value = $_REQUEST['latitude'];
$lon_value = $_REQUEST['longitude'];
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "XXX",
'oauth_access_token_secret' => "XXX",
'consumer_key' => "XXX",
'consumer_secret' => "XXX"
);
$url = 'https://api.twitter.com/1.1/geo/search.json';
$getfield = '?q=test&geocode='.$lat_value.','.$lon_value.',1mi&count=100';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
// Response from twitter
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
var_dump(json_decode($response));

As per the documentation says, you have to specify either lat, long, ip or query, and you are not specifying any of them. I don't see any of those parameters in your querystring.
Try with this query:
$getfield = "query=test&lat={$lat_value}&long={$lon_value}&accuracy=1m&max_results=100"

Related

Get hashtag posts with specific user

I want to get tweets with a specific hashtag with a specific user. I used
this request to that. But it's not working. But its working without but in the documentation, they said this is working.
<?php
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ * */
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
/** Note: Set the GET field BEFORE calling buildOauth(); * */
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$hashtag = 'zkst';
$getfield = '?q=from%3Ajulia44K%20%23' . $hashtag . '&result_type=mixed&include_entities=true&count=20';
// Perform the request
$twitter = new TwitterAPIExchange($settings);
$json_output = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
// Let's calculate how many results we got
$json = json_decode($json_output);
$n = count($json->statuses);
echo $n;
?>

Count Tweets - Twitter API

I want to count all the tweets that are getting returned from below Twitter API Script. I couldnt find out how to count the tweets that get returned in array json..
Code:
<?php
ini_set('display_errors', 1);
require_once('TwitterAPIClass/TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "XXXX",
'oauth_access_token_secret' => "YYYY",
'consumer_key' => "XXX",
'consumer_secret' => "YYYY"
);
/** Perform a GET request and echo the response **/
/** Note: Set the GET field BEFORE calling buildOauth(); **/
// Twitter Search API goes back to max last 7 days
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$getfield = '?q=%24neos&count=100&since=2017-09-4';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$twitter_data = json_decode($twitter->setGetfield($getfield)->buildOauth($url, $requestMethod)->performRequest(), true);
print_r ($twitter_data);
$countTweets = count($twitter_data['statuses']);
echo $countTweets;
?>

PHP error empty response

The php code below should work however, I receive an error, empty response, from my server. Ive tried shutting down firewall but no luck. Can you please help me I have been trying different methods for ages I have no idea whats wrongs. Thank you.
<?php
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET";
$getfield = '?q=cloud%20computing&count=5&result_type=popular';
$twitter = new TwitterAPIExchange($settings);
echo $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
?>
you need to use json_decode : http://php.net/manual/en/function.json-decode.php
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "xxx",
'oauth_access_token_secret' => "xxx",
'consumer_key' => "xxx",
'consumer_secret' => "xxx"
);
$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET";
$getfield = '?q=cloud%20computing&count=5&result_type=popular';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$string = json_decode($response, true); // there you handle twitter response
print_r($string); // here is the echo'd result, not needed after
// from here, you have access to the data
// then, a foreach loop will give you access to whatever you want to echo
foreach($string['statuses'] as $tweets) {
$tweet = $tweets['text'];
echo "<p>Tweet: $tweet </p>";
}
?>
updated with the loop example

var_dump(tweet->id); gives int(198231293) int(123981623) how to i retrieve one and store it on a variable?

So i am trying to get that tweet id of a retweeted id and since twitter recently allows you to retweet your own tweet, when i try to get the retweeted tweet's id of my own tweet, it gives me two variables when i use var_dump i want only one of it how do i extract?
i used
var_dump(tweet->id);
in the code and when i execute it, it shows like int(12381239) int(29384121)
i want to take the first int variable that is 12381239 and store it in a variable, i tried all i can but failed can anyone help me?
UPDATE : Here is the code
<?php
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "xxxxxx-asdsaldjasd",
'oauth_access_token_secret' => "xxxxxx",
'consumer_key' => "xxxxxxx",
'consumer_secret' => "xxxxxxxxxx"
);
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = "GET";
$getfield = '?screen_name=username';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
$tweets = json_decode($response);
foreach($tweets as $tweet)
{
if ($tweet->retweeted) // here is the part where i did var_dump
{
$url = "https://api.twitter.com/1.1/statuses/destroy/$tweet->id.json";
$requestMethod = 'POST';
$postfields = array(
'id' => '$tweet->id'
);
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
$response;
}
}
what i'm trying to do is undo the retweets, which was working fine until twitter let the accs rt themselves.

Applying Twitter API using example but return Null

I am using twitter-api-php-master to search keywords through twitter but got Null return. Really not sure why it is like that.
My code is like:
<?php
#### Include the class file ####
require_once('TwitterAPIExchange.php');
#### Set access tokens ####
$settings = array(
'oauth_access_token' => "[something]",
'oauth_access_token_secret' => "[something]",
'consumer_key' => "[something]",
'consumer_secret' => "[something]"
);
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
$getfield = '?q=test&geocode=37.781157,-122.398720,1mi&count=100';
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
var_dump(json_decode($response));
?>
I pretty much copy this from example from https://github.com/J7mbo/twitter-api-php/wiki/Twitter-API-PHP-Wiki. But the result is just Null. Someone knows what is the problem?

Categories