Version 1
[yaffs-website] / vendor / php-instagram-api / php-instagram-api / Instagram / Core / Proxy.php
1 <?php
2
3 /**
4 * Instagram PHP
5 * @author Galen Grover <galenjr@gmail.com>
6 * @license http://opensource.org/licenses/mit-license.php The MIT License
7 */
8
9 namespace Instagram\Core;
10
11 /**
12  * Proxy
13  *
14  * This class performs all the API calls
15  *
16  * It uses the supplied HTTP client as a default (cURL)
17  * 
18  */
19 class Proxy {
20
21     /**
22      * HTTP Client
23      * 
24      * @var \Instagram\Net\ClientInterface
25      * @access protected
26      */
27     protected $client;
28
29     /**
30      * Instagram access token
31      * 
32      * @var string
33      * @access protected
34      */
35     protected $access_token = null;
36
37     /**
38      * Client ID
39      * 
40      * @var string
41      * @access protected
42      */
43     protected $client_id = null;
44
45     /**
46      * API URL
47      * 
48      * @var string
49      * @access protected
50      */
51     protected $api_url = 'https://api.instagram.com/v1';
52
53     /**
54      * Constructor
55      *
56      * @param \Instagram\Net\ClientInterface $client HTTP Client
57      * @param string $access_token The access token from authentication
58      * @access public
59      */
60     public function __construct( \Instagram\Net\ClientInterface $client, $access_token = null ) {
61         $this->client = $client;
62         $this->access_token = $access_token;
63     }
64
65     /**
66      * Get the access token
67      * 
68      * @param array $data Auth data
69      * @return string Returns the access token
70      */
71     public function getAccessToken( array $data ) {
72         $response = $this->apiCall(
73             'post',
74             'https://api.instagram.com/oauth/access_token',
75             $data
76         );
77         return $response;
78     }
79
80     /**
81      * Set the access token
82      * 
83      * @param string $access_token The access token
84      * @access public
85      */
86     public function setAccessToken( $access_token ) {
87         $this->access_token = $access_token;
88     }
89
90     /**
91      * Set the client ID
92      * 
93      * @param string $client_id the client ID
94      * @access public
95      */
96     public function setClientID( $client_id ) {
97         $this->client_id = $client_id;
98     }
99
100     /**
101      * Logout of instagram
102      *
103      * This hasn't been implemented by instagram yet
104      * 
105      * @access public
106      */
107     public function logout() {
108         $this->client->get( 'https://instagram.com/accounts/logout/', array() );
109     }
110
111     /**
112      * Get the media associated with an object
113      *
114      * This function is used by the individual object functions
115      * getLocationMedia, getTagMedia, atc...
116      * 
117      * @param  string $api_endpoint API endpoint for the object type
118      * @param  string $id Id of the object to get the media for
119      * @param  array $params Extra parameters for the API call
120      * @return StdClass Returns the raw response
121      * @access protected
122      */
123     protected function getObjectMedia( $api_endpoint, $id, array $params = null ) {
124         $response = $this->apiCall(
125             'get',
126             sprintf( '%s/%s/%s/media/recent', $this->api_url, strtolower( $api_endpoint ), $id  ),
127             $params
128         );
129         return $response->getRawData();
130     }
131
132     /**
133      * Get location media
134      * 
135      * @param string $id Location ID
136      * @param array $params Extra params to pass to the API
137      * @return StdClass Returns the location media
138      * @access public
139      */
140     public function getLocationMedia( $id, array $params = null ) {
141         return $this->getObjectMedia( 'Locations', $id, $params );
142     }
143
144     /**
145      * Get tag media
146      * 
147      * @param string $id Location ID
148      * @param array $params Extra params to pass to the API
149      * @return StdClass Returns the location media
150      * @access public
151      */
152     public function getTagMedia( $id, array $params = null ) {
153         return $this->getObjectMedia( 'Tags', $id, $params );
154     }
155
156     /**
157      * Get user media
158      * 
159      * @param string $id Location ID
160      * @param array $params Extra params to pass to the API
161      * @return StdClass Returns the location media
162      * @access public
163      */
164     public function getUserMedia( $id, array $params = null ) {
165         return $this->getObjectMedia( 'Users', $id, $params );
166     }
167
168     /**
169      * Get user
170      * 
171      * @param string $id User ID
172      * @return StdClass Returns the user data
173      * @access public
174      */
175     public function getUser( $id ) {
176         $response = $this->apiCall(
177             'get',
178             sprintf( '%s/users/%s', $this->api_url, $id )
179         );
180         return $response->getData();
181     }
182
183     /**
184      * Get a user's follows
185      * 
186      * @param string $id User's ID
187      * @param array $params Extra params to pass to the API
188      * @return StdClass Returns the user's followers
189      * @access public
190      */
191     public function getUserFollows( $id, array $params = null ) {
192         $response = $this->apiCall(
193             'get',
194             sprintf( '%s/users/%s/follows', $this->api_url, $id ),
195             $params
196         );
197         return $response->getRawData();
198     }
199
200     /**
201      * Get a user's followers
202      * 
203      * @param string $id User's ID
204      * @param array $params Extra params to pass to the API
205      * @return StdClass Returns the user's followers
206      * @access public
207      */
208     public function getUserFollowers( $id, array $params = null ) {
209         $response = $this->apiCall(
210             'get',
211             sprintf( '%s/users/%s/followed-by', $this->api_url, $id ),
212             $params
213         );
214         return $response->getRawData();
215     }
216
217     /**
218      * Get media comments
219      * 
220      * @param string $id Media ID
221      * @return StdClass Returns the media data
222      * @access public
223      */
224     public function getMediaComments( $id ) {
225         $response = $this->apiCall(
226             'get',
227             sprintf( '%s/media/%s/comments', $this->api_url, $id )
228         );
229         return $response->getRawData();
230     }
231
232     /**
233      * Get media likes
234      * 
235      * @param string $id Media ID
236      * @return StdClass Returns the media likes
237      * @access public
238      */
239     public function getMediaLikes( $id ) {
240         $response = $this->apiCall(
241             'get',
242             sprintf( '%s/media/%s/likes', $this->api_url, $id )
243         );
244         return $response->getRawData();
245     }
246
247     /**
248      * Get media comments
249      * 
250      * @return StdClass Returns the current user data
251      * @access public
252      */
253     public function getCurrentUser() {
254         $response = $this->apiCall(
255             'get',
256             sprintf( '%s/users/self', $this->api_url )
257         );
258         return $response->getData();
259     }
260
261     /**
262      * Get media
263      * 
264      * @param string $id Media ID
265      * @return StdClass Returns the media data
266      * @access public
267      */
268     public function getMedia( $id ) {
269         $response = $this->apiCall(
270             'get',
271             sprintf( '%s/media/%s', $this->api_url, $id )
272         );
273         return $response->getData();
274     }
275
276     /**
277      * Get tag
278      * 
279      * @param string $id Tag ID
280      * @return StdClass Returns the tag data
281      * @access public
282      */
283     public function getTag( $tag ) {
284         $response = $this->apiCall(
285             'get',
286             sprintf( '%s/tags/%s', $this->api_url, $tag )
287         );
288         return $response->getData();
289     }
290
291     /**
292      * Get location
293      * 
294      * @param string $id Location ID
295      * @return StdClass Returns the location data
296      * @access public
297      */
298     public function getLocation( $id ) {
299         $response = $this->apiCall(
300             'get',
301             sprintf( '%s/locations/%s', $this->api_url, $id )
302         );
303         return $response->getData();
304     }
305
306     /**
307      * Search users
308      * 
309      * @param array $params Search params
310      * @return array Returns an array of user data
311      * @access public
312      */
313     public function searchUsers( array $params = null ) {
314         $response = $this->apiCall(
315             'get',
316             $this->api_url . '/users/search',
317             $params
318         );
319         return $response->getRawData();
320     }
321
322     /**
323      * Search tags
324      * 
325      * @param array $params Search params
326      * @return array Returns an array of tag data
327      * @access public
328      */
329     public function searchTags( array $params = null ) {
330         $response = $this->apiCall(
331             'get',
332             $this->api_url . '/tags/search',
333             $params
334         );
335         return $response->getRawData();
336     }
337
338     /**
339      * Search media
340      * 
341      * @param array $params Search params
342      * @return array Returns an array of media data
343      * @access public
344      */
345     public function searchMedia( array $params = null ) {
346         $response = $this->apiCall(
347             'get',
348             $this->api_url . '/media/search',
349             $params
350         );
351         return $response->getRawData();
352     }
353
354     /**
355      * Search locations
356      * 
357      * @param array $params Search params
358      * @return array Returns an array of location data
359      * @access public
360      */
361     public function searchLocations( array $params = null ) {
362         $response = $this->apiCall(
363             'get',
364             $this->api_url . '/locations/search',
365             $params
366         );
367         return $response->getRawData();
368     }
369
370     /**
371      * Get popular media
372      * 
373      * @param array $params Extra params
374      * @return array Returns an array of popular media data
375      * @access public
376      */
377     public function getPopularMedia( array $params = null ) {
378         $response = $this->apiCall(
379             'get',
380             $this->api_url . '/media/popular',
381             $params
382         );
383         return $response->getRawData();
384     }
385
386     /**
387      * Get the current user's feed
388      * 
389      * @param array $params Extra params
390      * @return array Returns an array of media data
391      * @access public
392      */
393     public function getFeed( array $params = null ) {
394         $response = $this->apiCall(
395             'get',
396             $this->api_url . '/users/self/feed',
397             $params
398         );
399         return $response->getRawData();
400     }
401
402     /**
403      * Get the current users follow requests
404      * 
405      * @param $params Extra params (not used in API, here in case it's added)
406      * @return array Returns an array of user data
407      * @access public
408      */
409     public function getFollowRequests( array $params = null ) {
410         $response = $this->apiCall(
411             'get',
412             $this->api_url . '/users/self/requested-by',
413             $params
414         );
415         return $response->getRawData();
416     }
417
418     /**
419      * Get the current user's liked media
420      * 
421      * @param array $params Extra params
422      * @return array Returns an array of media data
423      * @access public
424      */
425     public function getLikedMedia( array $params = null ) {
426         $response = $this->apiCall(
427             'get',
428             $this->api_url . '/users/self/media/liked',
429             $params
430         );
431         return $response->getRawData();
432     }
433
434     /**
435      * Get a user's relationship to the current user
436      * 
437      * @param string $user_id User to check relationship for
438      * @return StdClass Returns the relationship
439      * @access public
440      */
441     public function getRelationshipToCurrentUser( $user_id ) {
442         $response = $this->apiCall(
443             'get',
444             $this->api_url . sprintf( '/users/%s/relationship', $user_id )
445         );
446         return $response->getData();
447     }
448
449     /**
450      * Modify a relationship with the current user
451      * @param string $user_id User ID of the user to change the relationship for
452      * @param string $relationship New relationship {@link http://instagram.com/developer/endpoints/relationships/#post_relationship}
453      * @return StdClass Returns the status
454      * @access public
455      */
456     public function modifyRelationship( $user_id, $relationship ) {
457         $response = $this->apiCall(
458             'post',
459             $this->api_url . sprintf( '/users/%s/relationship', $user_id ),
460             array( 'action' => $relationship )
461         );
462         return $response->getData();
463     }
464
465     /**
466      * Add a like form the current user on a media
467      * 
468      * @param string $media_id Media ID to like
469      * @return StdClass Returns the status
470      * @access public
471      */
472     public function like( $media_id ) {
473         $this->apiCall(
474             'post',
475             $this->api_url . sprintf( '/media/%s/likes', $media_id )
476         );
477     }
478
479     /**
480      * Delete a like form the current user on a media
481      * 
482      * @param string $media_id Media ID to unlike
483      * @return StdClass Returns the status
484      * @access public
485      */
486     public function unLike( $media_id ) {
487         $this->apiCall(
488             'delete',
489             $this->api_url . sprintf( '/media/%s/likes', $media_id )
490         );
491     }
492
493     /**
494      * Add a comment to a media
495      * 
496      * @param string $media_id Media ID
497      * @param string $text Comment text
498      * @return StdClass Returns the status
499      * @access public
500      */
501     public function addMediaComment( $media_id, $text ) {
502         $this->apiCall(
503             'post',
504             $this->api_url . sprintf( '/media/%s/comments', $media_id ),
505             array( 'text' => $text )
506         );
507     }
508
509     /**
510      * Delete a comment from a media
511      * 
512      * @param string $media_id Media ID
513      * @param string $comment_id Comment ID to delete
514      * @return StdClass
515      * @access public
516      */
517     public function deleteMediaComment( $media_id, $comment_id ) {
518         $this->apiCall(
519             'delete',
520             $this->api_url . sprintf( '/media/%s/comments/%s', $media_id, $comment_id )
521         );
522     }
523
524     /**
525      * Make a call to the API
526      * 
527      * @param string $method HTTP method to use
528      * @param string $url URL
529      * @param array $params API parameters
530      * @param boolean $throw_exception True to throw exceptoins
531      * @throws APIException, APIAuthException
532      * @return  \Instagram\Net\ApiResponse Returns teh API response
533      * @access private
534      */
535     private function apiCall( $method, $url, array $params = null, $throw_exception = true ){
536
537         $raw_response = $this->client->$method(
538             $url,
539             array(
540                 'access_token'  => $this->access_token,
541                 'client_id'     => isset( $params['client_id'] ) ? $params['client_id'] : $this->client_id
542             ) + (array) $params
543         );
544
545         $response = new \Instagram\Net\ApiResponse( $raw_response );
546
547         if ( !$response->isValid() ) {
548             if ( $throw_exception ) {
549                 if ( $response->getErrorType() == 'OAuthAccessTokenException' ) {
550                     throw new \Instagram\Core\ApiAuthException( $response->getErrorMessage(), $response->getErrorCode(), $response->getErrorType() );
551                 }
552                 else {
553                     throw new \Instagram\Core\ApiException( $response->getErrorMessage(), $response->getErrorCode(), $response->getErrorType() );
554                 }
555             }
556             else {
557                 return false;
558             }
559         }
560         return $response;
561     }
562
563
564 }