1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The MIT License (MIT) |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016 Haridarshan Gorana |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
15
|
|
|
* copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
23
|
|
|
* SOFTWARE. |
24
|
|
|
*/ |
25
|
|
|
namespace Haridarshan\Instagram; |
26
|
|
|
|
27
|
|
|
use Haridarshan\Instagram\Exceptions\InstagramRequestException; |
28
|
|
|
use Haridarshan\Instagram\Exceptions\InstagramResponseException; |
29
|
|
|
use Haridarshan\Instagram\Exceptions\InstagramThrottleException; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* InstagramRequest class |
33
|
|
|
* |
34
|
|
|
* @library instagram-php |
35
|
|
|
* |
36
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
37
|
|
|
* |
38
|
|
|
* @link http://github.com/haridarshan/instagram-php Class Documentation |
39
|
|
|
* @link http://instagram.com/developer/ API Documentation |
40
|
|
|
* |
41
|
|
|
* @author Haridarshan Gorana <[email protected]> |
42
|
|
|
* |
43
|
|
|
* @since May 09, 2016 |
44
|
|
|
* |
45
|
|
|
* @copyright Haridarshan Gorana |
46
|
|
|
* |
47
|
|
|
* @version 2.2.2 |
48
|
|
|
*/ |
49
|
|
|
class InstagramRequest |
50
|
|
|
{ |
51
|
|
|
/** @var string $path */ |
52
|
|
|
protected $path; |
53
|
|
|
|
54
|
|
|
/** @var array $params */ |
55
|
|
|
protected $params; |
56
|
|
|
|
57
|
|
|
/** @var string $method */ |
58
|
|
|
protected $method; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Remaining Rate Limit |
62
|
|
|
* Sandbox = 500 |
63
|
|
|
* Live = 5000 |
64
|
|
|
* |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected $xRateLimitRemaining = 500; |
68
|
|
|
|
69
|
|
|
/** @var InstagramResponse $response */ |
70
|
|
|
protected $response; |
71
|
|
|
|
72
|
|
|
/** @var Instagram $instagram */ |
73
|
|
|
protected $instagram; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Create the request and execute it to get the response |
77
|
|
|
* |
78
|
|
|
* @param Instagram $instagram |
79
|
|
|
* @param string $path |
80
|
|
|
* @param array $params |
81
|
|
|
* @param string $method |
82
|
|
|
*/ |
83
|
|
|
public function __construct(Instagram $instagram, $path, array $params = [], $method = 'GET') |
84
|
|
|
{ |
85
|
|
|
$this->instagram = $instagram; |
86
|
|
|
$this->path = $path; |
87
|
|
|
$this->params = $params; |
88
|
|
|
$this->method = $method; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Execute the Instagram Request |
93
|
|
|
* |
94
|
|
|
* @param void |
95
|
|
|
* |
96
|
|
|
* @throws InstagramResponseException |
97
|
|
|
* |
98
|
|
|
* @return InstagramResponse |
99
|
|
|
*/ |
100
|
|
|
protected function execute() |
101
|
|
|
{ |
102
|
|
|
$authMethod = '?access_token=' . $this->params['access_token']; |
103
|
|
|
$endpoint = Constants::API_VERSION . $this->path . (('GET' === $this->method) ? '?' . http_build_query($this->params) : $authMethod); |
104
|
|
|
$endpoint .= (strstr($endpoint, '?') ? '&' : '?') . 'sig=' . static::generateSignature($this->instagram->getApp(), $this->path, $this->params); |
105
|
|
|
|
106
|
|
|
$request = HelperFactory::getInstance()->request($this->instagram->getHttpClient(), $endpoint, $this->params, $this->method); |
107
|
|
|
|
108
|
|
|
if ($request === null) { |
109
|
|
|
throw new InstagramResponseException('400 Bad Request: instanceof InstagramResponse cannot be null', 400); |
110
|
|
|
} |
111
|
|
|
$this->response = new InstagramResponse($request); |
112
|
|
|
$this->xRateLimitRemaining = $this->response->getHeader('X-Ratelimit-Remaining'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Check Access Token is present. If not throw InstagramRequestException |
117
|
|
|
* |
118
|
|
|
* @throws InstagramRequestException |
119
|
|
|
*/ |
120
|
|
|
protected function isAccessTokenPresent() |
121
|
|
|
{ |
122
|
|
|
if (!isset($this->params['access_token'])) { |
123
|
|
|
throw new InstagramRequestException("{$this->path} - api requires an authenticated users access token.", 400); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get Response |
129
|
|
|
* |
130
|
|
|
* @return InstagramResponse |
131
|
|
|
*/ |
132
|
|
|
public function getResponse() |
133
|
|
|
{ |
134
|
|
|
$this->isRateLimitReached(); |
135
|
|
|
$this->isAccessTokenPresent(); |
136
|
|
|
$oauth = $this->instagram->getOAuth(); |
137
|
|
|
|
138
|
|
|
if (!$oauth->isAccessTokenSet()) { |
139
|
|
|
$oauth->setAccessToken($this->params['access_token']); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$this->execute(); |
143
|
|
|
|
144
|
|
|
return $this->response; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Check whether api rate limit is reached or not |
149
|
|
|
* |
150
|
|
|
* @throws InstagramThrottleException |
151
|
|
|
*/ |
152
|
|
|
private function isRateLimitReached() |
153
|
|
|
{ |
154
|
|
|
if (!$this->getRateLimit()) { |
155
|
|
|
throw new InstagramThrottleException('400 Bad Request : You have reached Instagram API Rate Limit', 400); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Secure API Request by using endpoint, paramters and API secret |
161
|
|
|
* |
162
|
|
|
* @see https://www.instagram.com/developer/secure-api-requests/ |
163
|
|
|
* |
164
|
|
|
* @param InstagramApp $app |
165
|
|
|
* @param string $endpoint |
166
|
|
|
* @param array $params |
167
|
|
|
* |
168
|
|
|
* @return string (Signature) |
169
|
|
|
*/ |
170
|
|
|
public static function generateSignature(InstagramApp $app, $endpoint, $params) |
171
|
|
|
{ |
172
|
|
|
$signature = $endpoint; |
173
|
|
|
ksort($params); |
174
|
|
|
foreach ($params as $key => $value) { |
175
|
|
|
$signature .= "|$key=$value"; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return hash_hmac('sha256', $signature, $app->getSecret(), false); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get Api Rate Limit |
183
|
|
|
* |
184
|
|
|
* @return int |
185
|
|
|
*/ |
186
|
|
|
public function getRateLimit() |
187
|
|
|
{ |
188
|
|
|
return $this->xRateLimitRemaining; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|