5 * @author Galen Grover <galenjr@gmail.com>
6 * @license http://opensource.org/licenses/mit-license.php The MIT License
11 use \Instagram\Collection\MediaSearchCollection;
12 use \Instagram\Collection\TagCollection;
13 use \Instagram\Collection\TagMediaCollection;
14 use \Instagram\Collection\UserCollection;
15 use \Instagram\Collection\MediaCollection;
16 use \Instagram\Collection\LocationCollection;
17 use \Instagram\CurrentUser;
21 use \Instagram\Location;
26 * All objects are created through this object
28 class Instagram extends \Instagram\Core\BaseObjectAbstract {
33 * You can supply a client, proxy, and an access token via the config array
35 * @param string $access_token Instagram access token obtained through authentication
36 * @param \Instagram\Net\ClientInterface $client Client object used to connect to the API
39 public function __construct( $access_token = null, \Instagram\Net\ClientInterface $client = null ) {
40 $this->proxy = new \Instagram\Core\Proxy( $client ?: new \Instagram\Net\CurlClient, $access_token ?: null );
44 * Set the access token
46 * Most API calls require an access ID
48 * @param string $access_token
51 public function setAccessToken( $access_token ) {
52 $this->proxy->setAccessToken( $access_token );
58 * Some API calls can be called with only a Client ID
60 * @param string $client_id Client ID
63 public function setClientID( $client_id ) {
64 $this->proxy->setClientId( $client_id );
70 * This doesn't actually work yet, waiting for Instagram to implement it in their API
74 public function logout() {
75 $this->proxy->logout();
81 * Retrieve a user given his/her ID
83 * @param int $id ID of the user to retrieve
84 * @return \Instagram\User
87 public function getUser( $id ) {
88 $user = new User( $this->proxy->getUser( $id ), $this->proxy );
93 * Get user by Username
95 * Retrieve a user given their username
97 * @param string $username Username of the user to retrieve
98 * @return \Instagram\User
100 * @throws \Instagram\ApiException
102 public function getUserByUsername( $username ) {
103 $user = $this->searchUsers( $username, array( 'count' => 1 ) )->getItem( 0 );
106 return $this->getUser( $user->getId() );
107 } catch( \Instagram\Core\ApiException $e ) {
108 if ( $e->getType() == $e::TYPE_NOT_ALLOWED ) {
113 throw new \Instagram\Core\ApiException( 'username not found', 400, 'InvalidUsername' );
117 * Check if a user is private
122 public function isUserPrivate( $user_id ) {
123 $relationship = $this->proxy->getRelationshipToCurrentUser( $user_id );
124 return (bool)$relationship->target_user_is_private;
130 * Retreive a media object given it's ID
132 * @param int $id ID of the media to retrieve
133 * @return \Instagram\Media
136 public function getMedia( $id ) {
137 $media = new Media( $this->proxy->getMedia( $id ), $this->proxy );
144 * @param string $tag Tag to retrieve
145 * @return \Instagram\Tag
148 public function getTag( $tag ) {
149 $tag = new Tag( $this->proxy->getTag( $tag ), $this->proxy );
156 * Retreive a location given it's ID
158 * @param int $id ID of the location to retrieve
159 * @return \Instagram\Location
162 public function getLocation( $id ) {
163 $location = new Location( $this->proxy->getLocation( $id ), $this->proxy );
170 * Returns the current user wrapped in a CurrentUser object
172 * @return \Instagram\CurrentUser
175 public function getCurrentUser() {
176 $current_user = new CurrentUser( $this->proxy->getCurrentUser(), $this->proxy );
177 return $current_user;
183 * Returns current popular media
185 * @return \Instagram\Collection\MediaCollection
188 public function getPopularMedia() {
189 $popular_media = new MediaCollection( $this->proxy->getPopularMedia(), $this->proxy );
190 return $popular_media;
196 * Search the users by username
198 * @param string $query Search query
199 * @param array $params Optional params to pass to the endpoint
200 * @return \Instagram\Collection\UserCollection
203 public function searchUsers( $query, array $params = null ) {
204 $params = (array)$params;
205 $params['q'] = $query;
206 $user_collection = new UserCollection( $this->proxy->searchUsers( $params ), $this->proxy );
207 return $user_collection;
213 * Returns media that is a certain distance from a given lat/lng
215 * To specify a distance, pass the distance (in meters) in the $params
217 * Default distance is 1000m
219 * @param float $lat Latitude of the search
220 * @param float $lng Longitude of the search
221 * @param array $params Optional params to pass to the endpoint
222 * @return \Instagram\Collection\MediaSearchCollection
225 public function searchMedia( $lat, $lng, array $params = null ) {
226 $params = (array)$params;
227 $params['lat'] = (float)$lat;
228 $params['lng'] = (float)$lng;
229 $media_collection = new MediaSearchCollection( $this->proxy->searchMedia( $params ), $this->proxy );
230 return $media_collection;
236 * @param string $query Search query
237 * @param array $params Optional params to pass to the endpoint
238 * @return \Instagram\Collection\TagCollection
241 public function searchTags( $query, array $params = null ) {
242 $params = (array)$params;
243 $params['q'] = $query;
244 $tag_collection = new TagCollection( $this->proxy->searchTags( $params ), $this->proxy );
245 return $tag_collection;
251 * Returns locations that are a certain distance from a given lat/lng
253 * To specify a distance, pass the distance (in meters) in the $params
255 * Default distance is 1000m
257 * @param float $lat Latitude of the search
258 * @param float $lng Longitude of the search
259 * @param array $params Optional params to pass to the endpoint
260 * @return \Instagram\LocationCollection
263 public function searchLocations( $lat, $lng, array $params = null ) {
264 $params = (array)$params;
265 $params['lat'] = (float)$lat;
266 $params['lng'] = (float)$lng;
267 $location_collection = new LocationCollection( $this->proxy->searchLocations( $params ), $this->proxy );
268 return $location_collection;
275 * @param array $params Optional params to pass to the endpoint
276 * @return TagMediaCollection
278 public function getTagMedia( $tag, array $params = null ) {
279 $params = (array)$params;
280 return new TagMediaCollection( $this->proxy->getTagMedia( $tag, $params ), $this->proxy );