How to get a count of followers from Twitter API and trendline

后端 未结 3 1325
鱼传尺愫
鱼传尺愫 2021-02-12 17:28

I am in the process of writing some reports for the number of followers over time for Twitter, however after substantial searches and trial and error, I have not being able to g

3条回答
  •  面向向阳花
    2021-02-12 18:23

    Here is a simple PHP exemple using CURL, with no library involved, to get the followers_count of a selected profile (here @TwitterFrance) using v2 API and the bearer token (bearer token is some kind of simplified method to access public data through an OAuth 2.0 API)

    $authorization = "Authorization: Bearer YOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEARERYOUREXTRALONGBEAR";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, "https://api.twitter.com/2/users/by/username/TwitterFrance?user.fields=public_metrics");
    $result = curl_exec($ch);
    curl_close($ch);
    if (is_string($result)) {
        echo (json_decode($result)->data->public_metrics->followers_count);
        die();
    }
    

提交回复
热议问题