Twitter API to get tweet and re-tweets count in php - twitter

Is there any API for getting twitter tweets and retweets count.
Please help me
Thanks in advance.

function getTweetCount($url)
{
$url = urlencode($url);
$twitterEndpoint = "http://urls.api.twitter.com/1/urls/count.json?url=%s";
$fileData = file_get_contents(sprintf($twitterEndpoint, $url));
$json = json_decode($fileData, true);
unset($fileData); // free memory
//print_r($json);
return $json['count'];
}

Related

how to implement friendships/exists in oriceon/oauth-5-laravel twitter api?

iam using oriceon/oauth-5-laravel .help me to implement friendships/exists .I want to post a request to follow particular person.help me.tysm advance
Consider the situation that you want to follow NASA in twitter.NASA is the screen name of NASA.You should add screen name to the url as below.Add this method to your controller and do proper routing.
public function followWithTwitter(Request $request)
{
$token = $request->get('oauth_token');
$verify = $request->get('oauth_verifier');
$tw = \OAuth::consumer('Twitter');
if ( ! is_null($token) && ! is_null($verify))
{
// This was a callback request from twitter, get the token
$token = $tw->requestAccessToken($token, $verify);
// Send a request with it
$result = json_decode($tw->request('https://api.twitter.com/1.1/friendships/create.json?screen_name=NASA&follow=true','POST'), true);
if (!$result){
//do some tasks for calculating and database updation for following
return ("failed");
}
else{
//do some tasks for calculating and database updation for following
return ("success");
}
}
// if not ask for permission first
else
{
// get request token
$reqToken = $tw->requestRequestToken();
// get Authorization Uri sending the request token
$url = $tw->getAuthorizationUri(['oauth_token' => $reqToken->getRequestToken()]);
// return to twitter login url
return redirect((string)$url);
}
}

How can i see a list of my Twilio account phone numbers by Twilio API?

I have a twilio account for sending SMS. However, i need guidance on how to see a list of my account phone numbers by calling its API. I have tried to navigate through the different menus in API but i dont see any option to get list of my account phone numbers. Thank you in advance for your guidance.
Twilio developer evangelist here.
If you want to list the phone numbers you have in your Twilio account through the API you need the Incoming Phone Numbers list resource.
You can access this easily with the Twilio rubygem like so:
require 'twilio-ruby'
#client = Twilio::REST::Client.new YOUR_ACCOUNT_SID, YOUR_AUTH_TOKEN
numbers = #client.account.incoming_phone_numbers.list
numbers.each do |number|
# do something with the number
puts number.phone_number
end
Here is a PHP version...
<?php
namespace App\Test;
use Twilio\Rest\Client;
use Twilio\Rest\API\V2010\AccountContext;
use Twilio\Rest\Api\V2010\Account\IncomingPhoneNumberList;
use Twilio\Rest\Api\V2010\Account\IncomingPhoneNumberInstance;
class TestClient {
private $client = null;
public function __construct() {
$sid = $_ENV['TWILIO_SID'];
$token = $_ENV['TWILIO_TOKEN'];
$this->client = new Client($sid, $token);
}
public function getActiveNumbers()
{
$context = $this->client->getAccount();
$activeNumbers = $context->incomingPhoneNumbers;
$activeNumberArray = $activeNumbers->read();
$numbers = [];
foreach($activeNumberArray as $activeNumber) {
error_log('active number = '.$activeNumber->phoneNumber);
$numbers[] = (object)[
'number' => $activeNumber->phoneNumber,
'name' => $activeNumber->friendlyName,
'sid' => $activeNumber->sid
];
}
var_dump($numbers);
return $numbers;
}
}

Google Youtube API Search doesn't retrieve all videos in the channel

