Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Twitter API游标PHP_Php_Twitter_Cursor - Fatal编程技术网

Twitter API游标PHP

Twitter API游标PHP,php,twitter,cursor,Php,Twitter,Cursor,我已经有了一个从friends/ID收集前5000个用户的脚本。 我想收集所有的朋友,所以我必须通过游标来获取他们。我想我理解光标的工作方式,但仍然无法使其工作。我正试图进入下一个光标,但不知道我做错了什么 我试图获取的光标似乎没有正确调用 我希望得到一些反馈,因为twitter的API没有给出任何真实的例子 <?php header("Content-Type: text/html;charset=utf-8"); ini_set('display_errors', 1); requi

我已经有了一个从friends/ID收集前5000个用户的脚本。 我想收集所有的朋友,所以我必须通过游标来获取他们。我想我理解光标的工作方式,但仍然无法使其工作。我正试图进入下一个光标,但不知道我做错了什么

我试图获取的光标似乎没有正确调用

我希望得到一些反馈,因为twitter的API没有给出任何真实的例子

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

?>

试试这段代码。它将吸引多达3000名朋友

脚本的运行时间约为3-5分钟

//function to fetch friends of the authenticating user

function getAllFriends($auth_user_screen_name)
{
    //---getting application tokens and connection to database
   $arraySettings = defaultSettings();

  $con = $arraySettings['connection'];

  //create api instance
   \Codebird\Codebird::setConsumerKey('consumerKey', 'consumerSecret');
   $objTwitter = \Codebird\Codebird::getInstance();

   $oauth_token = "oauth_token";
   $oauth_token_secret = "oauth_token_secret";

   $objTwitter->setToken($oauth_token, $oauth_token_secret);

   //initialize empty array to store friends data returned from API     
   $friendsData=array();

  if (strlen($oauth_token) > 0 && strlen($oauth_token_secret) > 0) {

    $cursor = -1;
    $j=0;
    while ($cursor != 0)
    {
        $params = array(
            'screen_name' => $auth_user_screen_name,
            'count' => 200,
            'cursor' => $cursor
        );

        $json = $objTwitter->friends_list($params);
        $httpStatus = $json->httpstatus;

        $jsonFriends=(array)$json;

        //meta is user defined key 
        $friendsData['meta']['remaining']=$jsonFriends['rate']['remaining'];
        $friendsData['meta']['seconds_to_reset']=($jsonFriends['rate']['reset'] - time());

        if ($httpStatus == 200) {

            echo "remaining requests " . $jsonFriends['rate']['remaining'] . '<br>';
            echo "next reset after (sec) " . ($jsonFriends['rate']['reset'] - time()) . '<br>';

            $lenght = count($jsonFriends['users']);

            $cursor = $jsonFriends['next_cursor'];
            echo "next_cursor=>" . $cursor . "<br>";

            $i = 0;  //loop counter


            while ($i < $lenght) {

                $friendsData[$j]['screen_name']=$profileScreenName = $jsonFriends['users'][$i]->screen_name;
                $friendsData[$j]['name']=$profileFullName = $jsonFriends['users'][$i]->name;
                $friendsData[$j]['location']=$profileLocation = $jsonFriends['users'][$i]->location;
                $friendsData[$j]['description']=$profileDescription = $jsonFriends['users'][$i]->description;
                $friendsData[$j]['followers_count']=$followersCount = $jsonFriends['users'][$i]->followers_count;
                $friendsData[$j]['friends_count']=$friendsCount = $jsonFriends['users'][$i]->friends_count;

                $friendsData['']=$language = $jsonFriends['users'][$i]->lang;
                $friendsData['']=$profileTwitterId = $jsonFriends['users'][$i]->id;
                $friendsData['']=$isVerified = $jsonFriends['users'][$i]->verified;
                $friendsData['']=$joinDate = date('Y-m-d H:i:s', strtotime($jsonFriends['users'][$i]->created_at));
                $friendsData['']=$userWebsiteUrl = $jsonFriends['users'][$i]->url;
                $friendsData['']=$tweetsCount = $jsonFriends['users'][$i]->statuses_count;

                $friendsData['']=$defaultProfileImage = $jsonFriends['users'][$i]->default_profile_image;

                $friendsData['']=$favouritesCount = $jsonFriends['users'][$i]->favourites_count;
                $friendsData['']=$isGeoEnabled = $jsonFriends['users'][$i]->geo_enabled;
                $friendsData['']=$utcOffset = $jsonFriends['users'][$i]->utc_offset;
                $friendsData['']=$timeZone = $jsonFriends['users'][$i]->time_zone;


                $i++; //counter for friends in particular cursor
                $j++; //counter for friends for all friends including all cursor requests

            }

        } else {
               //if httpStatus other than 200
            $friendsData['meta']['error_code']=$errorCode = $jsonFriends['errors'][0]->code;
            $friendsData['meta']['error_message']=$errorMessage = $jsonFriends['errors'][0]->message;


            echo "<pre>";
            //print error and rate data
            print_r($friendsData['meta']); 


        }
        // check whether Api limit exceed
        if ($friendsData['meta']['remaining']==0)
        {

            echo "<br>please wait=>".$friendsData['meta']['seconds_to_reset']." seconds";
            break;
        }

    }
    echo "<pre>";
    print_r($friendsData); //print all friends data including errors if any

  }
}


