├── CurlHttpClient.php ├── Instagram.php ├── README ├── callback.php ├── example.php ├── styles.css └── subscription.php /CurlHttpClient.php: -------------------------------------------------------------------------------- 1 | handler = curl_init($uri); 33 | $this->_setOptions(); 34 | } 35 | 36 | protected function _setOptions() { 37 | curl_setopt($this->handler, CURLOPT_RETURNTRANSFER, true); 38 | curl_setopt($this->handler, CURLOPT_FOLLOWLOCATION, true); 39 | curl_setopt($this->handler, CURLOPT_USERAGENT, self::DEFAULT_USER_AGENT); 40 | } 41 | 42 | /** 43 | * Set the URI 44 | * @param $uri 45 | */ 46 | public function setUri($uri) { 47 | $this->handler = curl_init($uri); 48 | $this->_setOptions(); 49 | } 50 | 51 | /** 52 | * Receive the response with full headers 53 | * @param boolean $value 54 | */ 55 | public function setHeaders($value = true) { 56 | curl_setopt($this->handler, CURLOPT_HEADER, $value); 57 | } 58 | 59 | /** 60 | * Set the HTTP request method 61 | * @param string $method 62 | */ 63 | public function setMethod($method = self::GET) { 64 | switch ($method) { 65 | case self::GET : 66 | curl_setopt($this->handler, CURLOPT_HTTPGET, true); 67 | break; 68 | case self::POST : 69 | curl_setopt($this->handler, CURLOPT_POST, true); 70 | break; 71 | case self::DELETE : 72 | curl_setopt($this->handler, CURLOPT_CUSTOMREQUEST, self::DELETE); 73 | break; 74 | default: 75 | throw new CurlHttpClientException('Method not supported'); 76 | } 77 | } 78 | 79 | /** 80 | * Add a new post param to the set 81 | * @param string $name 82 | * @param mixed $value 83 | */ 84 | public function setPostParam($name, $value) { 85 | $this->postParams[$name] = $value; 86 | curl_setopt($this->handler, CURLOPT_POSTFIELDS, $this->postParams); 87 | } 88 | 89 | /** 90 | * Get the response 91 | * @return string 92 | */ 93 | public function getResponse() { 94 | $response = curl_exec($this->handler); 95 | curl_close($this->handler); 96 | 97 | return $response; 98 | } 99 | 100 | /** 101 | * Extract the headers from a response string 102 | * 103 | * @param string $response 104 | * @return mixed[] 105 | */ 106 | protected function extractHeaders($response) { 107 | $headers = array(); 108 | 109 | // First, split body and headers 110 | $parts = preg_split('|(?:\r?\n){2}|m', $response_str, 2); 111 | if (!$parts[0]) return $headers; 112 | 113 | // Split headers part to lines 114 | $lines = explode("\n", $parts[0]); 115 | unset($parts); 116 | $last_header = null; 117 | 118 | foreach($lines as $line) { 119 | $line = trim($line, "\r\n"); 120 | if ($line == "") break; 121 | 122 | // Locate headers like 'Location: ...' and 'Location:...' (note the missing space) 123 | if (preg_match("|^([\w-]+):\s*(.+)|", $line, $m)) { 124 | unset($last_header); 125 | $h_name = strtolower($m[1]); 126 | $h_value = $m[2]; 127 | 128 | if (isset($headers[$h_name])) { 129 | if (! is_array($headers[$h_name])) { 130 | $headers[$h_name] = array($headers[$h_name]); 131 | } 132 | 133 | $headers[$h_name][] = $h_value; 134 | } else { 135 | $headers[$h_name] = $h_value; 136 | } 137 | $last_header = $h_name; 138 | } else if (preg_match("|^\s+(.+)$|", $line, $m) && $last_header !== null) { 139 | if (is_array($headers[$last_header])) { 140 | end($headers[$last_header]); 141 | $last_header_key = key($headers[$last_header]); 142 | $headers[$last_header][$last_header_key] .= $m[1]; 143 | } else { 144 | $headers[$last_header] .= $m[1]; 145 | } 146 | } 147 | } 148 | 149 | return $headers; 150 | } 151 | } 152 | 153 | class CurlHttpClientException extends Exception {} 154 | -------------------------------------------------------------------------------- /Instagram.php: -------------------------------------------------------------------------------- 1 | . 18 | */ 19 | // require_once 'Zend/Http/Client.php'; 20 | require_once 'CurlHttpClient.php'; 21 | 22 | class Instagram { 23 | 24 | /** 25 | * The name of the GET param that holds the authentication code 26 | * @var string 27 | */ 28 | const RESPONSE_CODE_PARAM = 'code'; 29 | 30 | /** 31 | * Format for endpoint URL requests 32 | * @var string 33 | */ 34 | protected $_endpointUrls = array( 35 | 'authorize' => 'https://api.instagram.com/oauth/authorize/?client_id=%s&redirect_uri=%s&response_type=%s', 36 | 'access_token' => 'https://api.instagram.com/oauth/access_token', 37 | 'user' => 'https://api.instagram.com/v1/users/%s/?access_token=%s', 38 | 'user_feed' => 'https://api.instagram.com/v1/users/self/feed?%s', 39 | 'user_recent' => 'https://api.instagram.com/v1/users/%s/media/recent/?access_token=%s&max_id=%s&min_id=%s&max_timestamp=%s&min_timestamp=%s', 40 | 'user_search' => 'https://api.instagram.com/v1/users/search?q=%s&access_token=%s', 41 | 'user_follows' => 'https://api.instagram.com/v1/users/%s/follows?access_token=%s&cursor=%s', 42 | 'user_followed_by' => 'https://api.instagram.com/v1/users/%s/followed-by?access_token=%s', 43 | 'user_requested_by' => 'https://api.instagram.com/v1/users/self/requested-by?access_token=%s', 44 | 'user_relationship' => 'https://api.instagram.com/v1/users/%s/relationship?access_token=%s', 45 | 'user_liked' => 'https://api.instagram.com/v1/users/self/media/liked?access_token=%s', 46 | 'modify_user_relationship' => 'https://api.instagram.com/v1/users/%s/relationship?action=%s&access_token=%s', 47 | 'media' => 'https://api.instagram.com/v1/media/%s?access_token=%s', 48 | 'media_search' => 'https://api.instagram.com/v1/media/search?lat=%s&lng=%s&max_timestamp=%s&min_timestamp=%s&distance=%s&access_token=%s', 49 | 'media_popular' => 'https://api.instagram.com/v1/media/popular?access_token=%s', 50 | 'media_comments' => 'https://api.instagram.com/v1/media/%s/comments?access_token=%s', 51 | 'post_media_comment' => 'https://api.instagram.com/v1/media/%s/comments?access_token=%s', 52 | 'delete_media_comment' => 'https://api.instagram.com/v1/media/%s/comments?comment_id=%s&access_token=%s', 53 | 'likes' => 'https://api.instagram.com/v1/media/%s/likes?access_token=%s', 54 | 'post_like' => 'https://api.instagram.com/v1/media/%s/likes', 55 | 'remove_like' => 'https://api.instagram.com/v1/media/%s/likes?access_token=%s', 56 | 'tags' => 'https://api.instagram.com/v1/tags/%s?access_token=%s', 57 | 'tags_recent' => 'https://api.instagram.com/v1/tags/%s/media/recent?max_id=%s&min_id=%s&access_token=%s', 58 | 'tags_search' => 'https://api.instagram.com/v1/tags/search?q=%s&access_token=%s', 59 | 'locations' => 'https://api.instagram.com/v1/locations/%s?access_token=%s', 60 | 'locations_recent' => 'https://api.instagram.com/v1/locations/%s/media/recent/?max_id=%s&min_id=%s&max_timestamp=%s&min_timestamp=%s&access_token=%s', 61 | 'locations_search' => 'https://api.instagram.com/v1/locations/search?lat=%s&lng=%s&foursquare_id=%s&distance=%s&access_token=%s', 62 | 'create_subscriptions' => 'https://api.instagram.com/v1/subscriptions', 63 | 'manage_subscriptions' => 'https://api.instagram.com/v1/subscriptions?%s' 64 | ); 65 | 66 | /** 67 | * Configuration parameter 68 | */ 69 | protected $_config = array(); 70 | 71 | /** 72 | * Whether all response are sent as JSON or decoded 73 | */ 74 | protected $_arrayResponses = false; 75 | 76 | /** 77 | * OAuth token 78 | * @var string 79 | */ 80 | protected $_oauthToken = null; 81 | 82 | /** 83 | * OAuth token 84 | * @var string 85 | */ 86 | protected $_accessToken = null; 87 | 88 | /** 89 | * OAuth user object 90 | * @var object 91 | */ 92 | protected $_currentUser = null; 93 | 94 | /** 95 | * Holds the HTTP client instance 96 | * @param Zend_Http_Client $httpClient 97 | */ 98 | protected $_httpClient = null; 99 | 100 | /** 101 | * Constructor needs to receive the config as an array 102 | * @param mixed $config 103 | */ 104 | public function __construct($config = null, $arrayResponses = false) { 105 | $this->_config = $config; 106 | $this->_arrayResponses = $arrayResponses; 107 | if (empty($config)) { 108 | throw new InstagramException('Configuration params are empty or not an array.'); 109 | } 110 | } 111 | 112 | /** 113 | * Instantiates the internal HTTP client 114 | * @param string $uri 115 | * @param string $method 116 | */ 117 | protected function _initHttpClient($uri, $method = CurlHttpClient::GET) { 118 | if ($this->_httpClient == null) { 119 | $this->_httpClient = new CurlHttpClient($uri); 120 | } else { 121 | $this->_httpClient->setUri($uri); 122 | } 123 | $this->_httpClient->setMethod($method); 124 | } 125 | 126 | /** 127 | * Returns the body of the HTTP client response 128 | * @return string 129 | */ 130 | protected function _getHttpClientResponse() { 131 | return $this->_httpClient->getResponse(); 132 | } 133 | 134 | /** 135 | * Retrieves the authorization code to be used in every request 136 | * @return string. The JSON encoded OAuth token 137 | */ 138 | protected function _setOauthToken() { 139 | $this->_initHttpClient($this->_endpointUrls['access_token'], CurlHttpClient::POST); 140 | $this->_httpClient->setPostParam('client_id', $this->_config['client_id']); 141 | $this->_httpClient->setPostParam('client_secret', $this->_config['client_secret']); 142 | $this->_httpClient->setPostParam('grant_type', $this->_config['grant_type']); 143 | $this->_httpClient->setPostParam('redirect_uri', $this->_config['redirect_uri']); 144 | $this->_httpClient->setPostParam('code', $this->getAccessCode()); 145 | 146 | $this->_oauthToken = $this->_getHttpClientResponse(); 147 | } 148 | 149 | /** 150 | * Return the decoded plain access token value 151 | * from the OAuth JSON encoded token. 152 | * @return string 153 | */ 154 | public function getAccessToken() { 155 | if ($this->_accessToken == null) { 156 | 157 | if ($this->_oauthToken == null) { 158 | $this->_setOauthToken(); 159 | } 160 | 161 | $this->_accessToken = json_decode($this->_oauthToken)->access_token; 162 | } 163 | 164 | return $this->_accessToken; 165 | } 166 | 167 | /** 168 | * Return the decoded user object 169 | * from the OAuth JSON encoded token 170 | * @return object 171 | */ 172 | public function getCurrentUser() { 173 | if ($this->_currentUser == null) { 174 | 175 | if ($this->_oauthToken == null) { 176 | $this->_setOauthToken(); 177 | } 178 | 179 | $this->_currentUser = json_decode($this->_oauthToken)->user; 180 | } 181 | 182 | return $this->_currentUser; 183 | } 184 | 185 | /** 186 | * Gets the code param received during the authorization step 187 | */ 188 | protected function getAccessCode() { 189 | return $_GET[self::RESPONSE_CODE_PARAM]; 190 | } 191 | 192 | /** 193 | * Sets the access token response from OAuth 194 | * @param string $accessToken 195 | */ 196 | public function setAccessToken($accessToken) { 197 | $this->_accessToken = $accessToken; 198 | } 199 | 200 | /** 201 | * Surf to Instagram credentials verification page. 202 | * If the user is already authenticated, redirects to 203 | * the URI set in the redirect_uri config param. 204 | * @return string 205 | */ 206 | public function openAuthorizationUrl() { 207 | header('Location: ' . $this->getAuthorizationUrl()); 208 | exit(1); 209 | } 210 | 211 | /** 212 | * Generate Instagram credentials verification page URL. 213 | * Usefull for creating a link to the Instagram authentification page. 214 | * @return string 215 | */ 216 | public function getAuthorizationUrl() { 217 | return sprintf($this->_endpointUrls['authorize'], 218 | $this->_config['client_id'], 219 | $this->_config['redirect_uri'], 220 | self::RESPONSE_CODE_PARAM); 221 | } 222 | 223 | /** 224 | * Setup subscription 225 | * @param $params array 226 | */ 227 | public function createSubscription($params) { 228 | $this->_initHttpClient($this->_endpointUrls['create_subscriptions'], CurlHttpClient::POST); 229 | $this->_httpClient->setPostParam('client_id', $this->_config['client_id']); 230 | $this->_httpClient->setPostParam('client_secret', $this->_config['client_secret']); 231 | $this->_httpClient->setPostParam('verify_token', $this->_config['verify_token']); 232 | $this->_httpClient->setPostParam('callback_url', $params['callback_url']); 233 | $this->_httpClient->setPostParam('object', $params['object']); 234 | $this->_httpClient->setPostParam('object_id', $params['object_id']); 235 | $this->_httpClient->setPostParam('aspect', $params['aspect']); 236 | return $this->_getHttpClientResponse(); 237 | } 238 | 239 | /** 240 | * List Subscriptions 241 | * @param $id 242 | */ 243 | public function listSubscriptions() { 244 | $getParams = array( 245 | 'client_id' => $this->_config['client_id'], 246 | 'client_secret' => $this->_config['client_secret'] 247 | ); 248 | $uri = sprintf($this->_endpointUrls['manage_subscriptions'], http_build_query($getParams)); 249 | $this->_initHttpClient($uri, CurlHttpClient::GET); 250 | return $this->_getHttpClientResponse(); 251 | } 252 | 253 | /** 254 | * Delete Subscription by id or type. 255 | * id=1 || object=all|tag|user 256 | * @param $params array 257 | */ 258 | public function deleteSubscription($params) { 259 | $getParams = array( 260 | 'client_id' => $this->_config['client_id'], 261 | 'client_secret' => $this->_config['client_secret'] 262 | ); 263 | if(isset($params['id'])) { 264 | $getParams['id'] = $params['id']; 265 | } else { 266 | $getParams['object'] = $params['object']; 267 | } 268 | $uri = sprintf($this->_endpointUrls['manage_subscriptions'], http_build_query($getParams)); 269 | $this->_initHttpClient($uri, CurlHttpClient::DELETE); 270 | return $this->_getHttpClientResponse(); 271 | } 272 | 273 | /** 274 | * Get basic information about a user. 275 | * @param $id 276 | */ 277 | public function getUser($id) { 278 | $endpointUrl = sprintf($this->_endpointUrls['user'], $id, $this->getAccessToken()); 279 | $this->_initHttpClient($endpointUrl); 280 | return $this->_getHttpClientResponse(); 281 | } 282 | 283 | /** 284 | * See the authenticated user's feed. 285 | * @param integer $maxId. Return media after this maxId. 286 | * @param integer $minId. Return media before this minId. 287 | */ 288 | public function getUserFeed($maxId = null, $minId = null, $count = null) { 289 | $endpointUrl = sprintf($this->_endpointUrls['user_feed'], http_build_query(array('access_token' => $this->getAccessToken(), 'max_id' => $maxId, 'min_id' => $minId, 'count' => $count))); 290 | $this->_initHttpClient($endpointUrl); 291 | return $this->_getHttpClientResponse(); 292 | } 293 | 294 | /** 295 | * Get information about the current user's relationship (follow/following/etc) to another user. 296 | * @param n/a 297 | */ 298 | public function getUserLiked() { 299 | $endpointUrl = sprintf($this->_endpointUrls['user_liked'], $this->getAccessToken()); 300 | $this->_initHttpClient($endpointUrl); 301 | return $this->_getHttpClientResponse(); 302 | } 303 | 304 | /** 305 | * Get the most recent media published by a user. 306 | * @param $id. User id 307 | * @param $maxId. Return media after this maxId 308 | * @param $minId. Return media before this minId 309 | * @param $maxTimestamp. Return media before this UNIX timestamp 310 | * @param $minTimestamp. Return media after this UNIX timestamp 311 | */ 312 | public function getUserRecent($id, $maxId = '', $minId = '', $maxTimestamp = '', $minTimestamp = '', $count = null) { 313 | $endpointUrl = sprintf($this->_endpointUrls['user_recent'], $id, $this->getAccessToken(), $maxId, $minId, $maxTimestamp, $minTimestamp, $count); 314 | $this->_initHttpClient($endpointUrl); 315 | return $this->_getHttpClientResponse(); 316 | } 317 | 318 | /** 319 | * Search for a user by name. 320 | * @param string $name. A query string 321 | */ 322 | public function searchUser($name) { 323 | $endpointUrl = sprintf($this->_endpointUrls['user_search'], $name, $this->getAccessToken()); 324 | $this->_initHttpClient($endpointUrl); 325 | return $this->_getHttpClientResponse(); 326 | } 327 | 328 | /** 329 | * Get the list of users this user follows. 330 | * @param $id. The user id. "self" is current user 331 | * @param integer $cursor. Cursor to paginate results 332 | */ 333 | public function getUserFollows($id, $cursor = '') { 334 | $endpointUrl = sprintf($this->_endpointUrls['user_follows'], $id, $this->getAccessToken(), $cursor); 335 | $this->_initHttpClient($endpointUrl); 336 | return $this->_getHttpClientResponse(); 337 | } 338 | 339 | /** 340 | * Get the list of users this user is followed by. 341 | * @param $id. The user id. "self" is current user 342 | */ 343 | public function getUserFollowedBy($id) { 344 | $endpointUrl = sprintf($this->_endpointUrls['user_followed_by'], $id, $this->getAccessToken()); 345 | $this->_initHttpClient($endpointUrl); 346 | return $this->_getHttpClientResponse(); 347 | } 348 | 349 | /** 350 | * List the users who have requested this user's permission to follow 351 | */ 352 | public function getUserRequestedBy() { 353 | $endpointUrl = sprintf($this->_endpointUrls['user_requested_by'], $this->getAccessToken()); 354 | $this->_initHttpClient($endpointUrl); 355 | return $this->_getHttpClientResponse(); 356 | } 357 | 358 | /** 359 | * Get information about the current user's relationship (follow/following/etc) to another user. 360 | * @param integer $id 361 | */ 362 | public function getUserRelationship($id) { 363 | $endpointUrl = sprintf($this->_endpointUrls['user_relationship'], $id, $this->getAccessToken()); 364 | $this->_initHttpClient($endpointUrl); 365 | return $this->_getHttpClientResponse(); 366 | } 367 | 368 | /** 369 | * Modify the relationship between the current user and the target user 370 | * In order to perform this action the scope must be set to 'relationships' 371 | * @param integer $id 372 | * @param string $action. One of follow/unfollow/block/unblock/approve/deny 373 | */ 374 | public function modifyUserRelationship($id, $action) { 375 | $endpointUrl = sprintf($this->_endpointUrls['modify_user_relationship'], $id, $action, $this->getAccessToken()); 376 | $this->_initHttpClient($endpointUrl, CurlHttpClient::POST); 377 | $this->_httpClient->setPostParam("action",$action); 378 | $this->_httpClient->setPostParam("access_token",$this->getAccessToken()); 379 | return $this->_getHttpClientResponse(); 380 | } 381 | 382 | /** 383 | * Get information about a media object. 384 | * @param integer $mediaId 385 | */ 386 | public function getMedia($id) { 387 | $endpointUrl = sprintf($this->_endpointUrls['media'], $id, $this->getAccessToken()); 388 | $this->_initHttpClient($endpointUrl); 389 | return $this->_getHttpClientResponse(); 390 | } 391 | 392 | /** 393 | * Search for media in a given area. 394 | * @param float $lat 395 | * @param float $lng 396 | * @param integer $maxTimestamp 397 | * @param integer $minTimestamp 398 | * @param integer $distance 399 | */ 400 | public function mediaSearch($lat, $lng, $maxTimestamp = '', $minTimestamp = '', $distance = '') { 401 | $endpointUrl = sprintf($this->_endpointUrls['media_search'], $lat, $lng, $maxTimestamp, $minTimestamp, $distance, $this->getAccessToken()); 402 | $this->_initHttpClient($endpointUrl); 403 | return $this->_getHttpClientResponse(); 404 | } 405 | 406 | /** 407 | * Get a list of what media is most popular at the moment. 408 | */ 409 | public function getPopularMedia() { 410 | $endpointUrl = sprintf($this->_endpointUrls['media_popular'], $this->getAccessToken()); 411 | $this->_initHttpClient($endpointUrl); 412 | return $this->_getHttpClientResponse(); 413 | } 414 | 415 | /** 416 | * Get a full list of comments on a media. 417 | * @param integer $id 418 | */ 419 | public function getMediaComments($id) { 420 | $endpointUrl = sprintf($this->_endpointUrls['media_comments'], $id, $this->getAccessToken()); 421 | $this->_initHttpClient($endpointUrl); 422 | return $this->_getHttpClientResponse(); 423 | } 424 | 425 | /** 426 | * Create a comment on a media. 427 | * @param integer $id 428 | * @param string $text 429 | */ 430 | public function postMediaComment($id, $text) { 431 | $endpointUrl = sprintf($this->_endpointUrls['post_media_comment'], $id, $text, $this->getAccessToken()); 432 | $this->_initHttpClient($endpointUrl, CurlHttpClient::POST); 433 | return $this->_getHttpClientResponse(); 434 | } 435 | 436 | /** 437 | * Remove a comment either on the authenticated user's media or authored by the authenticated user. 438 | * @param integer $mediaId 439 | * @param integer $commentId 440 | */ 441 | public function deleteComment($mediaId, $commentId) { 442 | $endpointUrl = sprintf($this->_endpointUrls['delete_media_comment'], $mediaId, $commentId, $this->getAccessToken()); 443 | $this->_initHttpClient($endpointUrl, CurlHttpClient::DELETE); 444 | return $this->_getHttpClientResponse(); 445 | } 446 | 447 | /** 448 | * Get a list of users who have liked this media. 449 | * @param integer $mediaId 450 | */ 451 | public function getLikes($mediaId) { 452 | $endpointUrl = sprintf($this->_endpointUrls['likes'], $mediaId, $this->getAccessToken()); 453 | $this->_initHttpClient($endpointUrl); 454 | return $this->_getHttpClientResponse(); 455 | } 456 | 457 | /** 458 | * Set a like on this media by the currently authenticated user. 459 | * @param integer $mediaId 460 | */ 461 | public function postLike($mediaId) { 462 | $endpointUrl = sprintf($this->_endpointUrls['post_like'], $mediaId); 463 | $this->_initHttpClient($endpointUrl, CurlHttpClient::POST); 464 | $this->_httpClient->setPostParam('access_token', $this->getAccessToken()); 465 | return $this->_getHttpClientResponse(); 466 | } 467 | 468 | /** 469 | * Remove a like on this media by the currently authenticated user. 470 | * @param integer $mediaId 471 | */ 472 | public function removeLike($mediaId) { 473 | $endpointUrl = sprintf($this->_endpointUrls['remove_like'], $mediaId, $this->getAccessToken()); 474 | $this->_initHttpClient($endpointUrl, CurlHttpClient::DELETE); 475 | return $this->_getHttpClientResponse(); 476 | } 477 | 478 | /** 479 | * Get information about a tag object. 480 | * @param string $tagName 481 | */ 482 | public function getTags($tagName) { 483 | $endpointUrl = sprintf($this->_endpointUrls['tags'], $tagName, $this->getAccessToken()); 484 | $this->_initHttpClient($endpointUrl); 485 | return $this->_getHttpClientResponse(); 486 | } 487 | 488 | /** 489 | * Get a list of recently tagged media. 490 | * @param string $tagName 491 | * @param integer $maxId 492 | * @param integer $minId 493 | */ 494 | public function getRecentTags($tagName, $maxId = '', $minId = '') { 495 | $endpointUrl = sprintf($this->_endpointUrls['tags_recent'], $tagName, $maxId, $minId, $this->getAccessToken()); 496 | $this->_initHttpClient($endpointUrl); 497 | return $this->_getHttpClientResponse(); 498 | } 499 | 500 | /** 501 | * Search for tags by name - results are ordered first as an exact match, then by popularity. 502 | * @param string $tagName 503 | */ 504 | public function searchTags($tagName) { 505 | $endpointUrl = sprintf($this->_endpointUrls['tags_search'], urlencode($tagName), $this->getAccessToken()); 506 | $this->_initHttpClient($endpointUrl); 507 | return $this->_getHttpClientResponse(); 508 | } 509 | 510 | /** 511 | * Get information about a location. 512 | * @param integer $id 513 | */ 514 | public function getLocation($id) { 515 | $endpointUrl = sprintf($this->_endpointUrls['locations'], $id, $this->getAccessToken()); 516 | $this->_initHttpClient($endpointUrl); 517 | return $this->_getHttpClientResponse(); 518 | } 519 | 520 | /** 521 | * Get a list of recent media objects from a given location. 522 | * @param integer $locationId 523 | */ 524 | public function getLocationRecentMedia($id, $maxId = '', $minId = '', $maxTimestamp = '', $minTimestamp = '') { 525 | $endpointUrl = sprintf($this->_endpointUrls['locations_recent'], $id, $maxId, $minId, $maxTimestamp, $minTimestamp, $this->getAccessToken()); 526 | $this->_initHttpClient($endpointUrl); 527 | return $this->_getHttpClientResponse(); 528 | } 529 | 530 | /** 531 | * Search for a location by name and geographic coordinate. 532 | * @see http://instagr.am/developer/endpoints/locations/#get_locations_search 533 | * @param float $lat 534 | * @param float $lng 535 | * @param integer $foursquareId 536 | * @param integer $distance 537 | */ 538 | public function searchLocation($lat, $lng, $foursquareId = '', $distance = '') { 539 | $endpointUrl = sprintf($this->_endpointUrls['locations_search'], $lat, $lng, $foursquareId, $distance, $this->getAccessToken()); 540 | $this->_initHttpClient($endpointUrl); 541 | return $this->_getHttpClientResponse(); 542 | } 543 | } 544 | 545 | class InstagramException extends Exception { 546 | } 547 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | ## This is a very simple instagram API implementation in PHP. 2 | 3 | Make sure you check out the example.php file that is provided along with the code which is almost self explanatory. 4 | 5 | -------------------------------------------------------------------------------- /callback.php: -------------------------------------------------------------------------------- 1 | '', // Your client id 26 | 'client_secret' => '', // Your client secret 27 | 'grant_type' => 'authorization_code', 28 | 'redirect_uri' => '', // The redirect URI you provided when signed up for the service 29 | ); 30 | 31 | // Instantiate the API handler object 32 | $instagram = new Instagram($config); 33 | $accessToken = $instagram->getAccessToken(); 34 | $_SESSION['InstagramAccessToken'] = $accessToken; 35 | 36 | $instagram->setAccessToken($_SESSION['InstagramAccessToken']); 37 | $popular = $instagram->getPopularMedia(); 38 | 39 | // After getting the response, let's iterate the payload 40 | $response = json_decode($popular, true); 41 | ?> 42 | 43 | 44 |
45 | 46 | 47 |