Retrieving all twitter followers with a single request - php

I tried this and even added a cursor but it would still retrieve only the first 100 followers.
<?php
$cursor = -1;
$account_from = 'username';
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)
{
$a[] = $account->screen_name ;
}
$cursor = $accounts->next_cursor;
}
while ($cursor > 0);
foreach($a as $f) {
echo $f ;
}
?>
Is there a better and simpler way of doing it? Where am i going wrong? help please?

API docs state this request is deprecated:
This method is deprecated as it will only return information about users who have Tweeted recently. It is not a functional way to retrieve all of a users followers. Instead of using this method use a combination of GET followers/ids and GET users/lookup.

Use this API instead: https://dev.twitter.com/docs/api/1/get/followers/ids
There should be a property in your object called next_cursor, as long as that's non-zero, keep doing the request again (specifying that cursor) until you get all the results.

Related

how to get an array from mongodb/mongodb package

recently I started using MongoDB and I found this https://packagist.org/packages/mongodb/mongodb package. it seems a good one to work with. but I cant get an array when I want to find a specific data or the whole collection. it gives me object or collection of objects( depends on the find() or findMany() method) but i need a PHP array to work with it( do some loops, check some conditions or pass them into View side).
use MongoDB\Client;
$mongoObject = new Client();
$usersCollection = $mongoObject->selectDatabase('test')->selectCollection('users');
$users = $usersCollection->find([]);
foreach ($users as $user) {
echo "<pre style='font-size: 20px;'>";
var_dump($user);
echo "</pre>";
}
die();
the package is recommanded by php.net himself check it out here: http://php.net/manual/en/mongodb.tutorial.library.php
Is there anybody that can help me?
thanx to http://php.net/manual/en/mongodb.tutorial.library.php this page, I can simply use below structure to get the data.
use MongoDB\Client;
$mongoObject = new Client();
$usersCollection = $mongoObject->selectDatabase('test')->selectCollection('users');
$users = $usersCollection->find([]);
foreach ($users as $user) {
echo $user['_id'], ': ', $user['company_name'], "<br>";
}
die();

Twitch API and PHP(View Streamer Data)

I'm trying to find the best way to pull data about users down to display on my site. In the long run, the usernames will be coming from a MYSQL database, but for now I just created a small array. For testing purposes, I would love for it to spit out (username from array) and viewer count. But I'm having no luck.
Here is my code below, the code below I was just trying to output anything from the two usernames, but it was coming up empty.
<?php
$streamstats = array();
$username = array("streamerhouse", "cohhcarnage");
foreach($username as $stream) {
$data = json_decode(file_get_contents('http://api.justin.tv/api/stream/list.json?channel=' . $stream));
$streamstats[] = $stream;
$print_r($streamstats);
}
?>
Which I get nothing. Any advice?
You have 2 errors. First you have $ before print_r. Second one is that you print it in a foreach loop, but you should print everything after the loop. Give a shot code below:
$streamstats = array();
$username = array("streamerhouse", "cohhcarnage");
foreach($username as $stream) {
$data = json_decode(file_get_contents('http://api.justin.tv/api/stream/list.json?channel=' . $stream));
$streamstats[] = $data;
}
print_r($streamstats);

Are Facebook Names Publicly Accesible Using Facebook API

