Twitter API: Get Followers +99 - php

Using the twitter API (and OAuth) if i was to call for the user followers, (statuses/followers) i would be returned only 99 results.
Is there a way i can return 99, then call again starting at follower 100 then looping through this style of calling until the total number of followers has been returned?
Or just return ALL followers?

You need to specify cursor parameter as described in the API documrnation. E.g. specify cursor=-1 to request the first page and then use a next_cursor value returned in the first response:
http://twitter.com/statuses/followers/barackobama.xml?cursor=-1
http://twitter.com/statuses/followers/barackobama.xml?cursor=1300794057949944903

<?php
$trends_url = "http://api.twitter.com/1/statuses/followers/fawadghafoor.json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $trends_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$curlout = curl_exec($ch);
curl_close($ch);
$response = json_decode($curlout, true);
foreach($response as $friends){
$thumb = $friends['profile_image_url'];
$url = $friends['screen_name'];
$name = $friends['name'];
?>
<a title="<?php echo $name;?>" href="http://www.twitter.com/<?php echo $url;?>"><img class="photo-img" src="<?php echo $thumb?>" border="0" alt="" width="40" /></a>
<?php } ?>

Be sure you're using the right call. followers/ids gives you 5000 at a time (but it's just a list of ids). This call, too, uses the cursor to let you step through pages of users. You get a zero back when you have them all.

Twitter API restricts us to make api Call for method followers/ids is 15 requests per 15 minutes. If you make more than this api will give you an error message that Rate Limit Reached.
For more information of twitter API rate Limit
Visit-https://dev.twitter.com/docs/rate-limiting/1.1 and https://dev.twitter.com/docs/rate-limiting/1.1/limits

Twitter only allows a certain number of API requests per hour, and I think minute. You might not be able to retrieve any more than 99 requests at once.

Though I asked this quite a while ago, I came back to building something quite similar recently (+ new programming skills).
I noticed Twitter API have a method to get all of a users followers (or followings) user ID's in one request. I found the best way was to array_chunk up the ID's into batches of 100 (and only take the first 30 arrays as I dont want to use all the users api requests that hour - they might want to actually tweet!). Then there is a method that allows you to get up to 100 users userinfo (from the view of the currently authenticated user) so I just do a loop (sleep a bit inbetween) and then you've got 30,000 twitter followers!
I would recommend doing this asynchronously in a queue system, as if you do this on the fly when the users requests a page on the site it could be very slow and you might be prone to a HTTP timeout. Also cache them like hell!
Sorry I didn't post any code, but hopefully this thought process will help someone :)

$cursor = -1;
$account_from = 'twitter_account';
do
{
$json = file_get_contents('http://api.twitter.com/1/statuses/followers/' . $account_from .'json?cursor=' . $cursor);
$accounts = json_decode($json);
foreach ($accounts->users as $account)
{
array(
':twitter_id' => $account->id_str,
':account' => $account->screen_name,
':description' => $account->description,
);
}
$cursor = $accounts->next_cursor;
}
while ($cursor > 0);

Related

Hit API only when there is change - Live Score

I'm using an API that provides the score of the game. To show the score in real-time, I'm hitting API after every 2-3 seconds to see if any changes happen in the score but I think this method isn't very efficient. It would exceed my API requests very quickly. Is there any other way to achieve this method (that hits API only when there is a change in the score)? or Is this something that isn't possible with PHP?
This is what I'm currently doing (just an example)
<?php
while(true){
$api_url = "here goes url that returns response in json";
$score_api = file_get_contents($api_url);
$score_array = json_decode($score_api, true);
$score = $score_array['result']['score'];
sleep(2); // hit API again after 2 seconds
}
?>

What type of configration is required to call run multirequest in PHP?

I want to run more than 2500+ call on same time. So i have created a batch of 100 (2500/100 = 25 total call).
// REQUEST_BATCH_LIMIT = 100
$insert_chunks = array_chunk(['array', 'i want', 'to', 'insert'], REQUEST_BATCH_LIMIT);
$mh = $running = $ch = [];
foreach ($insert_chunks as $chunk_key => $insert_chunk) {
$mh[$chunk_key] = curl_multi_init();
$ch[$chunk_key] = [];
foreach ($insert_chunk as $ch_key => $_POST) {
$ch[$chunk_key][$ch_key] = curl_init('[Dynamic path of API]');
curl_setopt($ch[$chunk_key][$ch_key], CURLOPT_RETURNTRANSFER, true);
curl_multi_add_handle($mh[$chunk_key], $ch[$chunk_key][$ch_key]);
}
do {
curl_multi_exec($mh[$chunk_key], $running[$chunk_key]);
curl_multi_select($mh[$chunk_key]);
} while ($running[$chunk_key] > 0);
foreach(array_keys($ch[$chunk_key]) as $ch_key) {
$response = curl_getinfo($ch[$chunk_key][$ch_key]);
$returned_data = curl_multi_getcontent($ch[$chunk_key][$ch_key]);
curl_multi_remove_handle($mh[$chunk_key], $ch[$chunk_key][$ch_key]);
}
curl_multi_close($mh[$chunk_key]);
}
When i running this in local the system is hanged totally.
But this limit of batch like 100, 500 are not same on different device and server, so what is the reason about it? and what changes should i do to increase it?
If i am adding 1000 data with batch of 50, so for every batch 50 records should insert, but it insert randomly for a batch like 40, 42, 48, etc. so way this is skipped calls? (If i am using single record with simple cURL using loop then it is working fine.)
P.S. This code is i am using for bigcommrece API.
The BigCommerce API definitely throttles requests. The limits are different depending on which plan you are on.
https://support.bigcommerce.com/s/article/Platform-Limits
The "Standard Plan" is 20,000 per hour. I'm not sure how that is really implemented, though, because in my own experience, I've been throttled before hitting 20,000 requests in an hour.
As Nico Haase suggests, the key is for you to log every response you get from the BigCommerce API. While not a perfect system, they do usually provide a response that is helpful to understand the failure.
I run a process that makes thousands of API requests every day. I do sometimes have requests that fail as if the BigCommerce API simply dropped the connection.

