使用PHP创建twitter的开发者账号

获得access_token

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
// 创建一个 Guzzle HTTP 客户端
$client = new GuzzleHttp\Client();

// 推特开发者账号和应用的 API 密钥
$api_key = "your_api_key";
$api_secret = "your_api_secret";

// 使用用户名和密码来获取 request token
$response = $client->post('https://api.twitter.com/oauth/request_token', [
'auth' => [$api_key, $api_secret],
'form_params' => [
'x_auth_mode' => 'client_auth',
'x_auth_username' => 'your_username',
'x_auth_password' => 'your_password',
],
]);

// 解析返回的 request token
parse_str($response->getBody()->getContents(), $request_token);

// 拼接授权链接
$authorize_url = "https://api.twitter.com/oauth/authorize?oauth_token={$request_token['oauth_token']}";

// 打印授权链接,供用户点击授权
echo "Please visit the following URL to authorize your account:\n";
echo "$authorize_url\n";

// 读取授权码
echo "Enter the PIN code: ";
$pin_code = trim(fgets(STDIN));

// 使用授权码和 request token 来获取 access token
$response = $client->post('https://api.twitter.com/oauth/access_token', [
'auth' => [$api_key, $api_secret],
'form_params' => [
'oauth_verifier' => $pin_code,
'oauth_token' => $request_token['oauth_token'],
],
]);

// 解析返回的 access token
parse_str($response->getBody()->getContents(), $access_token);

// 打印 access token
echo "Your access token:\n";
echo "oauth_token: {$access_token['oauth_token']}\n";
echo "oauth_token_secret: {$access_token['oauth_token_secret']}\n";


获得账户信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
// 使用推特 API 来获取用户信息
function getUserInfo($screen_name) {
$api_key = "your_api_key";
$api_secret = "your_api_secret";
$access_token = "your_access_token";
$access_token_secret = "your_access_token_secret";

// 拼接 API 请求地址
$url = "https://api.twitter.com/1.1/users/show.json?screen_name=$screen_name";

// 使用 API 密钥和令牌来初始化 Twitter API 客户端
$client = new TwitterAPIExchange($settings);

// 调用 API 接口,获取用户信息
$response = $client->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();

// 将返回的 JSON 数据解析为 PHP 对象
$user = json_decode($response);

// 返回用户 ID
return $user->id;
}

// 跟踪用户 ID 的变化
function trackUserID($screen_name) {
// 获取用户初始的 ID
$initial_id = getUserInfo($screen_name);

// 每隔一定时间段,检查用户 ID 是否发生变化
while (true) {
sleep(60); // 等待 60 秒

// 获取当前的用户 ID
$current_id = getUserInfo($screen_name);

// 如果用户 ID 发生了变化,打印日志
if ($current_id != $initial_id) {
echo "User ID changed from $initial_id to $current_id.\n";

// 更新初始 ID
$initial_id = $current_id;
}
}
}

推特授权登录

1
composer require abraham/twitteroauth
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// 引入 OAuth 库
require 'vendor/autoload.php';
use Abraham\TwitterOAuth\TwitterOAuth;

// 定义消费者密钥和消费者密钥密码
define('CONSUMER_KEY', 'YOUR_CONSUMER_KEY');
define('CONSUMER_SECRET', 'YOUR_CONSUMER_SECRET');

// 定义回调 URL
define('CALLBACK_URL', 'YOUR_CALLBACK_URL');

// 创建 OAuth 客户端对象
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET);

// 获取 request token
$request_token = $connection->oauth('oauth/request_token', array('oauth_callback' => CALLBACK_URL));

// 获取 request token 的字符串形式
$request_token_str = $request_token['oauth_token'];

// 将 request token 存储到 session 中
session_start();
$_SESSION['oauth_token'] = $request_token_str;
$_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];

// 获取授权页面的 URL
$url = $connection->url('oauth/authorize', array('oauth_token' => $request_token_str));

// 在浏览器中打开授权页面
echo "Please visit this URL to authorize your app: $url\n";

// 等待用户完成授权
echo "Enter the PIN number: ";

// 获取用户输入的 PIN 码
$pin = trim(fgets(STDIN));

// 使用 request token 和 PIN 码,获取 access token
$access_token = $connection->oauth('oauth/access_token', array('oauth_verifier' => $pin, 'oauth_token' => $request_token_str));

// 创建 OAuth 客户端对象,并使用 access token
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

$friends = $connection->get('friends/list', array('count' => 200));

// 遍历用户列表,打印用户信息
foreach ($friends->users as $user) {
echo "ID: $user->id\n";
echo "Name: $user->name\n";
echo "Username: $user->screen_name\n";
echo "Location: $user->location\n";
echo "Description: $user->description\n";
echo "Followers: $user->followers_count\n";
echo "Following: $user->friends_count\n";
echo "Statuses: $user->statuses_count\n";
echo "Listed: $user->listed_count\n";
echo "Created at: $user->created_at\n";
echo "Favourites: $user->favourites_count\n";
echo "URL: $user->url\n";
echo "Verified: $user->verified\n";
echo "\n";
}


使用PHP创建twitter的开发者账号
https://www.ggss.club/2022/12/08/twitter-php/
作者
Nicol Li
发布于
2022年12月8日
许可协议