5 * @author Galen Grover <galenjr@gmail.com>
6 * @license http://opensource.org/licenses/mit-license.php The MIT License
9 namespace Instagram\Core;
14 * This class performs all the API calls
16 * It uses the supplied HTTP client as a default (cURL)
24 * @var \Instagram\Net\ClientInterface
30 * Instagram access token
35 protected $access_token = null;
43 protected $client_id = null;
51 protected $api_url = 'https://api.instagram.com/v1';
56 * @param \Instagram\Net\ClientInterface $client HTTP Client
57 * @param string $access_token The access token from authentication
60 public function __construct( \Instagram\Net\ClientInterface $client, $access_token = null ) {
61 $this->client = $client;
62 $this->access_token = $access_token;
66 * Get the access token
68 * @param array $data Auth data
69 * @return string Returns the access token
71 public function getAccessToken( array $data ) {
72 $response = $this->apiCall(
74 'https://api.instagram.com/oauth/access_token',
81 * Set the access token
83 * @param string $access_token The access token
86 public function setAccessToken( $access_token ) {
87 $this->access_token = $access_token;
93 * @param string $client_id the client ID
96 public function setClientID( $client_id ) {
97 $this->client_id = $client_id;
101 * Logout of instagram
103 * This hasn't been implemented by instagram yet
107 public function logout() {
108 $this->client->get( 'https://instagram.com/accounts/logout/', array() );
112 * Get the media associated with an object
114 * This function is used by the individual object functions
115 * getLocationMedia, getTagMedia, atc...
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
123 protected function getObjectMedia( $api_endpoint, $id, array $params = null ) {
124 $response = $this->apiCall(
126 sprintf( '%s/%s/%s/media/recent', $this->api_url, strtolower( $api_endpoint ), $id ),
129 return $response->getRawData();
135 * @param string $id Location ID
136 * @param array $params Extra params to pass to the API
137 * @return StdClass Returns the location media
140 public function getLocationMedia( $id, array $params = null ) {
141 return $this->getObjectMedia( 'Locations', $id, $params );
147 * @param string $id Location ID
148 * @param array $params Extra params to pass to the API
149 * @return StdClass Returns the location media
152 public function getTagMedia( $id, array $params = null ) {
153 return $this->getObjectMedia( 'Tags', $id, $params );
159 * @param string $id Location ID
160 * @param array $params Extra params to pass to the API
161 * @return StdClass Returns the location media
164 public function getUserMedia( $id, array $params = null ) {
165 return $this->getObjectMedia( 'Users', $id, $params );
171 * @param string $id User ID
172 * @return StdClass Returns the user data
175 public function getUser( $id ) {
176 $response = $this->apiCall(
178 sprintf( '%s/users/%s', $this->api_url, $id )
180 return $response->getData();
184 * Get a user's follows
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
191 public function getUserFollows( $id, array $params = null ) {
192 $response = $this->apiCall(
194 sprintf( '%s/users/%s/follows', $this->api_url, $id ),
197 return $response->getRawData();
201 * Get a user's followers
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
208 public function getUserFollowers( $id, array $params = null ) {
209 $response = $this->apiCall(
211 sprintf( '%s/users/%s/followed-by', $this->api_url, $id ),
214 return $response->getRawData();
220 * @param string $id Media ID
221 * @return StdClass Returns the media data
224 public function getMediaComments( $id ) {
225 $response = $this->apiCall(
227 sprintf( '%s/media/%s/comments', $this->api_url, $id )
229 return $response->getRawData();
235 * @param string $id Media ID
236 * @return StdClass Returns the media likes
239 public function getMediaLikes( $id ) {
240 $response = $this->apiCall(
242 sprintf( '%s/media/%s/likes', $this->api_url, $id )
244 return $response->getRawData();
250 * @return StdClass Returns the current user data
253 public function getCurrentUser() {
254 $response = $this->apiCall(
256 sprintf( '%s/users/self', $this->api_url )
258 return $response->getData();
264 * @param string $id Media ID
265 * @return StdClass Returns the media data
268 public function getMedia( $id ) {
269 $response = $this->apiCall(
271 sprintf( '%s/media/%s', $this->api_url, $id )
273 return $response->getData();
279 * @param string $id Tag ID
280 * @return StdClass Returns the tag data
283 public function getTag( $tag ) {
284 $response = $this->apiCall(
286 sprintf( '%s/tags/%s', $this->api_url, $tag )
288 return $response->getData();
294 * @param string $id Location ID
295 * @return StdClass Returns the location data
298 public function getLocation( $id ) {
299 $response = $this->apiCall(
301 sprintf( '%s/locations/%s', $this->api_url, $id )
303 return $response->getData();
309 * @param array $params Search params
310 * @return array Returns an array of user data
313 public function searchUsers( array $params = null ) {
314 $response = $this->apiCall(
316 $this->api_url . '/users/search',
319 return $response->getRawData();
325 * @param array $params Search params
326 * @return array Returns an array of tag data
329 public function searchTags( array $params = null ) {
330 $response = $this->apiCall(
332 $this->api_url . '/tags/search',
335 return $response->getRawData();
341 * @param array $params Search params
342 * @return array Returns an array of media data
345 public function searchMedia( array $params = null ) {
346 $response = $this->apiCall(
348 $this->api_url . '/media/search',
351 return $response->getRawData();
357 * @param array $params Search params
358 * @return array Returns an array of location data
361 public function searchLocations( array $params = null ) {
362 $response = $this->apiCall(
364 $this->api_url . '/locations/search',
367 return $response->getRawData();
373 * @param array $params Extra params
374 * @return array Returns an array of popular media data
377 public function getPopularMedia( array $params = null ) {
378 $response = $this->apiCall(
380 $this->api_url . '/media/popular',
383 return $response->getRawData();
387 * Get the current user's feed
389 * @param array $params Extra params
390 * @return array Returns an array of media data
393 public function getFeed( array $params = null ) {
394 $response = $this->apiCall(
396 $this->api_url . '/users/self/feed',
399 return $response->getRawData();
403 * Get the current users follow requests
405 * @param $params Extra params (not used in API, here in case it's added)
406 * @return array Returns an array of user data
409 public function getFollowRequests( array $params = null ) {
410 $response = $this->apiCall(
412 $this->api_url . '/users/self/requested-by',
415 return $response->getRawData();
419 * Get the current user's liked media
421 * @param array $params Extra params
422 * @return array Returns an array of media data
425 public function getLikedMedia( array $params = null ) {
426 $response = $this->apiCall(
428 $this->api_url . '/users/self/media/liked',
431 return $response->getRawData();
435 * Get a user's relationship to the current user
437 * @param string $user_id User to check relationship for
438 * @return StdClass Returns the relationship
441 public function getRelationshipToCurrentUser( $user_id ) {
442 $response = $this->apiCall(
444 $this->api_url . sprintf( '/users/%s/relationship', $user_id )
446 return $response->getData();
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
456 public function modifyRelationship( $user_id, $relationship ) {
457 $response = $this->apiCall(
459 $this->api_url . sprintf( '/users/%s/relationship', $user_id ),
460 array( 'action' => $relationship )
462 return $response->getData();
466 * Add a like form the current user on a media
468 * @param string $media_id Media ID to like
469 * @return StdClass Returns the status
472 public function like( $media_id ) {
475 $this->api_url . sprintf( '/media/%s/likes', $media_id )
480 * Delete a like form the current user on a media
482 * @param string $media_id Media ID to unlike
483 * @return StdClass Returns the status
486 public function unLike( $media_id ) {
489 $this->api_url . sprintf( '/media/%s/likes', $media_id )
494 * Add a comment to a media
496 * @param string $media_id Media ID
497 * @param string $text Comment text
498 * @return StdClass Returns the status
501 public function addMediaComment( $media_id, $text ) {
504 $this->api_url . sprintf( '/media/%s/comments', $media_id ),
505 array( 'text' => $text )
510 * Delete a comment from a media
512 * @param string $media_id Media ID
513 * @param string $comment_id Comment ID to delete
517 public function deleteMediaComment( $media_id, $comment_id ) {
520 $this->api_url . sprintf( '/media/%s/comments/%s', $media_id, $comment_id )
525 * Make a call to the API
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
535 private function apiCall( $method, $url, array $params = null, $throw_exception = true ){
537 $raw_response = $this->client->$method(
540 'access_token' => $this->access_token,
541 'client_id' => isset( $params['client_id'] ) ? $params['client_id'] : $this->client_id
545 $response = new \Instagram\Net\ApiResponse( $raw_response );
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() );
553 throw new \Instagram\Core\ApiException( $response->getErrorMessage(), $response->getErrorCode(), $response->getErrorType() );