【问题标题】:How to use Twitter API pagination for getting user tweets如何使用 Twitter API 分页来获取用户推文
【发布时间】:2016-10-12 04:12:59
【问题描述】:

我正在获取用户的推文。我需要显示所有带有 ajax 分页的推文。我怎样才能做到这一点?

https://api.twitter.com/1.1/statuses/user_timeline.json

试过

我正在使用上面的链接。我听说过max_idsince_id,但不知道如何使用。我试过max_idsince_id,然后收集重复。我没有得到任何光标响应。

我的代码

$api_key = urlencode('*********'); // Consumer Key (API Key)
$api_secret = urlencode('***********'); // Consumer Secret (API Secret)
$auth_url = 'https://api.twitter.com/oauth2/token';

    // what we want?
    $data_username = '********'; // username
    $data_count = 1; // number of tweets
    $data_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';

    // get api access token
    $api_credentials = base64_encode($api_key . ':' . $api_secret);

    $auth_headers = 'Authorization: Basic ' . $api_credentials . "\r\n" .
            'Content-Type: application/x-www-form-urlencoded;charset=UTF-8' . "\r\n";

    $auth_context = stream_context_create(
            array(
                'http' => array(
                    'header' => $auth_headers,
                    'method' => 'POST',
                    'content' => http_build_query(array('grant_type' => 'client_credentials',)),
                )
            )
    );

    $auth_response = json_decode(file_get_contents($auth_url, 0, $auth_context), true);
    $auth_token = $auth_response['access_token'];

    // get tweets
    $data_context = stream_context_create(array('http' => array('header' => 'Authorization: Bearer ' . $auth_token . "\r\n",)));

    $datas = json_decode(file_get_contents($data_url . '?include_rts=true&count=' . $data_count . '&screen_name=' . urlencode($data_username), 0, $data_context), true);

    // result - do what you want
    print('<pre>');
    print_r($datas);

问题

我必须传递哪些值才能获取分页 url?

谢谢。

【问题讨论】:

  • 请发布您的示例代码。
  • 任何人都知道解决方案吗?

标签: php json twitter twitter-oauth


【解决方案1】:

Twitter API 响应应包含一个 next_token 变量,该变量可用于后续 API 调用/分页。

有关该主题的更多信息,请参阅https://developer.twitter.com/en/docs/twitter-api/pagination(这是指 v2,因为 v1.1 现已弃用)

【讨论】:

    猜你喜欢
    • 2015-02-21
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 2016-05-18
    • 2015-05-15
    相关资源
    最近更新 更多