drfrvbq736383 2016-03-07 06:23
浏览 61
已采纳

如何使用laravel从twitter API获取所有可用的用户搜索记录

I am working on twitter API (https://api.twitter.com/1.1/users/search.json). When I request for a user records using GET request I am getting just first page of records in json first 20 records but I want all records of any specific name which I enter. How to extend the request and store them in database

public function getTwitter() {
    $settings = array(
        'oauth_access_token'        => "xxxxxxxxxxxxxx",
        'oauth_access_token_secret' => "xxxxxxxxxxxxxxxx",
        'consumer_key'              => "xxxxxxxxxxxxxxxxxxxxx",
        'consumer_secret'           => "xxxxxxxxxxxxxxxxxxxxxxx",

    );


    $url           = 'https://api.twitter.com/1.1/users/search.json';
    $getfield      = '?q=name to search';
    $requestMethod = 'GET';

    $twitter = new TwitterAPIExchange($settings);
    $users   = $twitter->setGetfield($getfield)
        ->buildOauth($url, $requestMethod)
        ->performRequest();

    $users = json_decode($users, true);
    // var_dump($users);die();
    for ($i = 0; $i <= 19; $i++) {
        $twitter           = new twitter_user();
        $twitter->user_id  = $users[$i]['id'];
        $twitter->name     = $users[$i]['name'];
        $twitter->sname    = $users[$i]['screen_name'];
        $twitter->location = $users[$i]['location'];
        $twitter->save();

    }

    echo "Data saved";
}

I am using this for loop till 19 because it's giving me 20 records I want to access all available records dynamically.

  • 写回答

1条回答 默认 最新

  • dr5779 2016-03-08 16:32
    关注
     public function full_Record($page){  
    
        $settings = array(
            'oauth_access_token' => "xxxxxxxxxxxxxxxxxxxxxxx",
            'oauth_access_token_secret' => "xxxxxxxxxxxxxxxxxxxx",
            'consumer_key' => "xxxxxxxxxxxxxxxxxxxxxxxxxxx",
            'consumer_secret' => "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
            );   
        $url = 'https://api.twitter.com/1.1/users/search.json';
        $getfield = '?q=john&page='. $page;
        $requestMethod = 'GET';
    
        $twitter = new TwitterAPIExchange($settings);
        $users =  $twitter->setGetfield($getfield)
                ->buildOauth($url, $requestMethod)
                ->performRequest();
    
        $users = json_decode($users,true);
        if(empty($users)){
    
    
            echo "Data saved less data available";
        }
        else{
    
                for($i=0;$i<=19;$i++){
    
                    $twitter=new twitter_user();
                    $twitter->user_id=$users[$i]['id'];
                    $twitter->name=$users[$i]['name'];
                    $twitter->sname=$users[$i]['screen_name'];
                    $twitter->location=$users[$i]['location'];
                    $twitter->save();
    
                }
    
                    $page++;
                    $this->full_Record($page);                  
    
                else{
                    echo "Data Saved ok";
                }
    
        }
    

    This Code works as per requirement

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在ISIS中什么是IP从地址
  • ¥15 压测时,并发量过高时,响应时间出现尖刺
  • ¥15 关于vmprotect3.8.4虚拟文件一项
  • ¥15 在不用IT调试的情况下怎样能连外网
  • ¥20 C#调用虚拟键盘TabTip.exe
  • ¥15 Qt4代码实现下面的界面
  • ¥15 CCS离散化传递函数与仿真不一致
  • ¥15 prism提示我reinstall prism 如何解决
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确