Skip to content

Commit 1a157c1

Browse files
committed
init
0 parents  commit 1a157c1

File tree

8 files changed

+294
-0
lines changed

8 files changed

+294
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor/
2+
/composer.phar
3+
composer.lock
4+
.idea

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Introduction
2+
3+
The official PHP library for using [RocketAPI](https://rocketapi.io).
4+
5+
## Installing
6+
7+
The recommended way to install RocketAPI is through [Composer](https://getcomposer.org/).
8+
9+
```bash
10+
# Install Composer
11+
curl -sS https://getcomposer.org/installer | php
12+
```
13+
14+
Next, run the Composer command to install the RocketAPI PHP Library:
15+
```bash
16+
composer require rocketapi/rocketapi
17+
```
18+
19+
After installing, you need to require Composer's autoloader:
20+
```php
21+
require 'vendor/autoload.php';
22+
use RocketAPI\InstagramAPI;
23+
```
24+
25+
## Usage
26+
27+
See the [documentation](https://docs.rocketapi.io) for more information.

composer.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "rocketapi/rocketapi",
3+
"description": "RocketAPI PHP SDK",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "RocketAPI"
8+
}
9+
],
10+
"require": {
11+
"ext-curl": "*",
12+
"ext-json": "*"
13+
},
14+
"autoload": {
15+
"psr-4": {
16+
"RocketAPI\\": "src/"
17+
}
18+
}
19+
}

examples.php

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

license.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 rocketapi
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

src/Exceptions.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
namespace RocketAPI;
3+
4+
class NotFoundException extends \Exception {}
5+
class BadResponseException extends \Exception {}

