@@ -3,13 +3,13 @@ |
||
3 | 3 | |
4 | 4 | class InstagramException extends \Exception |
5 | 5 | { |
6 | - /* |
|
6 | + /* |
|
7 | 7 | * Get Exception type |
8 | 8 | * @return string | nul; |
9 | 9 | */ |
10 | - public function getType() |
|
11 | - { |
|
12 | - $message = json_decode($this->message); |
|
13 | - return isset($message->Type) ? $message->Type : null; |
|
14 | - } |
|
10 | + public function getType() |
|
11 | + { |
|
12 | + $message = json_decode($this->message); |
|
13 | + return isset($message->Type) ? $message->Type : null; |
|
14 | + } |
|
15 | 15 | } |
@@ -9,30 +9,30 @@ discard block |
||
9 | 9 | |
10 | 10 | class InstagramRequest |
11 | 11 | { |
12 | - /** @var string $path */ |
|
13 | - private $path; |
|
12 | + /** @var string $path */ |
|
13 | + private $path; |
|
14 | 14 | |
15 | - /** @var array $params */ |
|
16 | - private $params; |
|
15 | + /** @var array $params */ |
|
16 | + private $params; |
|
17 | 17 | |
18 | - /** @var string $method */ |
|
19 | - private $method; |
|
18 | + /** @var string $method */ |
|
19 | + private $method; |
|
20 | 20 | |
21 | - /* |
|
21 | + /* |
|
22 | 22 | * Remaining Rate Limit |
23 | 23 | * Sandbox = 500 |
24 | 24 | * Live = 5000 |
25 | 25 | * @var array $x_rate_limit_remaining |
26 | 26 | */ |
27 | - private $xRateLimitRemaining = 500; |
|
27 | + private $xRateLimitRemaining = 500; |
|
28 | 28 | |
29 | - /** @var InstagramResponse $response */ |
|
30 | - protected $response; |
|
29 | + /** @var InstagramResponse $response */ |
|
30 | + protected $response; |
|
31 | 31 | |
32 | - /** @var Instagram $instagram */ |
|
33 | - protected $instagram; |
|
32 | + /** @var Instagram $instagram */ |
|
33 | + protected $instagram; |
|
34 | 34 | |
35 | - /* |
|
35 | + /* |
|
36 | 36 | * Create the request and execute it to get the response |
37 | 37 | * @param Instagram $instagram |
38 | 38 | * @param string $path |
@@ -41,68 +41,68 @@ discard block |
||
41 | 41 | * |
42 | 42 | * @return InstagramResponse |
43 | 43 | */ |
44 | - public function __construct(Instagram $instagram, $path, array $params = array(), $method = 'GET') |
|
45 | - { |
|
46 | - $this->instagram = $instagram; |
|
47 | - $this->path = $path; |
|
48 | - $this->params = $params; |
|
49 | - $this->method = $method; |
|
50 | - } |
|
44 | + public function __construct(Instagram $instagram, $path, array $params = array(), $method = 'GET') |
|
45 | + { |
|
46 | + $this->instagram = $instagram; |
|
47 | + $this->path = $path; |
|
48 | + $this->params = $params; |
|
49 | + $this->method = $method; |
|
50 | + } |
|
51 | 51 | |
52 | - /* |
|
52 | + /* |
|
53 | 53 | * Execute the Instagram Request |
54 | 54 | * @param void |
55 | 55 | * @return InstagramResponse |
56 | 56 | */ |
57 | - protected function execute() |
|
58 | - { |
|
59 | - $this->isRateLimitReached(); |
|
60 | - $this->isAccessTokenPresent(); |
|
61 | - $oauth = $this->instagram->getOAuth(); |
|
62 | - if (!$oauth->isAccessTokenSet()) { |
|
63 | - $oauth->setAccessToken($this->params['access_token']); |
|
64 | - } |
|
65 | - $authentication_method = '?access_token='.$this->params['access_token']; |
|
66 | - $endpoint = Constants::API_VERSION.$this->path.(('GET' === $this->method) ? '?'.http_build_query($this->params) : $authentication_method); |
|
67 | - $endpoint .= (strstr($endpoint, '?') ? '&' : '?').'sig='.static::generateSignature($this->instagram->getClientSecret(), $this->path, $this->params); |
|
57 | + protected function execute() |
|
58 | + { |
|
59 | + $this->isRateLimitReached(); |
|
60 | + $this->isAccessTokenPresent(); |
|
61 | + $oauth = $this->instagram->getOAuth(); |
|
62 | + if (!$oauth->isAccessTokenSet()) { |
|
63 | + $oauth->setAccessToken($this->params['access_token']); |
|
64 | + } |
|
65 | + $authentication_method = '?access_token='.$this->params['access_token']; |
|
66 | + $endpoint = Constants::API_VERSION.$this->path.(('GET' === $this->method) ? '?'.http_build_query($this->params) : $authentication_method); |
|
67 | + $endpoint .= (strstr($endpoint, '?') ? '&' : '?').'sig='.static::generateSignature($this->instagram->getClientSecret(), $this->path, $this->params); |
|
68 | 68 | |
69 | - $this->response = new InstagramResponse(HelperFactory::request($this->instagram->getHttpClient(), $endpoint, $this->params, $this->method)); |
|
70 | - $this->xRateLimitRemaining = $this->response->getHeader('X-Ratelimit-Remaining'); |
|
71 | - } |
|
69 | + $this->response = new InstagramResponse(HelperFactory::request($this->instagram->getHttpClient(), $endpoint, $this->params, $this->method)); |
|
70 | + $this->xRateLimitRemaining = $this->response->getHeader('X-Ratelimit-Remaining'); |
|
71 | + } |
|
72 | 72 | |
73 | - /* |
|
73 | + /* |
|
74 | 74 | * Check Access Token is present. If not throw InstagramRequestException |
75 | 75 | * @throws InstagramRequestException |
76 | 76 | */ |
77 | - protected function isAccessTokenPresent() |
|
78 | - { |
|
79 | - if (!isset($this->params['access_token'])) { |
|
80 | - throw new InstagramRequestException("{$this->path} - api requires an authenticated users access token.", 400); |
|
81 | - } |
|
82 | - } |
|
77 | + protected function isAccessTokenPresent() |
|
78 | + { |
|
79 | + if (!isset($this->params['access_token'])) { |
|
80 | + throw new InstagramRequestException("{$this->path} - api requires an authenticated users access token.", 400); |
|
81 | + } |
|
82 | + } |
|
83 | 83 | |
84 | - /* |
|
84 | + /* |
|
85 | 85 | * Get Response |
86 | 86 | * @return InstagramResponse |
87 | 87 | */ |
88 | - public function getResponse() |
|
89 | - { |
|
90 | - $this->execute(); |
|
91 | - return $this->response; |
|
92 | - } |
|
88 | + public function getResponse() |
|
89 | + { |
|
90 | + $this->execute(); |
|
91 | + return $this->response; |
|
92 | + } |
|
93 | 93 | |
94 | - /* |
|
94 | + /* |
|
95 | 95 | * Check whether api rate limit is reached or not |
96 | 96 | * @throws InstagramThrottleException |
97 | 97 | */ |
98 | - private function isRateLimitReached() |
|
99 | - { |
|
100 | - if (!$this->getRateLimit()) { |
|
101 | - throw new InstagramThrottleException("400 Bad Request : You have reached Instagram API Rate Limit", 400); |
|
102 | - } |
|
103 | - } |
|
98 | + private function isRateLimitReached() |
|
99 | + { |
|
100 | + if (!$this->getRateLimit()) { |
|
101 | + throw new InstagramThrottleException("400 Bad Request : You have reached Instagram API Rate Limit", 400); |
|
102 | + } |
|
103 | + } |
|
104 | 104 | |
105 | - /* |
|
105 | + /* |
|
106 | 106 | * Secure API Request by using endpoint, paramters and API secret |
107 | 107 | * copy from Instagram API Documentation: https://www.instagram.com/developer/secure-api-requests/ |
108 | 108 | * |
@@ -112,21 +112,21 @@ discard block |
||
112 | 112 | * |
113 | 113 | * @return string (Signature) |
114 | 114 | */ |
115 | - public static function generateSignature($secret, $endpoint, $params) |
|
116 | - { |
|
117 | - $signature = $endpoint; |
|
118 | - ksort($params); |
|
119 | - foreach ($params as $key => $value) { |
|
120 | - $signature .= "|$key=$value"; |
|
121 | - } |
|
122 | - return hash_hmac('sha256', $signature, $secret, false); |
|
123 | - } |
|
115 | + public static function generateSignature($secret, $endpoint, $params) |
|
116 | + { |
|
117 | + $signature = $endpoint; |
|
118 | + ksort($params); |
|
119 | + foreach ($params as $key => $value) { |
|
120 | + $signature .= "|$key=$value"; |
|
121 | + } |
|
122 | + return hash_hmac('sha256', $signature, $secret, false); |
|
123 | + } |
|
124 | 124 | |
125 | - /* |
|
125 | + /* |
|
126 | 126 | * @return int |
127 | 127 | */ |
128 | - public function getRateLimit() |
|
129 | - { |
|
130 | - return $this->xRateLimitRemaining; |
|
131 | - } |
|
128 | + public function getRateLimit() |
|
129 | + { |
|
130 | + return $this->xRateLimitRemaining; |
|
131 | + } |
|
132 | 132 | } |
@@ -3,18 +3,18 @@ |
||
3 | 3 | |
4 | 4 | class Constants |
5 | 5 | { |
6 | - /** Library Version */ |
|
7 | - const VERSION = '2.0'; |
|
6 | + /** Library Version */ |
|
7 | + const VERSION = '2.0'; |
|
8 | 8 | |
9 | - /** API End Point */ |
|
10 | - const API_VERSION = 'v1'; |
|
9 | + /** API End Point */ |
|
10 | + const API_VERSION = 'v1'; |
|
11 | 11 | |
12 | - /** API End Point */ |
|
13 | - const API_HOST = 'https://api.instagram.com/'; |
|
12 | + /** API End Point */ |
|
13 | + const API_HOST = 'https://api.instagram.com/'; |
|
14 | 14 | |
15 | - /** OAuth2.0 Authorize API End Point */ |
|
16 | - const API_AUTH = 'oauth/authorize'; |
|
15 | + /** OAuth2.0 Authorize API End Point */ |
|
16 | + const API_AUTH = 'oauth/authorize'; |
|
17 | 17 | |
18 | - /** OAuth Access Token API End Point */ |
|
19 | - const API_TOKEN = 'oauth/access_token'; |
|
18 | + /** OAuth Access Token API End Point */ |
|
19 | + const API_TOKEN = 'oauth/access_token'; |
|
20 | 20 | } |
@@ -21,199 +21,199 @@ |
||
21 | 21 | |
22 | 22 | class Instagram |
23 | 23 | { |
24 | - /** @var string */ |
|
25 | - private $clientId; |
|
24 | + /** @var string */ |
|
25 | + private $clientId; |
|
26 | 26 | |
27 | - /** @var string */ |
|
28 | - private $clientSecret; |
|
27 | + /** @var string */ |
|
28 | + private $clientSecret; |
|
29 | 29 | |
30 | - /** @var string */ |
|
31 | - private $callbackUrl; |
|
30 | + /** @var string */ |
|
31 | + private $callbackUrl; |
|
32 | 32 | |
33 | - /** @var array<string> */ |
|
34 | - private $defaultScopes = array("basic", "public_content", "follower_list", "comments", "relationships", "likes"); |
|
33 | + /** @var array<string> */ |
|
34 | + private $defaultScopes = array("basic", "public_content", "follower_list", "comments", "relationships", "likes"); |
|
35 | 35 | |
36 | - /** @var array<string> */ |
|
37 | - private $scopes = array(); |
|
36 | + /** @var array<string> */ |
|
37 | + private $scopes = array(); |
|
38 | 38 | |
39 | - /* |
|
39 | + /* |
|
40 | 40 | * Random string indicating the state to prevent spoofing |
41 | 41 | * @var string |
42 | 42 | */ |
43 | - private $state; |
|
43 | + private $state; |
|
44 | 44 | |
45 | - /** @var \GuzzleHttp\Client $client */ |
|
46 | - protected $client; |
|
45 | + /** @var \GuzzleHttp\Client $client */ |
|
46 | + protected $client; |
|
47 | 47 | |
48 | - /** @var object $oauthResponse */ |
|
49 | - private $oauthResponse; |
|
48 | + /** @var object $oauthResponse */ |
|
49 | + private $oauthResponse; |
|
50 | 50 | |
51 | - /* |
|
51 | + /* |
|
52 | 52 | * Default Constructor |
53 | 53 | * Instagram Configuration Data |
54 | 54 | * @param array|object|string $config |
55 | 55 | */ |
56 | - public function __construct($config) |
|
57 | - { |
|
58 | - if (is_array($config)) { |
|
59 | - $this->setClientId($config['ClientId']); |
|
60 | - $this->setClientSecret($config['ClientSecret']); |
|
61 | - $this->setCallbackUrl($config['Callback']); |
|
62 | - $this->state = isset($config['State']) ? $config['State'] : substr(md5(rand()), 0, 7); |
|
63 | - } else { |
|
64 | - throw new InstagramException('Invalid Instagram Configuration data', 400); |
|
65 | - } |
|
66 | - $this->client = HelperFactory::client(Constants::API_HOST); |
|
67 | - } |
|
68 | - |
|
69 | - /* |
|
56 | + public function __construct($config) |
|
57 | + { |
|
58 | + if (is_array($config)) { |
|
59 | + $this->setClientId($config['ClientId']); |
|
60 | + $this->setClientSecret($config['ClientSecret']); |
|
61 | + $this->setCallbackUrl($config['Callback']); |
|
62 | + $this->state = isset($config['State']) ? $config['State'] : substr(md5(rand()), 0, 7); |
|
63 | + } else { |
|
64 | + throw new InstagramException('Invalid Instagram Configuration data', 400); |
|
65 | + } |
|
66 | + $this->client = HelperFactory::client(Constants::API_HOST); |
|
67 | + } |
|
68 | + |
|
69 | + /* |
|
70 | 70 | * Make URLs for user browser navigation |
71 | 71 | * @param string $path |
72 | 72 | * @param array $parameters |
73 | 73 | * @return string |
74 | 74 | */ |
75 | - public function getLoginUrl(array $parameters) |
|
76 | - { |
|
77 | - if (!isset($parameters['scope'])) { |
|
78 | - throw new InstagramException("Missing or Invalid Scope permission used", 400); |
|
79 | - } |
|
80 | - if (count(array_diff($parameters['scope'], $this->defaultScopes)) === 0) { |
|
81 | - $this->scopes = $parameters['scope']; |
|
82 | - } else { |
|
83 | - throw new InstagramException("Missing or Invalid Scope permission used", 400); |
|
84 | - } |
|
85 | - $query = 'client_id='.$this->getClientId().'&redirect_uri='.urlencode($this->getCallbackUrl()).'&response_type=code&state='.$this->state; |
|
86 | - $query .= isset($this->scopes) ? '&scope='.urlencode(str_replace(",", " ", implode(",", $parameters['scope']))) : ''; |
|
87 | - return sprintf('%s%s?%s', Constants::API_HOST, Constants::API_AUTH, $query); |
|
88 | - } |
|
89 | - |
|
90 | - /* |
|
75 | + public function getLoginUrl(array $parameters) |
|
76 | + { |
|
77 | + if (!isset($parameters['scope'])) { |
|
78 | + throw new InstagramException("Missing or Invalid Scope permission used", 400); |
|
79 | + } |
|
80 | + if (count(array_diff($parameters['scope'], $this->defaultScopes)) === 0) { |
|
81 | + $this->scopes = $parameters['scope']; |
|
82 | + } else { |
|
83 | + throw new InstagramException("Missing or Invalid Scope permission used", 400); |
|
84 | + } |
|
85 | + $query = 'client_id='.$this->getClientId().'&redirect_uri='.urlencode($this->getCallbackUrl()).'&response_type=code&state='.$this->state; |
|
86 | + $query .= isset($this->scopes) ? '&scope='.urlencode(str_replace(",", " ", implode(",", $parameters['scope']))) : ''; |
|
87 | + return sprintf('%s%s?%s', Constants::API_HOST, Constants::API_AUTH, $query); |
|
88 | + } |
|
89 | + |
|
90 | + /* |
|
91 | 91 | * Get the Oauth Access Token of a user from callback code |
92 | 92 | * @param string $code - Oauth2 Code returned with callback url after successfull login |
93 | 93 | * @return InstagramOAuth |
94 | 94 | */ |
95 | - public function oauth($code) |
|
96 | - { |
|
97 | - $options = array( |
|
98 | - "grant_type" => "authorization_code", |
|
99 | - "client_id" => $this->getClientId(), |
|
100 | - "client_secret" => $this->getClientSecret(), |
|
101 | - "redirect_uri" => $this->getCallbackUrl(), |
|
102 | - "code" => $code, |
|
103 | - "state" => $this->state |
|
104 | - ); |
|
95 | + public function oauth($code) |
|
96 | + { |
|
97 | + $options = array( |
|
98 | + "grant_type" => "authorization_code", |
|
99 | + "client_id" => $this->getClientId(), |
|
100 | + "client_secret" => $this->getClientSecret(), |
|
101 | + "redirect_uri" => $this->getCallbackUrl(), |
|
102 | + "code" => $code, |
|
103 | + "state" => $this->state |
|
104 | + ); |
|
105 | 105 | $response = HelperFactory::request($this->client, Constants::API_TOKEN, $options, 'POST'); |
106 | 106 | $this->oauthResponse = new InstagramOAuth( |
107 | 107 | json_decode($response->getBody()->getContents()) |
108 | 108 | ); |
109 | - return $this->oauthResponse; |
|
110 | - } |
|
109 | + return $this->oauthResponse; |
|
110 | + } |
|
111 | 111 | |
112 | - /* |
|
112 | + /* |
|
113 | 113 | * Set Client Id |
114 | 114 | * @param string $clientId |
115 | 115 | * @return void |
116 | 116 | */ |
117 | - public function setClientId($clientId) |
|
118 | - { |
|
119 | - $this->clientId = $clientId; |
|
120 | - } |
|
117 | + public function setClientId($clientId) |
|
118 | + { |
|
119 | + $this->clientId = $clientId; |
|
120 | + } |
|
121 | 121 | |
122 | - /* |
|
122 | + /* |
|
123 | 123 | * Get Client Id |
124 | 124 | * @return string |
125 | 125 | */ |
126 | - public function getClientId() |
|
127 | - { |
|
128 | - return $this->clientId; |
|
129 | - } |
|
126 | + public function getClientId() |
|
127 | + { |
|
128 | + return $this->clientId; |
|
129 | + } |
|
130 | 130 | |
131 | - /* |
|
131 | + /* |
|
132 | 132 | * Set Client Secret |
133 | 133 | * @param string $secret |
134 | 134 | * @return void |
135 | 135 | */ |
136 | - public function setClientSecret($secret) |
|
137 | - { |
|
138 | - $this->clientSecret = $secret; |
|
139 | - } |
|
136 | + public function setClientSecret($secret) |
|
137 | + { |
|
138 | + $this->clientSecret = $secret; |
|
139 | + } |
|
140 | 140 | |
141 | - /* |
|
141 | + /* |
|
142 | 142 | * Getter: Client Id |
143 | 143 | * @return string |
144 | 144 | */ |
145 | - public function getClientSecret() |
|
146 | - { |
|
147 | - return $this->clientSecret; |
|
148 | - } |
|
145 | + public function getClientSecret() |
|
146 | + { |
|
147 | + return $this->clientSecret; |
|
148 | + } |
|
149 | 149 | |
150 | - /* |
|
150 | + /* |
|
151 | 151 | * Setter: Callback Url |
152 | 152 | * @param string $url |
153 | 153 | * @return void |
154 | 154 | */ |
155 | - public function setCallbackUrl($url) |
|
156 | - { |
|
157 | - $this->callbackUrl = $url; |
|
158 | - } |
|
155 | + public function setCallbackUrl($url) |
|
156 | + { |
|
157 | + $this->callbackUrl = $url; |
|
158 | + } |
|
159 | 159 | |
160 | - /* |
|
160 | + /* |
|
161 | 161 | * Getter: Callback Url |
162 | 162 | * @return string |
163 | 163 | */ |
164 | - public function getCallbackUrl() |
|
165 | - { |
|
166 | - return $this->callbackUrl; |
|
167 | - } |
|
164 | + public function getCallbackUrl() |
|
165 | + { |
|
166 | + return $this->callbackUrl; |
|
167 | + } |
|
168 | 168 | |
169 | - /* |
|
169 | + /* |
|
170 | 170 | * Get InstagramOAuth |
171 | 171 | * @return InstagramOAuth |
172 | 172 | */ |
173 | - public function getOAuth() |
|
174 | - { |
|
175 | - if ($this->oauthResponse instanceof InstagramOAuth) { |
|
176 | - return $this->oauthResponse; |
|
177 | - } else { |
|
178 | - $this->oauthResponse = new InstagramOAuth(json_decode(json_encode(["access_token" => null]))); |
|
179 | - return $this->oauthResponse; |
|
180 | - } |
|
181 | - } |
|
182 | - /* |
|
173 | + public function getOAuth() |
|
174 | + { |
|
175 | + if ($this->oauthResponse instanceof InstagramOAuth) { |
|
176 | + return $this->oauthResponse; |
|
177 | + } else { |
|
178 | + $this->oauthResponse = new InstagramOAuth(json_decode(json_encode(["access_token" => null]))); |
|
179 | + return $this->oauthResponse; |
|
180 | + } |
|
181 | + } |
|
182 | + /* |
|
183 | 183 | * @return Client |
184 | 184 | */ |
185 | - public function getHttpClient() |
|
186 | - { |
|
187 | - return $this->client; |
|
188 | - } |
|
185 | + public function getHttpClient() |
|
186 | + { |
|
187 | + return $this->client; |
|
188 | + } |
|
189 | 189 | |
190 | - /* |
|
190 | + /* |
|
191 | 191 | * Setter: User Access Token |
192 | 192 | * @param string $token |
193 | 193 | * @return void |
194 | 194 | */ |
195 | - public function setAccessToken($token) |
|
196 | - { |
|
197 | - if (!$this->oauthResponse instanceof InstagramOAuth) { |
|
198 | - $this->oauthResponse = new InstagramOAuth(json_decode(json_encode(["access_token" => $token]))); |
|
199 | - } |
|
200 | - } |
|
195 | + public function setAccessToken($token) |
|
196 | + { |
|
197 | + if (!$this->oauthResponse instanceof InstagramOAuth) { |
|
198 | + $this->oauthResponse = new InstagramOAuth(json_decode(json_encode(["access_token" => $token]))); |
|
199 | + } |
|
200 | + } |
|
201 | 201 | |
202 | - /* |
|
202 | + /* |
|
203 | 203 | * Get a string containing the version of the library. |
204 | 204 | * @return string |
205 | 205 | */ |
206 | - public function getLibraryVersion() |
|
207 | - { |
|
208 | - return Constants::VERSION; |
|
209 | - } |
|
206 | + public function getLibraryVersion() |
|
207 | + { |
|
208 | + return Constants::VERSION; |
|
209 | + } |
|
210 | 210 | |
211 | - /* |
|
211 | + /* |
|
212 | 212 | * Get state value |
213 | 213 | * @return string|mixed |
214 | 214 | */ |
215 | - public function getState() |
|
216 | - { |
|
217 | - return $this->state; |
|
218 | - } |
|
215 | + public function getState() |
|
216 | + { |
|
217 | + return $this->state; |
|
218 | + } |
|
219 | 219 | } |
@@ -5,60 +5,60 @@ |
||
5 | 5 | |
6 | 6 | class InstagramOAuth |
7 | 7 | { |
8 | - /** @var string $accessToken */ |
|
9 | - private $accessToken; |
|
8 | + /** @var string $accessToken */ |
|
9 | + private $accessToken; |
|
10 | 10 | |
11 | - /** @var object $user */ |
|
12 | - private $user; |
|
11 | + /** @var object $user */ |
|
12 | + private $user; |
|
13 | 13 | |
14 | - /* |
|
14 | + /* |
|
15 | 15 | * @param \stdClass $oauth |
16 | 16 | * @throws InstagramOAuthException |
17 | 17 | */ |
18 | - public function __construct(\stdClass $oauth) |
|
19 | - { |
|
20 | - if (!empty($oauth)) { |
|
21 | - $this->accessToken = $oauth->access_token; |
|
22 | - $this->user = isset($oauth->user) ? $oauth->user : null; |
|
23 | - } else { |
|
24 | - throw new InstagramOAuthException("Bad Request 400 empty Response", 400); |
|
25 | - } |
|
26 | - } |
|
18 | + public function __construct(\stdClass $oauth) |
|
19 | + { |
|
20 | + if (!empty($oauth)) { |
|
21 | + $this->accessToken = $oauth->access_token; |
|
22 | + $this->user = isset($oauth->user) ? $oauth->user : null; |
|
23 | + } else { |
|
24 | + throw new InstagramOAuthException("Bad Request 400 empty Response", 400); |
|
25 | + } |
|
26 | + } |
|
27 | 27 | |
28 | - /* |
|
28 | + /* |
|
29 | 29 | * Get Access Token |
30 | 30 | * @return string |
31 | 31 | */ |
32 | - public function getAccessToken() |
|
33 | - { |
|
34 | - return $this->accessToken; |
|
35 | - } |
|
32 | + public function getAccessToken() |
|
33 | + { |
|
34 | + return $this->accessToken; |
|
35 | + } |
|
36 | 36 | |
37 | - /* |
|
37 | + /* |
|
38 | 38 | * Set Access Token |
39 | 39 | * @param string $token |
40 | 40 | * @return void |
41 | 41 | */ |
42 | - public function setAccessToken($token) |
|
43 | - { |
|
44 | - $this->accessToken = $token; |
|
45 | - } |
|
42 | + public function setAccessToken($token) |
|
43 | + { |
|
44 | + $this->accessToken = $token; |
|
45 | + } |
|
46 | 46 | |
47 | - /* |
|
47 | + /* |
|
48 | 48 | * If AccessToken is set return true else false |
49 | 49 | * @return bool |
50 | 50 | */ |
51 | - public function isAccessTokenSet() |
|
52 | - { |
|
53 | - return isset($this->accessToken) ? true : false; |
|
54 | - } |
|
51 | + public function isAccessTokenSet() |
|
52 | + { |
|
53 | + return isset($this->accessToken) ? true : false; |
|
54 | + } |
|
55 | 55 | |
56 | - /* |
|
56 | + /* |
|
57 | 57 | * Get User Info |
58 | 58 | * @return object |
59 | 59 | */ |
60 | - public function getUserInfo() |
|
61 | - { |
|
62 | - return $this->user; |
|
63 | - } |
|
60 | + public function getUserInfo() |
|
61 | + { |
|
62 | + return $this->user; |
|
63 | + } |
|
64 | 64 | } |
@@ -6,14 +6,14 @@ discard block |
||
6 | 6 | |
7 | 7 | class InstagramResponse |
8 | 8 | { |
9 | - /** @var int $status_code */ |
|
10 | - private $statusCode; |
|
9 | + /** @var int $status_code */ |
|
10 | + private $statusCode; |
|
11 | 11 | |
12 | - /** @var string $protocol */ |
|
13 | - private $protocol = '1.1'; |
|
12 | + /** @var string $protocol */ |
|
13 | + private $protocol = '1.1'; |
|
14 | 14 | |
15 | - /** @var array $headers */ |
|
16 | - private $headers = []; |
|
15 | + /** @var array $headers */ |
|
16 | + private $headers = []; |
|
17 | 17 | |
18 | 18 | /** @var bool */ |
19 | 19 | private $isPagination = false; |
@@ -30,35 +30,35 @@ discard block |
||
30 | 30 | /** @var object */ |
31 | 31 | private $data; |
32 | 32 | |
33 | - /** @var object $body */ |
|
34 | - private $body; |
|
33 | + /** @var object $body */ |
|
34 | + private $body; |
|
35 | 35 | |
36 | 36 | /* |
37 | 37 | * @param Response $response |
38 | 38 | * @throws InstagramResponseException |
39 | 39 | */ |
40 | - public function __construct(Response $response) |
|
41 | - { |
|
42 | - if ($response instanceof Response) { |
|
43 | - $this->setParams($response); |
|
44 | - } else { |
|
45 | - throw new InstagramResponseException('Bad Request: Response is not valid instance of GuzzleHttp\Psr7\Response', 404); |
|
46 | - } |
|
47 | - } |
|
40 | + public function __construct(Response $response) |
|
41 | + { |
|
42 | + if ($response instanceof Response) { |
|
43 | + $this->setParams($response); |
|
44 | + } else { |
|
45 | + throw new InstagramResponseException('Bad Request: Response is not valid instance of GuzzleHttp\Psr7\Response', 404); |
|
46 | + } |
|
47 | + } |
|
48 | 48 | |
49 | 49 | /* |
50 | 50 | * Set Values to the class members |
51 | 51 | * @param Response $response |
52 | 52 | * @return void |
53 | 53 | */ |
54 | - private function setParams(Response $response) |
|
55 | - { |
|
56 | - $this->protocol = $response->getProtocolVersion(); |
|
57 | - $this->statusCode = (int) $response->getStatusCode(); |
|
58 | - $this->headers = $response->getHeaders(); |
|
59 | - $this->body = json_decode($response->getBody()->getContents()); |
|
54 | + private function setParams(Response $response) |
|
55 | + { |
|
56 | + $this->protocol = $response->getProtocolVersion(); |
|
57 | + $this->statusCode = (int) $response->getStatusCode(); |
|
58 | + $this->headers = $response->getHeaders(); |
|
59 | + $this->body = json_decode($response->getBody()->getContents()); |
|
60 | 60 | $this->extractBodyParts(); |
61 | - } |
|
61 | + } |
|
62 | 62 | |
63 | 63 | private function extractBodyParts() |
64 | 64 | { |
@@ -79,38 +79,38 @@ discard block |
||
79 | 79 | * Get response |
80 | 80 | * @return object|string |
81 | 81 | */ |
82 | - public function getBody() |
|
83 | - { |
|
84 | - return $this->body; |
|
85 | - } |
|
82 | + public function getBody() |
|
83 | + { |
|
84 | + return $this->body; |
|
85 | + } |
|
86 | 86 | |
87 | 87 | /* |
88 | 88 | * Get Status Code |
89 | 89 | * @return int |
90 | 90 | */ |
91 | - public function getStatusCode() |
|
92 | - { |
|
93 | - return $this->statusCode; |
|
94 | - } |
|
91 | + public function getStatusCode() |
|
92 | + { |
|
93 | + return $this->statusCode; |
|
94 | + } |
|
95 | 95 | |
96 | 96 | /* |
97 | 97 | * Get specific header |
98 | 98 | * @param string $header |
99 | 99 | * @retrun string |
100 | 100 | */ |
101 | - public function getHeader($header) |
|
102 | - { |
|
103 | - return isset($this->headers[$header]) ? $this->headers[$header] : []; |
|
104 | - } |
|
101 | + public function getHeader($header) |
|
102 | + { |
|
103 | + return isset($this->headers[$header]) ? $this->headers[$header] : []; |
|
104 | + } |
|
105 | 105 | |
106 | 106 | /* |
107 | 107 | * Get all headers |
108 | 108 | * @retrun array |
109 | 109 | */ |
110 | - public function getHeaders() |
|
111 | - { |
|
112 | - return $this->headers; |
|
113 | - } |
|
110 | + public function getHeaders() |
|
111 | + { |
|
112 | + return $this->headers; |
|
113 | + } |
|
114 | 114 | |
115 | 115 | /* |
116 | 116 | * Get data from body |
@@ -11,33 +11,33 @@ discard block |
||
11 | 11 | |
12 | 12 | class HelperFactory |
13 | 13 | { |
14 | - /** @var Response $response */ |
|
15 | - protected static $response; |
|
14 | + /** @var Response $response */ |
|
15 | + protected static $response; |
|
16 | 16 | |
17 | - /** @var Stream $stream */ |
|
18 | - protected static $stream; |
|
17 | + /** @var Stream $stream */ |
|
18 | + protected static $stream; |
|
19 | 19 | |
20 | - /** @var object $content */ |
|
21 | - protected static $content; |
|
20 | + /** @var object $content */ |
|
21 | + protected static $content; |
|
22 | 22 | |
23 | - private function __construct() |
|
24 | - { |
|
25 | - // a factory constructor should never be invoked |
|
26 | - } |
|
23 | + private function __construct() |
|
24 | + { |
|
25 | + // a factory constructor should never be invoked |
|
26 | + } |
|
27 | 27 | |
28 | - /* |
|
28 | + /* |
|
29 | 29 | * Factory Client method to create \GuzzleHttp\Client object |
30 | 30 | * @param string $uri |
31 | 31 | * @return Client |
32 | 32 | */ |
33 | - public static function client($uri) |
|
34 | - { |
|
35 | - return new Client([ |
|
36 | - 'base_uri' => $uri |
|
37 | - ]); |
|
38 | - } |
|
33 | + public static function client($uri) |
|
34 | + { |
|
35 | + return new Client([ |
|
36 | + 'base_uri' => $uri |
|
37 | + ]); |
|
38 | + } |
|
39 | 39 | |
40 | - /* |
|
40 | + /* |
|
41 | 41 | * @param Client $client |
42 | 42 | * @param string $endpoint |
43 | 43 | * @param array|string $options |
@@ -46,79 +46,79 @@ discard block |
||
46 | 46 | * @throws InstagramOAuthException |
47 | 47 | * @throws InstagramException |
48 | 48 | */ |
49 | - public static function request(Client $client, $endpoint, $options, $method = 'GET') |
|
50 | - { |
|
51 | - try { |
|
52 | - return $client->request($method, $endpoint, [ |
|
53 | - 'headers' => ['Accept' => 'application/json'], |
|
54 | - 'body' => static::createBody($options, $method) |
|
55 | - ]); |
|
56 | - } catch (ClientException $e) { |
|
57 | - static::throwException(static::extractOriginalExceptionMessage($e), $e); |
|
58 | - } |
|
59 | - } |
|
49 | + public static function request(Client $client, $endpoint, $options, $method = 'GET') |
|
50 | + { |
|
51 | + try { |
|
52 | + return $client->request($method, $endpoint, [ |
|
53 | + 'headers' => ['Accept' => 'application/json'], |
|
54 | + 'body' => static::createBody($options, $method) |
|
55 | + ]); |
|
56 | + } catch (ClientException $e) { |
|
57 | + static::throwException(static::extractOriginalExceptionMessage($e), $e); |
|
58 | + } |
|
59 | + } |
|
60 | 60 | |
61 | - /* |
|
61 | + /* |
|
62 | 62 | * Create body for Guzzle client request |
63 | 63 | * @param array|null|string $options |
64 | 64 | * @param string $method GET|POST |
65 | 65 | * @return string|mixed |
66 | 66 | */ |
67 | - protected static function createBody($options, $method) |
|
68 | - { |
|
69 | - return ('GET' !== $method) ? is_array($options) ? http_build_query($options) : ltrim($options, '&') : null; |
|
70 | - } |
|
67 | + protected static function createBody($options, $method) |
|
68 | + { |
|
69 | + return ('GET' !== $method) ? is_array($options) ? http_build_query($options) : ltrim($options, '&') : null; |
|
70 | + } |
|
71 | 71 | |
72 | - /* |
|
72 | + /* |
|
73 | 73 | * Method to extract all exceptions for Guzzle ClientException |
74 | 74 | * @param ClientException $e |
75 | 75 | * @return |
76 | 76 | */ |
77 | - protected static function extractOriginalExceptionMessage(ClientException $e) |
|
78 | - { |
|
79 | - self::$response = $e->getResponse(); |
|
80 | - self::$stream = self::$response->getBody(); |
|
81 | - self::$content = self::$stream->getContents(); |
|
82 | - if (empty(self::$content)) { |
|
83 | - throw new InstagramServerException( |
|
84 | - $e->getMessage(), |
|
85 | - $e->getCode(), |
|
86 | - $e |
|
87 | - ); |
|
88 | - } else { |
|
89 | - return json_decode(self::$content); |
|
90 | - } |
|
91 | - } |
|
77 | + protected static function extractOriginalExceptionMessage(ClientException $e) |
|
78 | + { |
|
79 | + self::$response = $e->getResponse(); |
|
80 | + self::$stream = self::$response->getBody(); |
|
81 | + self::$content = self::$stream->getContents(); |
|
82 | + if (empty(self::$content)) { |
|
83 | + throw new InstagramServerException( |
|
84 | + $e->getMessage(), |
|
85 | + $e->getCode(), |
|
86 | + $e |
|
87 | + ); |
|
88 | + } else { |
|
89 | + return json_decode(self::$content); |
|
90 | + } |
|
91 | + } |
|
92 | 92 | |
93 | - /* |
|
93 | + /* |
|
94 | 94 | * @param \stdClass $object |
95 | 95 | * @param ClientException $e |
96 | 96 | * @return void |
97 | 97 | */ |
98 | - protected static function throwException(\stdClass $object, ClientException $e) |
|
99 | - { |
|
100 | - $exception = array(); |
|
101 | - if (isset($object->meta)) { |
|
102 | - $exception['error_type'] = $object->meta->error_type; |
|
103 | - $exception['error_message'] = $object->meta->error_message; |
|
104 | - $exception['error_code'] = $object->meta->code; |
|
105 | - } else { |
|
106 | - $exception['error_type'] = $object->error_type; |
|
107 | - $exception['error_message'] = $object->error_message; |
|
108 | - $exception['error_code'] = $object->code; |
|
109 | - } |
|
110 | - if (stripos($exception['error_type'], "oauth") !== false) { |
|
111 | - throw new InstagramOAuthException( |
|
112 | - json_encode(array("Type" => $exception['error_type'], "Message" => $exception['error_message'])), |
|
113 | - $exception['error_code'], |
|
114 | - $e |
|
115 | - ); |
|
116 | - } else { |
|
117 | - throw new InstagramException( |
|
118 | - json_encode(array("Type" => $exception['error_type'], "Message" => $exception['error_message'])), |
|
119 | - $exception['error_code'], |
|
120 | - $e |
|
121 | - ); |
|
122 | - } |
|
123 | - } |
|
98 | + protected static function throwException(\stdClass $object, ClientException $e) |
|
99 | + { |
|
100 | + $exception = array(); |
|
101 | + if (isset($object->meta)) { |
|
102 | + $exception['error_type'] = $object->meta->error_type; |
|
103 | + $exception['error_message'] = $object->meta->error_message; |
|
104 | + $exception['error_code'] = $object->meta->code; |
|
105 | + } else { |
|
106 | + $exception['error_type'] = $object->error_type; |
|
107 | + $exception['error_message'] = $object->error_message; |
|
108 | + $exception['error_code'] = $object->code; |
|
109 | + } |
|
110 | + if (stripos($exception['error_type'], "oauth") !== false) { |
|
111 | + throw new InstagramOAuthException( |
|
112 | + json_encode(array("Type" => $exception['error_type'], "Message" => $exception['error_message'])), |
|
113 | + $exception['error_code'], |
|
114 | + $e |
|
115 | + ); |
|
116 | + } else { |
|
117 | + throw new InstagramException( |
|
118 | + json_encode(array("Type" => $exception['error_type'], "Message" => $exception['error_message'])), |
|
119 | + $exception['error_code'], |
|
120 | + $e |
|
121 | + ); |
|
122 | + } |
|
123 | + } |
|
124 | 124 | } |