Skip to content

Commit b70874b

Browse files
committed
🔥 Threads API added, 1.0.5
1 parent 8366b98 commit b70874b

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed

examples_threads.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
require('src/RocketAPI.php');
3+
require('src/Exceptions/NotFoundException.php');
4+
require('src/Exceptions/BadResponseException.php');
5+
require('src/ThreadsAPI.php');
6+
7+
use RocketAPI\ThreadsAPI;
8+
9+
$api = new ThreadsAPI("put your token here");
10+
11+
// Get user feed by user id
12+
$user_id = 35670846775;
13+
try {
14+
$user = $api->getUserFeed($user_id);
15+
print_r($user);
16+
} catch (RocketAPI\Exceptions\NotFoundException $e) {
17+
echo "User $user_id not found\n";
18+
} catch (RocketAPI\Exceptions\BadResponseException $e) {
19+
echo "Can't get $user_id feed from API\n";
20+
}

src/ThreadsAPI.php

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
namespace RocketAPI;
3+
4+
class ThreadsAPI extends RocketAPI
5+
{
6+
public function __construct($token)
7+
{
8+
parent::__construct($token);
9+
}
10+
11+
/**
12+
* @throws Exceptions\NotFoundException
13+
* @throws Exceptions\BadResponseException
14+
*/
15+
protected function request($method, $data)
16+
{
17+
$response = parent::request($method, $data);
18+
if ($response['status'] == 'done') {
19+
if ($response['response']['status_code'] == 200 && $response['response']['content_type'] == 'application/json') {
20+
return $response['response']['body'];
21+
} else if ($response['response']['status_code'] == 404) {
22+
throw new Exceptions\NotFoundException('Threads resource not found');
23+
} else {
24+
throw new Exceptions\BadResponseException('Bad response from Threads');
25+
}
26+
} else {
27+
throw new Exceptions\BadResponseException('Bad response from RocketAPI');
28+
}
29+
}
30+
31+
/**
32+
* @throws Exceptions\NotFoundException
33+
* @throws Exceptions\BadResponseException
34+
*/
35+
public function search_users($query, $rank_token=null, $page_token=null) {
36+
$payload = [
37+
'query' => $query,
38+
];
39+
if ($rank_token) {
40+
$payload['rank_token'] = $rank_token;
41+
}
42+
if ($page_token) {
43+
$payload['page_token'] = $page_token;
44+
}
45+
return $this->request('threads/search_users', $payload);
46+
}
47+
48+
/**
49+
* @throws Exceptions\NotFoundException
50+
* @throws Exceptions\BadResponseException
51+
*/
52+
public function getUserInfo($user_id) {
53+
return $this->request('threads/user/get_info', [
54+
'id' => $user_id,
55+
]);
56+
}
57+
58+
/**
59+
* @throws Exceptions\NotFoundException
60+
* @throws Exceptions\BadResponseException
61+
*/
62+
public function getUserFeed($user_id, $max_id=null) {
63+
$payload = [
64+
'id' => $user_id,
65+
];
66+
if ($max_id) {
67+
$payload['max_id'] = $max_id;
68+
}
69+
return $this->request('threads/user/get_feed', $payload);
70+
}
71+
72+
/**
73+
* @throws Exceptions\NotFoundException
74+
* @throws Exceptions\BadResponseException
75+
*/
76+
public function getUserReplies($user_id, $max_id=null) {
77+
$payload = [
78+
'id' => $user_id,
79+
];
80+
if ($max_id) {
81+
$payload['max_id'] = $max_id;
82+
}
83+
return $this->request('threads/user/get_replies', $payload);
84+
}
85+
86+
/**
87+
* @throws Exceptions\NotFoundException
88+
* @throws Exceptions\BadResponseException
89+
*/
90+
public function getUserFollowers($user_id, $max_id=null) {
91+
$payload = ['id' => $user_id];
92+
if ($max_id) {
93+
$payload['max_id'] = $max_id;
94+
}
95+
return $this->request('threads/user/get_followers', $payload);
96+
}
97+
98+
/**
99+
* @throws Exceptions\NotFoundException
100+
* @throws Exceptions\BadResponseException
101+
*/
102+
public function getUserFollowing($user_id, $max_id=null)
103+
{
104+
$payload = ['id' => $user_id];
105+
if ($max_id) {
106+
$payload['max_id'] = $max_id;
107+
}
108+
return $this->request('threads/user/get_following', $payload);
109+
}
110+
111+
/**
112+
* @throws Exceptions\NotFoundException
113+
* @throws Exceptions\BadResponseException
114+
*/
115+
public function searchUserFollowers($user_id, $query) {
116+
return $this->request('threads/user/get_followers', [
117+
'id' => $user_id,
118+
'query' => $query,
119+
]);
120+
}
121+
122+
/**
123+
* @throws Exceptions\NotFoundException
124+
* @throws Exceptions\BadResponseException
125+
*/
126+
public function searchUserFollowing($user_id, $query) {
127+
return $this->request('threads/user/get_following', [
128+
'id' => $user_id,
129+
'query' => $query,
130+
]);
131+
}
132+
133+
/**
134+
* @throws Exceptions\NotFoundException
135+
* @throws Exceptions\BadResponseException
136+
*/
137+
public function getThreadReplies($thread_id, $max_id=null) {
138+
$payload = ['id' => $thread_id];
139+
if ($max_id) {
140+
$payload['max_id'] = $max_id;
141+
}
142+
return $this->request('threads/thread/get_replies', $payload);
143+
}
144+
145+
/**
146+
* @throws Exceptions\NotFoundException
147+
* @throws Exceptions\BadResponseException
148+
*/
149+
public function getThreadLikes($thread_id) {
150+
$payload = ['id' => $thread_id];
151+
return $this->request('threads/thread/get_likes', $payload);
152+
}
153+
}

0 commit comments

Comments
 (0)