Already I have a script that collects the first 5000 users from friends/ids. I want to collect all friends so I have to move through cursors to get them. I think I understand the way the cursor works but still I'm not able to make it work. I'm trying to acceed to next_cursor but don't know what I'm doing wrong.
It seems like the cursor I'm trying to get isn't properly called.
Would like some feedback with these because twitter's API doesn't give any real example.
<?php
header("Content-Type: text/html;charset=utf-8");
ini_set('display_errors', 1);
require_once('TwitterAPIExchange.php');
session_start();
$user = $_POST['nombre'];
$_SESSION['user']=$_POST['nombre'];
$usuario = $user;
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "twitter";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "k1",
'oauth_access_token_secret' => "k2",
'consumer_key' => "k3",
'consumer_secret' => "k4"
);
//https://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name=microsoft
$cursor = "cursor=".-1;
$url = 'https://api.twitter.com/1.1/friends/ids.json';
$getfield = '?'.$cursor.'screen_name='.$usuario;
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$json = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(true, array(CURLOPT_CAINFO => dirname(__FILE__) . '/cacert.pem'));
$arrayFriends = json_decode($json, true,512,JSON_BIGINT_AS_STRING);
echo 'Usuario' .";". 'Nombre'.";".'Location'.";".'Numero Amigos'.";".'Numero followers'.";".'Descripcion'."
";
foreach($arrayFriends['next_cursor'] as $curs){
foreach($arrayFriends['ids'] as $obj){
while($cursor != 0){
//$cursor->$arrayFriends['ids']->next_cursor;
$cursor = $curs;
//$cursor = "&cursor=" + $cursor;
$sql = "INSERT INTO friends (user, id) VALUES ('$usuario','$obj')";
if ($conn->query($sql) === TRUE) {
//echo "New record created successfully";
header("Location:logic.php");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
}
$conn->close();
?>