J7mbo / twitter-api-php

The simplest PHP Wrapper for Twitter API v1.1 calls
MIT License
1.82k stars 802 forks source link

Cursoring trough 5000+ users #232

Closed SergiTorres closed 7 years ago

SergiTorres commented 7 years ago

Hey. 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.

<?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'."\n";

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();

?>`
SergiTorres commented 7 years ago

Hey guys, sorry for being so tiring but I don't find the way to do it. Any tips/advices?

SergiTorres commented 7 years ago

I'm not finding any way to solve it. It stills doesn't work and don't know what I'm doing wrong.

If someone could help me I would be soso pleased.

J7mbo commented 7 years ago

Hey, sorry I'm not quite sure exactly what you're trying to do here. I would try and help but issues here are for library-specific problems not PHP general knowledge - but if you try Stackoverflow you'll very likely get a much faster answer from others :-)