Version 1
[yaffs-website] / vendor / php-instagram-api / php-instagram-api / Instagram / Instagram.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;
10
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;
18 use \Instagram\User;
19 use \Instagram\Media;
20 use \Instagram\Tag;
21 use \Instagram\Location;
22
23 /**
24  * Instagram!
25  *
26  * All objects are created through this object
27  */
28 class Instagram extends \Instagram\Core\BaseObjectAbstract {
29
30     /**
31      * Constructor
32      *
33      * You can supply a client, proxy, and an access token via the config array
34      *
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
37      * @access public
38      */
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 );
41     }
42
43     /**
44      * Set the access token
45      *
46      * Most API calls require an access ID
47      * 
48      * @param string $access_token
49      * @access public
50      */
51     public function setAccessToken( $access_token ) {
52         $this->proxy->setAccessToken( $access_token );
53     }
54
55     /**
56      * Set the client ID
57      *
58      * Some API calls can be called with only a Client ID
59      * 
60      * @param string $client_id Client ID
61      * @access public
62      */
63     public function setClientID( $client_id ) {
64         $this->proxy->setClientId( $client_id );
65     }
66
67     /**
68      * Logout
69      *
70      * This doesn't actually work yet, waiting for Instagram to implement it in their API
71      *
72      * @access public
73      */
74     public function logout() {
75         $this->proxy->logout();
76     }
77
78     /**
79      * Get user
80      *
81      * Retrieve a user given his/her ID
82      *
83      * @param int $id ID of the user to retrieve
84      * @return \Instagram\User
85      * @access public
86      */
87     public function getUser( $id ) {
88         $user = new User( $this->proxy->getUser( $id ), $this->proxy );
89         return $user;
90     }
91
92     /**
93      * Get user by Username
94      *
95      * Retrieve a user given their username
96      *
97      * @param string $username Username of the user to retrieve
98      * @return \Instagram\User
99      * @access public
100      * @throws \Instagram\ApiException
101      */
102     public function getUserByUsername( $username ) {
103         $user = $this->searchUsers( $username, array( 'count' => 1 ) )->getItem( 0 );
104         if ( $user ) {
105             try {
106                 return $this->getUser( $user->getId() );
107             } catch( \Instagram\Core\ApiException $e ) {
108                 if ( $e->getType() == $e::TYPE_NOT_ALLOWED ) {
109                     return $user;
110                 }
111             }
112         }
113         throw new \Instagram\Core\ApiException( 'username not found', 400, 'InvalidUsername' );
114     }
115
116     /**
117      * Check if a user is private
118      *
119      * @return bool
120      * @access public
121      */
122     public function isUserPrivate( $user_id ) {
123         $relationship = $this->proxy->getRelationshipToCurrentUser( $user_id );
124         return (bool)$relationship->target_user_is_private;
125     }
126
127     /**
128      * Get media
129      *
130      * Retreive a media object given it's ID
131      *
132      * @param int $id ID of the media to retrieve
133      * @return \Instagram\Media
134      * @access public
135      */
136     public function getMedia( $id ) {
137         $media = new Media( $this->proxy->getMedia( $id ), $this->proxy );
138         return $media;
139     }
140
141     /**
142      * Get Tag
143      *
144      * @param string $tag Tag to retrieve
145      * @return \Instagram\Tag
146      * @access public
147      */
148     public function getTag( $tag ) {
149         $tag = new Tag( $this->proxy->getTag( $tag ), $this->proxy );
150         return $tag;
151     }
152
153     /**
154      * Get location
155      *
156      * Retreive a location given it's ID
157      *
158      * @param int $id ID of the location to retrieve
159      * @return \Instagram\Location
160      * @access public
161      */
162     public function getLocation( $id ) {
163         $location = new Location( $this->proxy->getLocation( $id ), $this->proxy );
164         return $location;
165     }
166
167     /**
168      * Get current user
169      *
170      * Returns the current user wrapped in a CurrentUser object
171      *
172      * @return \Instagram\CurrentUser
173      * @access public
174      */
175     public function getCurrentUser() {
176         $current_user = new CurrentUser( $this->proxy->getCurrentUser(), $this->proxy );
177         return $current_user;
178     }
179
180     /**
181      * Get popular media
182      *
183      * Returns current popular media
184      *
185      * @return \Instagram\Collection\MediaCollection
186      * @access public
187      */
188     public function getPopularMedia() {
189         $popular_media = new MediaCollection( $this->proxy->getPopularMedia(), $this->proxy );
190         return $popular_media;
191     }
192
193     /**
194      * Search users
195      *
196      * Search the users by username
197      *
198      * @param string $query Search query
199      * @param array $params Optional params to pass to the endpoint
200      * @return \Instagram\Collection\UserCollection
201      * @access public
202      */
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;
208     }
209
210     /**
211      * Search Media
212      *
213      * Returns media that is a certain distance from a given lat/lng
214      *
215      * To specify a distance, pass the distance (in meters) in the $params
216      *
217      * Default distance is 1000m
218      *
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
223      * @access public
224      */
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;
231     }
232
233     /**
234      * Search for tags
235      *
236      * @param string $query Search query
237      * @param array $params Optional params to pass to the endpoint
238      * @return \Instagram\Collection\TagCollection
239      * @access public
240      */
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;
246     }
247
248     /**
249      * Search Locations
250      *
251      * Returns locations that are a certain distance from a given lat/lng
252      *
253      * To specify a distance, pass the distance (in meters) in the $params
254      *
255      * Default distance is 1000m
256      *
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
261      * @access public
262      */
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;
269     }
270
271     /**
272      * Get tag media
273      *
274      * @param string $tag
275      * @param array $params Optional params to pass to the endpoint
276      * @return TagMediaCollection
277      */
278     public function getTagMedia( $tag, array $params = null ) {
279         $params = (array)$params;
280         return new TagMediaCollection( $this->proxy->getTagMedia( $tag, $params ), $this->proxy );
281     }
282
283 }