Twitter api not display following count - php

I want to get total number of following count in twitter using TwitterAPIExchange API but the result does not contain following data.
How can i do this any idea?
The followers_count and friends_count are availabel but not following_count.

You can use the following code for follower counter:
<?php
require_once('TwitterAPIExchange.php'); //https://github.com/J7mbo/twitter-api-php
/** Access token (https://dev.twitter.com/apps/) **/
$access_token_settings = array(
'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN",
'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);
$twitter_api_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=YOUR SCREEN NAME';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($access_token_settings);
$follow_count=$twitter->setGetfield($getfield)->buildOauth($twitter_api_url, $requestMethod)->performRequest();
$data = json_decode($follow_count, true);
$followers_count=$data[0]['user']['followers_count'];
echo $followers_count;
?>
For parsing data into xml format you can use following code:
<?php
$xml_data = new SimpleXMLElement(urlencode(strip_tags('https://twitter.com/users/google.xml')), null, true);
echo "Follower count: ".$xml_data->followers_count;
?>
Hope this helps.

Related

Php Twitter Post tweet with media

With this code, I can upload image and print Media ID but I can't post a tweet with image. Could you help me?
Thanks
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_include_path('/home/.....');
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' => "..."
);
$twitter = new TwitterAPIExchange($settings);
$file = file_get_contents('aaa.jpg');
$data = base64_encode($file);
// Upload image to twitter
$url = "https://upload.twitter.com/1.1/media/upload.json";
$method = "POST";
$params = array(
"media_data" => $data,
);
$json = $twitter
->buildOauth($url, $method)
->setPostfields($params)
->performRequest();
// Result is a json string
$res = json_decode($json);
// Extract media id
$id = $res->media_id_string;
print_r($id);
////// Code is working to this line and can print media id like 213213214123123
$url = "https://api.twitter.com/1.1/statuses/update.json";
$twitter = new TwitterAPIExchange($settings);
$requestMethod = 'POST';
$response = $twitter->setPostfields(
array('status' => '', 'media_ids' => $id)
)
->buildOauth($url, $requestMethod)
->performRequest();
The media_id value can be used to create a Tweet with an attached photo using the statuses/update endpoint. Use your media_id as a parameter in the Statuses/update

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

Follower count number in Twitter

How to get my followers count number with PHP.
I found this answer here: Twitter follower count number, but it is not working because API 1.0 is no longer active.
I have also tried with API 1.1 using this URL: https://api.twitter.com/1.1/users/lookup.json?screen_name=google but is is showing an error(Bad Authentication data).
Here is my code:
$data = json_decode(file_get_contents('http://api.twitter.com/1.1/users/lookup.json?screen_name=google'), true);
echo $data[0]['followers_count'];
If do it without auth (replace 'stackoverflow' with your user)
$.ajax({
url: "https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names=stackoverflow"
dataType : 'jsonp',
crossDomain : true
}).done(function(data) {
console.log(data[0]['followers_count']);
});
with php
$tw_username = 'stackoverflow';
$data = file_get_contents('https://cdn.syndication.twimg.com/widgets/followbutton/info.json?screen_names='.$tw_username);
$parsed = json_decode($data,true);
$tw_followers = $parsed[0]['followers_count'];
Twitter API 1.0 is deprecated and is no longer active. With the REST 1.1 API, you need oAuth authentication to retrieve data from Twitter.
Use this instead:
<?php
require_once('TwitterAPIExchange.php'); //get it from https://github.com/J7mbo/twitter-api-php
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN",
'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);
$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=REPLACE_ME';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$follow_count=$twitter->setGetfield($getfield)
->buildOauth($ta_url, $requestMethod)
->performRequest();
$data = json_decode($follow_count, true);
$followers_count=$data[0]['user']['followers_count'];
echo $followers_count;
?>
Parsing the XML might be easier in some cases.
Here's a solution (tested):
<?php
$xml = new SimpleXMLElement(urlencode(strip_tags('https://twitter.com/users/google.xml')), null, true);
echo "Follower count: ".$xml->followers_count;
?>
Hope this helps!

Twitter follower count number

Is the only way to get the follower count number in plain text is using cURL? or does the twitter API provides any such option?
https://api.twitter.com/1/users/lookup.json?screen_name=tvdw (my profile, just replace the screen name)
Also available as XML: https://api.twitter.com/1/users/lookup.xml?screen_name=tvdw
Obtaining it in PHP:
$data = json_decode(file_get_contents('https://api.twitter.com/1/users/lookup.json?screen_name=tvdw'), true);
echo $data[0]['followers_count'];
In API version 1.1 you can use: https://dev.twitter.com/docs/api/1.1/get/users/show
the 'followers_count' field should contain the follower count number.
In API version 1 which is deprecated you can use: https://dev.twitter.com/docs/api/1/get/users/show
Twitter API 1.0 is deprecated and is no longer active. With the REST 1.1 API, you need oAuth authentication to retrieve data from Twitter.
Use this instead:
<?php
require_once('TwitterAPIExchange.php'); //get it from https://github.com/J7mbo/twitter-api-php
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "YOUR_OAUTH_ACCESS_TOKEN",
'oauth_access_token_secret' => "YOUR_OAUTH_ACCESS_TOKEN_SECRET",
'consumer_key' => "YOUR_CONSUMER_KEY",
'consumer_secret' => "YOUR_CONSUMER_SECRET"
);
$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=REPLACE_ME';
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$follow_count=$twitter->setGetfield($getfield)
->buildOauth($ta_url, $requestMethod)
->performRequest();
$data = json_decode($follow_count, true);
$followers_count=$data[0]['user']['followers_count'];
echo $followers_count;
?>
Follow #twitterapi
<script>
!function(d,s,id){
var js,fjs=d.getElementsByTagName(s)[0];
if(!d.getElementById(id)){
js=d.createElement(s);
js.id=id;
js.src="//platform.twitter.com/widgets.js";
fjs.parentNode.insertBefore(js,fjs);
}
}
(document,"script","twitter-wjs");
</script>
data-show-count = "true"

Categories