Are the names of profiles on Facebook publicly accessible, as if I would NOT need to log into Facebook to access them?
I am intending to store a large amount of names as a small piece of a larger project. I feel as if scraping Facebook for names would be a relatively simple task using the Facebook Graph API, but I am a little confused.
I found another tutorial online at http://jilltxt.net/?p=2810 which described an easy way of finding any Facebook profile picture using one simple line:
https://graph.facebook.com/USER-ID/picture?type=large
This was very helpful because I am able to use a range of ID numbers and a small amount of PHP to gather large amounts of profile pictures as seen on my test page here: http://www.joshiefishbein.com/fi/photobook.php
But what I am unfamiliar with is how I go from collecting pictures to names in this one simple line. Is it possible? Is there another (better) way?
Here's the code I am working with. The range of ID's are just an example.
function gen_pix($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
$x_arr = array_slice($numbers, 0, $quantity);
foreach ($x_arr as $key => $value) {
$username = "https://graph.facebook.com/" . $value . "/";
$json = json_decode(file_get_contents($username), true);
echo $json["name"];
}
}
$x = 337800042;
$y = 337800382;
$z = 1;
gen_pix($x,$y,$z);
I've gotten a little farther with this code, I can echo $username and I get the URL that I am looking for (for example https://graph.facebook.com/337800382/) but I do not get anything after that. json_decode isn't working seemingly.
In the same way you are pulling the profile picture, you can get the basic information of a user with their ID.
This page provides a list of data that is always publicly accessible.
So you need to make a GET request to pull back the JSON, like so...
https://graph.facebook.com/{user-id}/
For example https://graph.facebook.com/586207189/ pulls back my basic information. So your PHP would look like this
$json = json_decode(file_get_contents("https://graph.facebook.com/$user_id/"), true);
echo $json["name"];
PHP fiddle here
Update: Based on the code above, it's worth adding an IF to catch invalid Facebook IDs. Facebook IDs may not be sequential so not every one will return a name or image.
Updated code:
<?php
function gen_pix($min, $max, $quantity) {
$numbers = range($min, $max);
shuffle($numbers);
$x_arr = array_slice($numbers, 0, $quantity);
foreach ($x_arr as $key => $value) {
$username = "https://graph.facebook.com/" . $value . "/";
$json = json_decode(file_get_contents($username), true);
if (!isset($json['name'])) {
echo "Invalid ID<br />";
}
else {
echo $json["name"]. '<br />';
}
}
}
$x = 337800042;
$y = 337800382;
$z = 50;
gen_pix($x,$y,$z);
?>
PHP Fiddle here
It's also worth noting that pulling that much data from the graph is going to take a while. Have a look at doing batch requests to speed things up a bit. More info here

How do I show all my Twitter friends?

This script isn't working so good.
<?php
//include twitter class
require('classes/twitter.php');
$twitter = new Twitter('custToken', 'custSecret');
// set tokens
$twitter->setOAuthToken('blah blah');
$twitter->setOAuthTokenSecret('blah blah');
//$tweet = "This tweet was posted from a custom php script.";
//$twitter->statusesUpdate($tweet); // <-- this works!
$friends = $twitter->friendsList('tynamite');
foreach ($friends as $friend){
print_r($friend);
echo "<hr>";
}
?>
I want to display all my Twitter friends (everyone I follow) in a list.
The result I am getting is this.
Could anyone please help on this?
You should pass a cursor. You could use something like the code below (untested and just pseudo-code):
while($count == 20) {
$friends = $twitter->friendsList('tynamite', $cursor);
$count = count($friends);
$cursor++;
.. your code ..
}
twitter uses json to return its results. you need to parse the results returned and pull out the information you need. check this blog out for an example of how to use php to parse json

Facebook comments loop very very slow

I have this function in order to retrieve the count of Facebook comments to blog posts:
function comment_count($url) {
$json = json_decode(file_get_contents('https://graph.facebook.com/?ids=' . $url));
return ($json->$url->comments) ? $json->$url->comments : 0;
}
However if I insert it in a loop fetching the results of a query in order to retrieve five posts on a page, this function is seriously affecting the speed of the website (the page takes up to 6-7 seconds to load).
Is there a way to avoid this? Why is it so slow?
Thanks
Pass in a comma separated list of URLs to the ids parameter to get all the counts at once, or alternatively, cache them on the server side and use those values.
Example: https://graph.facebook.com/?ids=http://www.google.com,http://www.bing.com,http://www.yahoo.com
This is specified in Facebook's Graph API Reference under the section "selection"
An example implementation follows:
<?php
function comment_count($urls) {
$json = json_decode(file_get_contents('https://graph.facebook.com/?ids=' . implode(',', array_map("rawurlencode", $urls))));
$output = Array();
foreach($json as $url=>$data)
{
$output[$url] = isset($data->comments) ? $data->comments : 0;
}
return $output;
}
var_dump(comment_count(Array('http://www.facebook.com/', 'http://www.google.com')));
I hope this helps!

Categories