52
loading...
This website collects cookies to deliver better user experience
["detail"]=>
string(201) "When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project. You can create a project via the developer portal."
["registration_url"]=>
string(55) "https://developer.twitter.com/en/docs/projects/overview"
// ︙
User | Application | |
---|---|---|
Request Limit | 180 / 15min | 450 / 15min |
Tweets per once | 10-100 | 10-100 |
<?php
require_once "vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$connection = new TwitterOAuth(
"************************", // API Key
"************************", // API Secret
null,
"************************" // Bearer Token
);
$query = "#keyword";
// Set API version
$connection->setApiVersion("2");
$params = [
/*
- Reference:https://developer.twitter.com/en/docs/twitter-api/tweets/search/api-reference/get-tweets-search-recent
- query and max_result are required
*/
"query" => $query . " -is:retweet", // if you exclute retweets
"start_time" => "2021-12-12T00:00:00", // UTC
"end_time" => "2021-12-03T00:00:00",
"tweet.fields" => "created_at", // additional posting date and time
"max_results" => 100 // set between 10-100
];
// Set the number of requests to $req_count
// set the number of requests to $loop_count
$req_count = 0;
$loop_count = 400;
for ($i = 0; $i < $loop_count; $i++) {
/**
* When request is limited
* We have set the time longer than 15 minutes and shorter than 450 times to allow for this.
* Please take care of these issues on your own.
*/
if($req_count > 400){
echo "Wait for 15 minutes";
$loop_count = 0;
sleep(1000000);
}
// Request using v2 API
$request = $connection->get("tweets/search/recent", $params);
// Process the acquired information
foreach ($request->data as $data) {
// Your code here
}
echo "Success Request";
// If you have more than 100 cases
if (isset($request->meta->next_token)) {
echo $req_count;
$req_count++;
// Add next_token to the query parameter array
$params["next_token"] = $request->meta->next_token;
} else {
// If not, quit
break;
}
}
<?php
require "vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
function tweet_loop($keyword, $loop_count)
{
global $CK, $CS, $AT, $ATS;
$connection = new TwitterOAuth($CK, $CS, $AT, $ATS);
$params = [
'q' => $keyword,
'count' => 100
];
for ($i = 0; $i < $loop_count; $i++) {
$results = $connection->get("search/tweets", $params);
foreach ($results->statuses as $val) {
// Write the post-acquisition process here
$tweet_results[] = $val->text;
}
//Conditional branching to see if there are more tweets that can be retrieved
if (isset($results->search_metadata->next_results)) {
// Get max_id
$max_id = preg_replace('/.*?max_id=([\d]+)&.*/', '$1', $results->search_metadata->next_results);
// Add max_id to params
$options['max_id'] = $max_id;
} else {
break;
}
}
return $tweet_results;
}