src/InstagramAPI.php

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
<?php
2+
namespace RocketAPI;
3+
4+
class InstagramAPI extends RocketAPI {
5+
public function __construct($token)
6+
{
7+
parent::__construct($token);
8+
}
9+
10+
/**
11+
* @throws NotFoundException
12+
* @throws BadResponseException
13+
*/
14+
protected function request($method, $data)
15+
{
16+
$response = parent::request($method, $data);
17+
if ($response['status'] == 'done') {
18+
if ($response['response']['status_code'] == 200 && $response['response']['content_type'] == 'application/json') {
19+
return $response['response']['body'];
20+
} else if ($response['response']['status_code'] == 404) {
21+
throw new NotFoundException('Instagram resource not found');
22+
} else {
23+
throw new BadResponseException('Bad response from Instagram');
24+
}
25+
} else {
26+
throw new BadResponseException('Bad response from RocketAPI');
27+
}
28+
}
29+
30+
/**
31+
* @throws NotFoundException
32+
* @throws BadResponseException
33+
*/
34+
public function search($query) {
35+
return $this->request('instagram/search', [
36+
'query' => $query,
37+
]);
38+
}
39+
40+
/**
41+
* @throws NotFoundException
42+
* @throws BadResponseException
43+
*/
44+
public function getUserInfo($username) {
45+
return $this->request('instagram/user/get_info', [
46+
'username' => $username,
47+
]);
48+
}
49+
50+
/**
51+
* @throws NotFoundException
52+
* @throws BadResponseException
53+
*/
54+
public function getUserInfoById($user_id) {
55+
return $this->request('instagram/user/get_info_by_id', [
56+
'id' => $user_id,
57+
]);
58+
}
59+
60+
/**
61+
* @throws NotFoundException
62+
* @throws BadResponseException
63+
*/
64+
public function getUserMedia($user_id, $count=12, $max_id=null) {
65+
$payload = ['id' => $user_id, 'count' => $count];
66+
if ($max_id) {
67+
$payload['max_id'] = $max_id;
68+
}
69+
return $this->request('instagram/user/get_media', $payload);
70+
}
71+
72+
/**
73+
* @throws NotFoundException
74+
* @throws BadResponseException
75+
*/
76+
public function getUserFollowing($user_id, $count=12, $max_id=null) {
77+
$payload = ['id' => $user_id, 'count' => $count];
78+
if ($max_id) {
79+
$payload['max_id'] = $max_id;
80+
}
81+
return $this->request('instagram/user/get_following', $payload);
82+
}
83+
84+
/**
85+
* @throws NotFoundException
86+
* @throws BadResponseException
87+
*/
88+
public function getUserFollowers($user_id, $count=12, $max_id=null)
89+
{
90+
$payload = ['id' => $user_id, 'count' => $count];
91+
if ($max_id) {
92+
$payload['max_id'] = $max_id;
93+
}
94+
return $this->request('instagram/user/get_followers', $payload);
95+
}
96+
97+
/**
98+
* @throws NotFoundException
99+
* @throws BadResponseException
100+
*/
101+
public function searchUserFollowers($user_id, $query) {
102+
return $this->request('instagram/user/get_followers', [
103+
'id' => $user_id,
104+
'query' => $query,
105+
]);
106+
}
107+
108+
/**
109+
* @throws NotFoundException
110+
* @throws BadResponseException
111+
*/
112+
public function getUserStoriesBulk($user_ids) {
113+
return $this->request('instagram/user/get_stories', [
114+
'ids' => $user_ids,
115+
]);
116+
}
117+
118+
/**
119+
* @throws NotFoundException
120+
* @throws BadResponseException
121+
*/
122+
public function getUserStories($user_id) {
123+
return $this->getUserStoriesBulk([$user_id]);
124+
}
125+
126+
/**
127+
* @throws NotFoundException
128+
* @throws BadResponseException
129+
*/
130+
public function getMediaInfo($media_id) {
131+
return $this->request('instagram/media/get_info', [
132+
'id' => $media_id,
133+
]);
134+
}
135+
136+
/**
137+
* @throws NotFoundException
138+
* @throws BadResponseException
139+
*/
140+
public function getMediaLikes($shortcode, $count=12, $max_id=null) {
141+
$payload = ['shortcode' => $shortcode, 'count' => $count];
142+
if ($max_id) {
143+
$payload['max_id'] = $max_id;
144+
}
145+
return $this->request('instagram/media/get_likes', $payload);
146+
}
147+
148+
/**
149+
* @throws NotFoundException
150+
* @throws BadResponseException
151+
*/
152+
public function getMediaComments($media_id, $can_support_threading=true, $min_id=null) {
153+
$payload = ['id' => $media_id, 'can_support_threading' => $can_support_threading];
154+
if ($min_id) {
155+
$payload['min_id'] = $min_id;
156+
}
157+
return $this->request('instagram/media/get_comments', $payload);
158+
}
159+
160+
/**
161+
* @throws NotFoundException
162+
* @throws BadResponseException
163+
*/
164+
public function getCommentLikes($comment_id, $max_id=null) {
165+
$payload = ['id' => $comment_id];
166+
if ($max_id) {
167+
$payload['max_id'] = $max_id;
168+
}
169+
return $this->request('instagram/comment/get_likes', $payload);
170+
}
171+
}

src/RocketAPI.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
namespace RocketAPI;
3+
4+
class RocketAPI {
5+
private $base_url = "https://v1.rocketapi.io/";
6+
private $token;
7+
private $max_timeout = 30;
8+
9+
public function __construct($token) {
10+
$this->token = $token;
11+
}
12+
13+
protected function request($method, $data) {
14+
$ch = curl_init();
15+
curl_setopt($ch, CURLOPT_URL, $this->base_url . $method);
16+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
17+
curl_setopt($ch, CURLOPT_POST, 1);
18+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
19+
curl_setopt($ch, CURLOPT_HTTPHEADER, [
20+
'Authorization: Token ' . $this->token,
21+
'Content-Type: application/json',
22+
]);
23+
curl_setopt($ch, CURLOPT_TIMEOUT, $this->max_timeout);
24+
$result = curl_exec($ch);
25+
curl_close($ch);
26+
return json_decode($result, true);
27+
}
28+
}

0 commit comments

Comments
 (0)