@@ -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 | } |