I have to retrieve all video of my channel with Youtube API.
All videos are published on Youtube and I can see them correctly.
I tried to make the request directly from this page:
https://developers.google.com/youtube/v3/docs/search/list
and this is the example request:
GET http s://www.googleapis.com/youtube/v3/search?part=snippet&channelId=myChannelID&maxResults=50&key={YOUR_API_KEY}
Request doesn't retrieve all videos, it returns only 7 on the total of 9.
All videos have the same configuration. Missing videos are always the same.
If I use the video API passing the ID of one of those videos excluded from the search response, it returns a correct response and it belong correctly to my channel:
https://developers.google.com/youtube/v3/docs/videos/list#try-it
Someone can help me?
thank you in advance
Francesco
The answer to "How do I obtain a list of all videos in a channel using the YouTube Data API v3?" here may be what you need. Look especially at the video linked to in the answer.
To summarize, to get all the uploads from a channel, you need to get the items from the uploads playlist for the channel using playlistItems.list on that playlist's ID rather than calling search.list on the channel ID.
Try this two-step approach:
Get the ID of your channel's uploads playlist using the channels.list API call: GET https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id={YOUR_CHANNEL_ID}&key={YOUR_API_KEY}
Get the videos from the uploads playlist using the playlistItems.list call: GET https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=3&playlistId={YOUR_PLAYLIST_ID}&key={YOUR_API_KEY}
try this
async static Task<IEnumerable<YouTubeVideo>> GetVideosList(Configurations configurations, string searchText = "", int maxResult = 20)
{
List<YouTubeVideo> videos = new List<YouTubeVideo>();
using (var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = configurations.ApiKey
}))
{
var searchListRequest = youtubeService.Search.List("snippet");
searchListRequest.Q = searchText;
searchListRequest.MaxResults = maxResult;
searchListRequest.ChannelId = configurations.ChannelId;
searchListRequest.Type = "video";
searchListRequest.Order = SearchResource.ListRequest.OrderEnum.Date;// Relevance;
var searchListResponse = await searchListRequest.ExecuteAsync();
foreach (var responseVideo in searchListResponse.Items)
{
videos.Add(new YouTubeVideo()
{
Id = responseVideo.Id.VideoId,
Description = responseVideo.Snippet.Description,
Title = responseVideo.Snippet.Title,
Picture = GetMainImg(responseVideo.Snippet.Thumbnails),
Thumbnail = GetThumbnailImg(responseVideo.Snippet.Thumbnails)
});
}
return videos;
}
}

Get user data by using Twitter Streaming API, Twitter Rest API

I do some analysis on twitter users features like number of following , number of retweet, number of friend , etc
I have all my information from Twitter Rest API
But there is an Rate Limit Exceeded error occurred when I tried to retrieve all data
can I have all these data from Twitter Streaming API , if I can , how I can you it?
If not what Is the Solution?
Thanks for Help
On each response you can call getRateLimitStatus() to get a RateLimitStatus and if remaining calls is 0, sleep the thread till time limit is over.
do {
TwitterResponse response = twitter.getFollowersIDs(userId, cursor);
RateLimitStatus status = response.getRateLimitStatus();
if(status.getRemaining() == 0) {
try {
Thread.sleep(status.getSecondsUntilReset() * 1000);
}
catch(InterruptedException e) {
// ...
}
}
} while(cursor > 0);
Twitter API update limits error 403

how to get list of retweeters using streaming api

Is there a way to get the list of retweeters ids using streaming api
REST api has "GET statuses/:id/retweeted_by/ids" for getting the list of retweeters
Streaming api has a "statuses/retweet", but is not a generally available resources.
So the idea is to use "statuses/filter" and filter based on tweet ids.
Thank you
In the results returned by the streaming API, retweeters (if any) are listed here:
$retweeters = $tweet->{'retweeted_status'}->{'activities'}->{'retweeters'};
Here's a page which shows the ids of retweeters for a stream filtered with a search for the word 'love' — make sure to use your Twitter username and password. Note that the APIs only return the first 100 retweeters.
<html><body>
<?php
echo(str_pad("START<br>",2048));
#ob_flush();
flush();
$opts = array(
'http'=>array(
'method' => "POST",
'content' => 'track=love',
'header' => "Content-Type: application/x-www-form-urlencoded\r\n"
)
);
$context = stream_context_create($opts);
$username = 'your_twitter_username';
$password = 'your_twitter_password';
while (1){
$instream = fopen('http://'.$username.':'.$password.'#stream.twitter.com/1/statuses/filter.json','r' ,false, $context);
while(! feof($instream)) {
if(! ($line = stream_get_line($instream, 20000, "\n"))) {
continue;
}else{
$tweet = json_decode($line);
$retweeters = array();
$retweeters = $tweet->{'retweeted_status'}->{'activities'}->{'retweeters'};
//We store the new post in the database, to be able to read it later
if (sizeof($retweeters) > 0) {
echo("<br><br>");
print_r($retweeters);
}
#ob_flush();
flush();
}
}
}
?>
</html></body>

Resources