use Curl in foreach

I am working on project need to get last video from many channels in Youtube and store it in mysql now I work in file it is work by cron job every on hours to check last video , the problem now I have more than 30 channels and I use curl to get last video in every channel ,you can see the code
foreach ($channels as $channel){
$channel_id = $channel['channel_id'];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=$channel_id&maxResults=50&order=date&key=$key" ,
CURLOPT_RETURNTRANSFER => true
));
$json = curl_exec($curl);
$data_json = json_decode($json , TRUE);
$item = $data_json['items'][0];
// complete code here to check last video
}
now my qustion is:
1- is it good idea or there are better?
2 - are there any problem to use many request Curl in youtube api?
Thanks
YouTube API v2 supported batch operations; API v3 no longer does. You could do try to run multiple requests in parallel using curl_multi_init().
If you only care about one item per request, don't request maxResults=50.
Do all of the videos have something else in common? For example, are the all uploaded by the authenticated user? If so, try searching for videos uploaded by that user (forMine=true), publishedAfter the time of the previous cron job. Then triage the results according to each video's channelId.

How to use auh with Twitter trends

I want to use Twitter Trends, but not limited to the last ten trends. However I do not understand the API.
My basic code is...
$woeid = 2459115;
$json = file_get_contents("https://api.twitter.com/1/trends/".$woeid.".json", true);
$decode = json_decode($json, false);
$data = $decode[0]->trends;
foreach ($data as $item) {
...
}
When i create an app with Twitter, I see...
Signature base string... Authorization header... cURL command...
with
Request type: GET Request URI: https://api.twitter.com/1/
But how to deal with this?
Thanks,
V.
No, the API itself returns only 10 trends. It doesn't require creating an app, or authentication to return more trends, the trends API is public.
https://dev.twitter.com/docs/api/1/get/trends/%3Awoeid
Returns the top 10 trending topics for a specific WOEID, if trending information is available for it.
Edit: Yeah your code is correct.

how to loop through 5,000 Twitter users to grab the bio / description

I have a text file with 5,000 Twitter Users :-
JRJSHEARD
KMM_1979
ELMOCHLOE
ANNIEMMERSON
PATLOCKLEY
LISSYNUMBER
CAL32INCHSCREEN
PRINGLEDUDE
CORESMUSIC
I have found this API http://api.twitter.com/1/users/show.xml?screen_name=JRJSHEARD which is really useful and just what I need.
How would I write a php function to loop through the user names in a text file and append their bio found between these tags (<description> </description>).
Is this possible? Any help would be gratefully received.
If you want to harvest user info in bulk from Twitter use users/lookup rather than users/show. The users/lookup API call returns 100 user objects at a time, and you can either pass the user IDs or the screen names when you make the call, however you will need to authenticate using OAuth in order to use it.
I recommend using JSON since it is a much more lightweight document format than XML. You will typically transfer only about 1/3 to 1/2 as much data over the wire, and I find that (in my experience) Twitter times-out less often when serving JSON.
http://api.twitter.com/1/users/lookup.json?screen_name=JRJSHEARD,KMM_1979,ELMOCHLOE
That's the direct API call, but if you're just starting out, what I would recommend is using a Twitter service implementation rather than try to do all the heavy lifting yourself. I'm not a PHP person, but my PHP-using Twitter buddies recommend Zend - http://framework.zend.com/manual/en/zend.service.twitter.html
$api = "http://api.twitter.com/1/users/show.xml?screen_name=";
$users = file("users.txt", FILE_IGNORE_NEW_LINES);
$i = 0;
foreach($users as $user){
$data = curl("$api$user");
preg_match("#<description>(.*?)</description>#is", $data, $matches);
$bio[$i]["user"] = $user;
$bio[$i]["description"] = $matches[1];
$i++;
}
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_close($ch);
return curl_exec($ch);
}

Categories