//call function to fetch all friends 
//only max 3000 friends will retrieved in single request
getAllFriends('myUsername');
//获取身份验证用户朋友的函数
函数getAllFriends($auth\u user\u screen\u name)
{
//---获取应用程序令牌和到数据库的连接
$arraySettings=defaultSettings();
$con=$arraySettings['connection'];
//创建api实例
\Codebird\Codebird::setConsumerKey('consumerKey','ConsumerCret');
$objTwitter=\Codebird\Codebird::getInstance();
$oauth_token=“oauth_token”;
$oauth\u token\u secret=“oauth\u token\u secret”;
$objTwitter->setToken($oauth\u token,$oauth\u token\u secret);
//初始化空数组以存储从API返回的好友数据
$friendsData=array();
如果(strlen($oauth\u令牌)>0和&strlen($oauth\u令牌)>0){
$cursor=-1;
$j=0;
而($cursor!=0)
{
$params=数组(
'screen\u name'=>$auth\u user\u screen\u name,
“计数”=>200,
“cursor”=>$cursor
);
$json=$objTwitter->friends\u list($params);
$httpStatus=$json->httpStatus;
$jsonFriends=(数组)$json;
//meta是用户定义的密钥
$friendsData['meta']['remaining']=$jsonFriends['rate']['remaining'];
$friendsData['meta']['seconds\u to_reset']=($jsonFriends['rate']['reset']-time());
如果($httpStatus==200){
回显“剩余请求”。$jsonFriends['rate']['remaining'].
; 回显“秒后的下一次重置”。($jsonFriends['rate']['reset']-time())。
; $lenght=count($jsonFriends['users']); $cursor=$jsonFriends['next_cursor']; 回显“下一个光标=>”$cursor.“
”; $i=0;//循环计数器 而($i<$lenght){ $friendsData[$j]['screen\u name']=$profileScreenName=$jsonFriends['users'][$i]>screen\u name; $friendsData[$j]['name']=$profileFullName=$jsonFriends['users'][$i]->name; $friendsData[$j]['location']=$profileLocation=$jsonFriends['users'][$i]->location; $friendsData[$j]['description']=$profileDescription=$jsonFriends['users'][$i]->description; $friendsData[$j]['followers\u count']=$followers count=$jsonFriends['users'][$i]>followers\u count; $friendsData[$j]['friends\u count']=$friendsCount=$jsonFriends['users'][$i]>friends\u count; $friendsData['']=$language=$jsonFriends['users'][$i]>lang; $friendsData['']=$profileTwitterId=$jsonFriends['users'][$i]->id; $friendsData['']=$isVerified=$jsonFriends['users'][$i]->已验证; $friendsData['']=$joinDate=date('Y-m-d H:i:s',strotTime($jsonFriends['users'][$i]->created_at)); $friendsData['']=$userWebsiteUrl=$jsonFriends['users'][$i]->url; $friendsData[''']=$tweetsCount=$jsonFriends['users'][$i]->状态计数; $friendsData[''']=$defaultProfileImage=$jsonFriends['users'][$i]>default\u profile\u image; $friendsData[''']=$FavoriteSCount=$jsonFriends['users'][$i]>Favorites\u count; $friendsData[''']=$isGeoEnabled=$jsonFriends['users'][$i]>geo_enabled; $friendsData['']=$utcOffset=$jsonFriends['users'][$i]>utc\U偏移量; $friendsData['']=$timeZone=$jsonFriends['users'][$i]>时区; $i++;//特定游标中朋友的计数器 $j++;//所有好友的好友计数器,包括所有光标请求 } }否则{ //如果httpStatus不是200 $friendsData['meta']['error_code']=$errorCode=$jsonFriends['errors'][0]->code; $friendsData['meta']['error_message']=$errorMessage=$jsonFriends['errors'][0]->message; 回声“; //打印错误和速率数据 打印($friendsData['meta']); } //检查Api是否超过限制 如果($friendsData['meta']['remaining']==0) { echo“
请稍候=>”$friendsData['meta']['seconds\u to\u reset'].“seconds”; 打破 } } 回声“; print_r($friendsData);//打印所有好友数据,包括错误(如果有) } } //调用函数获取所有好友 //一次请求最多只能检索3000个好友 getAllFriends('myUsername');
试试这段代码。它将吸引多达3000名朋友

脚本的运行时间约为3-5分钟

//function to fetch friends of the authenticating user

function getAllFriends($auth_user_screen_name)
{
    //---getting application tokens and connection to database
   $arraySettings = defaultSettings();

  $con = $arraySettings['connection'];

  //create api instance
   \Codebird\Codebird::setConsumerKey('consumerKey', 'consumerSecret');
   $objTwitter = \Codebird\Codebird::getInstance();

   $oauth_token = "oauth_token";
   $oauth_token_secret = "oauth_token_secret";

   $objTwitter->setToken($oauth_token, $oauth_token_secret);

   //initialize empty array to store friends data returned from API     
   $friendsData=array();

  if (strlen($oauth_token) > 0 && strlen($oauth_token_secret) > 0) {

    $cursor = -1;
    $j=0;
    while ($cursor != 0)
    {
        $params = array(
            'screen_name' => $auth_user_screen_name,
            'count' => 200,
            'cursor' => $cursor
        );

        $json = $objTwitter->friends_list($params);
        $httpStatus = $json->httpstatus;

        $jsonFriends=(array)$json;

        //meta is user defined key 
        $friendsData['meta']['remaining']=$jsonFriends['rate']['remaining'];
        $friendsData['meta']['seconds_to_reset']=($jsonFriends['rate']['reset'] - time());

        if ($httpStatus == 200) {

            echo "remaining requests " . $jsonFriends['rate']['remaining'] . '<br>';
            echo "next reset after (sec) " . ($jsonFriends['rate']['reset'] - time()) . '<br>';

            $lenght = count($jsonFriends['users']);

            $cursor = $jsonFriends['next_cursor'];
            echo "next_cursor=>" . $cursor . "<br>";

            $i = 0;  //loop counter


            while ($i < $lenght) {

                $friendsData[$j]['screen_name']=$profileScreenName = $jsonFriends['users'][$i]->screen_name;
                $friendsData[$j]['name']=$profileFullName = $jsonFriends['users'][$i]->name;
                $friendsData[$j]['location']=$profileLocation = $jsonFriends['users'][$i]->location;
                $friendsData[$j]['description']=$profileDescription = $jsonFriends['users'][$i]->description;
                $friendsData[$j]['followers_count']=$followersCount = $jsonFriends['users'][$i]->followers_count;
                $friendsData[$j]['friends_count']=$friendsCount = $jsonFriends['users'][$i]->friends_count;

                $friendsData['']=$language = $jsonFriends['users'][$i]->lang;
                $friendsData['']=$profileTwitterId = $jsonFriends['users'][$i]->id;
                $friendsData['']=$isVerified = $jsonFriends['users'][$i]->verified;
                $friendsData['']=$joinDate = date('Y-m-d H:i:s', strtotime($jsonFriends['users'][$i]->created_at));
                $friendsData['']=$userWebsiteUrl = $jsonFriends['users'][$i]->url;
                $friendsData['']=$tweetsCount = $jsonFriends['users'][$i]->statuses_count;

                $friendsData['']=$defaultProfileImage = $jsonFriends['users'][$i]->default_profile_image;

                $friendsData['']=$favouritesCount = $jsonFriends['users'][$i]->favourites_count;
                $friendsData['']=$isGeoEnabled = $jsonFriends['users'][$i]->geo_enabled;
                $friendsData['']=$utcOffset = $jsonFriends['users'][$i]->utc_offset;
                $friendsData['']=$timeZone = $jsonFriends['users'][$i]->time_zone;


                $i++; //counter for friends in particular cursor
                $j++; //counter for friends for all friends including all cursor requests

            }

        } else {
               //if httpStatus other than 200
            $friendsData['meta']['error_code']=$errorCode = $jsonFriends['errors'][0]->code;
            $friendsData['meta']['error_message']=$errorMessage = $jsonFriends['errors'][0]->message;


            echo "<pre>";
            //print error and rate data
            print_r($friendsData['meta']); 


        }
        // check whether Api limit exceed
        if ($friendsData['meta']['remaining']==0)
        {

            echo "<br>please wait=>".$friendsData['meta']['seconds_to_reset']." seconds";
            break;
        }

    }
    echo "<pre>";
    print_r($friendsData); //print all friends data including errors if any

  }
}


//call function to fetch all friends 
//only max 3000 friends will retrieved in single request
getAllFriends('myUsername');
//获取身份验证用户朋友的函数
函数getAllFriends($auth\u user\u screen\u name)
{
//---获取应用程序令牌和到数据库的连接
$arraySettings=defaultSettings();
$con=$arraySettings['connection'];
//创建api实例
\Codebird\Codebird::setConsumerKey('consumerKey','ConsumerCret');
$objTwitter=\Codebird\Codebird::getInstance();
$oauth_token=“oauth_token”;
$oauth\u token\u secret=“oauth\u token\u secret”;
$objTwitter->setToken($oauth\u token,$oauth\u token\u secret);
//初始化空数组以存储从API返回的好友数据
$friendsData=array();
如果(strlen($oauth\u令牌)>0和&strlen($oauth\u令牌)>0){
$cursor=-1;
$j=0;
而($cursor!=0)
{
$params=数组(
'screen\u name'=>$auth\u user\u screen\u name,
“计数”=>200,
“cursor”=>$cursor