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 | defined('INSTAGRAM_ROOT') or define('INSTAGRAM_ROOT', realpath(dirname(__FILE__))); |
---|
10 | |
---|
11 | include_once(INSTAGRAM_ROOT.'/Core/Proxy.php'); |
---|
12 | include_once(INSTAGRAM_ROOT.'/Core/ApiException.php'); |
---|
13 | include_once(INSTAGRAM_ROOT.'/Net/ClientInterface.php'); |
---|
14 | include_once(INSTAGRAM_ROOT.'/Net/CurlClient.php'); |
---|
15 | include_once(INSTAGRAM_ROOT.'/Collection/MediaSearchCollection.php'); |
---|
16 | include_once(INSTAGRAM_ROOT.'/Collection/TagCollection.php'); |
---|
17 | include_once(INSTAGRAM_ROOT.'/Collection/UserCollection.php'); |
---|
18 | include_once(INSTAGRAM_ROOT.'/Collection/MediaCollection.php'); |
---|
19 | include_once(INSTAGRAM_ROOT.'/Collection/LocationCollection.php'); |
---|
20 | include_once(INSTAGRAM_ROOT.'/CurrentUser.php'); |
---|
21 | include_once(INSTAGRAM_ROOT.'/User.php'); |
---|
22 | include_once(INSTAGRAM_ROOT.'/Media.php'); |
---|
23 | include_once(INSTAGRAM_ROOT.'/Tag.php'); |
---|
24 | include_once(INSTAGRAM_ROOT.'/Location.php'); |
---|
25 | |
---|
26 | |
---|
27 | /** |
---|
28 | * Instagram! |
---|
29 | * |
---|
30 | * All objects are created through this object |
---|
31 | */ |
---|
32 | class Instagram extends Instagram_Core_BaseObjectAbstract { |
---|
33 | |
---|
34 | /** |
---|
35 | * Constructor |
---|
36 | * |
---|
37 | * You can supply a client, proxy, and an access token via the config array |
---|
38 | * |
---|
39 | * @param string $access_token Instagram access token obtained through authentication |
---|
40 | * @param Instagram_Net_ClientInterface $client Client object used to connect to the API |
---|
41 | * @access public |
---|
42 | */ |
---|
43 | public function __construct( $access_token = null, Instagram_Net_ClientInterface $client = null ) { |
---|
44 | $this->proxy = new Instagram_Core_Proxy( $client ? $client : new Instagram_Net_CurlClient, $access_token ? $access_token : null ); |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * Enable cache system |
---|
49 | * @see Instagram_Core_Proxy->enableCache() |
---|
50 | */ |
---|
51 | function enableCache ( $directory, $cache_expire = 600 ) { |
---|
52 | $this->proxy->enableCache( $directory, $cache_expire ); |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * Set the access token |
---|
57 | * |
---|
58 | * Most API calls require an access ID |
---|
59 | * |
---|
60 | * @param string $access_token |
---|
61 | * @access public |
---|
62 | */ |
---|
63 | public function setAccessToken( $access_token ) { |
---|
64 | $this->proxy->setAccessToken( $access_token ); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Set the client ID |
---|
69 | * |
---|
70 | * Some API calls can be called with only a Client ID |
---|
71 | * |
---|
72 | * @param string $client_id Client ID |
---|
73 | * @access public |
---|
74 | */ |
---|
75 | public function setClientID( $client_id ) { |
---|
76 | $this->proxy->setClientId( $client_id ); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * Logout |
---|
81 | * |
---|
82 | * This doesn't actually work yet, waiting for Instagram to implement it in their API |
---|
83 | * |
---|
84 | * @access public |
---|
85 | */ |
---|
86 | public function logout() { |
---|
87 | $this->proxy->logout(); |
---|
88 | } |
---|
89 | |
---|
90 | /** |
---|
91 | * Get user |
---|
92 | * |
---|
93 | * Retrieve a user given his/her ID |
---|
94 | * |
---|
95 | * @param int $id ID of the user to retrieve |
---|
96 | * @return Instagram_User |
---|
97 | * @access public |
---|
98 | */ |
---|
99 | public function getUser( $id ) { |
---|
100 | $user = new User( $this->proxy->getUser( $id ), $this->proxy ); |
---|
101 | return $user; |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * Get user by Username |
---|
106 | * |
---|
107 | * Retrieve a user given their username |
---|
108 | * |
---|
109 | * @param string $username Username of the user to retrieve |
---|
110 | * @return Instagram_User |
---|
111 | * @access public |
---|
112 | * @throws Instagram_ApiException |
---|
113 | */ |
---|
114 | public function getUserByUsername( $username ) { |
---|
115 | $user = $this->searchUsers( $username, array( 'count' => 1 ) )->getItem( 0 ); |
---|
116 | if ( $user ) { |
---|
117 | return $this->getUser( $user->getId() ); |
---|
118 | } |
---|
119 | throw new Instagram_Core_ApiException( 'username not found', 400, 'InvalidUsername' ); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Check if a user is private |
---|
124 | * |
---|
125 | * @return bool |
---|
126 | * @access public |
---|
127 | */ |
---|
128 | public function isUserPrivate( $user_id ) { |
---|
129 | $relationship = $this->proxy->getRelationshipToCurrentUser( $user_id ); |
---|
130 | return (bool)$relationship->target_user_is_private; |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Get media |
---|
135 | * |
---|
136 | * Retreive a media object given it's ID |
---|
137 | * |
---|
138 | * @param int $id ID of the media to retrieve |
---|
139 | * @return Instagram_Media |
---|
140 | * @access public |
---|
141 | */ |
---|
142 | public function getMedia( $id ) { |
---|
143 | $media = new Instagram_Media( $this->proxy->getMedia( $id ), $this->proxy ); |
---|
144 | return $media; |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * Get Tag |
---|
149 | * |
---|
150 | * @param string $tag Tag to retrieve |
---|
151 | * @return \Instagram\Tag |
---|
152 | * @access public |
---|
153 | */ |
---|
154 | public function getTag( $tag ) { |
---|
155 | $tag = new Instagram_Tag( $this->proxy->getTag( $tag ), $this->proxy ); |
---|
156 | return $tag; |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * Get location |
---|
161 | * |
---|
162 | * Retreive a location given it's ID |
---|
163 | * |
---|
164 | * @param int $id ID of the location to retrieve |
---|
165 | * @return Instagram_Location |
---|
166 | * @access public |
---|
167 | */ |
---|
168 | public function getLocation( $id ) { |
---|
169 | $location = new Instagram_Location( $this->proxy->getLocation( $id ), $this->proxy ); |
---|
170 | return $location; |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * Get current user |
---|
175 | * |
---|
176 | * Returns the current user wrapped in a Instagram_CurrentUser object |
---|
177 | * |
---|
178 | * @return Instagram_CurrentUser |
---|
179 | * @access public |
---|
180 | */ |
---|
181 | public function getCurrentUser() { |
---|
182 | $current_user = new Instagram_CurrentUser( $this->proxy->getCurrentUser(), $this->proxy ); |
---|
183 | return $current_user; |
---|
184 | } |
---|
185 | |
---|
186 | /** |
---|
187 | * Get popular media |
---|
188 | * |
---|
189 | * Returns current popular media |
---|
190 | * |
---|
191 | * @return Instagram_Collection_MediaCollection |
---|
192 | * @access public |
---|
193 | */ |
---|
194 | public function getPopularMedia() { |
---|
195 | $popular_media = new Instagram_Collection_MediaCollection( $this->proxy->getPopularMedia(), $this->proxy ); |
---|
196 | return $popular_media; |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * Search users |
---|
201 | * |
---|
202 | * Search the users by username |
---|
203 | * |
---|
204 | * @param string $query Search query |
---|
205 | * @param array $params Optional params to pass to the endpoint |
---|
206 | * @return Instagram_Collection_UserCollection |
---|
207 | * @access public |
---|
208 | */ |
---|
209 | public function searchUsers( $query, array $params = null ) { |
---|
210 | $params = (array)$params; |
---|
211 | $params['q'] = $query; |
---|
212 | $user_collection = new Instagram_Collection_UserCollection( $this->proxy->searchUsers( $params ), $this->proxy ); |
---|
213 | return $user_collection; |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * Search Media |
---|
218 | * |
---|
219 | * Returns media that is a certain distance from a given lat/lng |
---|
220 | * |
---|
221 | * To specify a distance, pass the distance (in meters) in the $params |
---|
222 | * |
---|
223 | * Default distance is 1000m |
---|
224 | * |
---|
225 | * @param float $lat Latitude of the search |
---|
226 | * @param float $lng Longitude of the search |
---|
227 | * @param array $params Optional params to pass to the endpoint |
---|
228 | * @return Instagram_Collection_MediaSearchCollection |
---|
229 | * @access public |
---|
230 | */ |
---|
231 | public function searchMedia( $lat, $lng, array $params = null ) { |
---|
232 | $params = (array)$params; |
---|
233 | $params['lat'] = (float)$lat; |
---|
234 | $params['lng'] = (float)$lng; |
---|
235 | $media_collection = new Instagram_Collection_MediaSearchCollection( $this->proxy->searchMedia( $params ), $this->proxy ); |
---|
236 | return $media_collection; |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | * Search for tags |
---|
241 | * |
---|
242 | * @param string $query Search query |
---|
243 | * @param array $params Optional params to pass to the endpoint |
---|
244 | * @return Instagram_Collection_TagCollection |
---|
245 | * @access public |
---|
246 | */ |
---|
247 | public function searchTags( $query, array $params = null ) { |
---|
248 | $params = (array)$params; |
---|
249 | $params['q'] = $query; |
---|
250 | $tag_collection = new Instagram_Collection_TagCollection( $this->proxy->searchTags( $params ), $this->proxy ); |
---|
251 | return $tag_collection; |
---|
252 | } |
---|
253 | |
---|
254 | /** |
---|
255 | * Search Locations |
---|
256 | * |
---|
257 | * Returns locations that are a certain distance from a given lat/lng |
---|
258 | * |
---|
259 | * To specify a distance, pass the distance (in meters) in the $params |
---|
260 | * |
---|
261 | * Default distance is 1000m |
---|
262 | * |
---|
263 | * @param float $lat Latitude of the search |
---|
264 | * @param float $lng Longitude of the search |
---|
265 | * @param array $params Optional params to pass to the endpoint |
---|
266 | * @return Instagram_LocationCollection |
---|
267 | * @access public |
---|
268 | */ |
---|
269 | public function searchLocations( $lat, $lng, array $params = null ) { |
---|
270 | $params = (array)$params; |
---|
271 | $params['lat'] = (float)$lat; |
---|
272 | $params['lng'] = (float)$lng; |
---|
273 | $location_collection = new Instagram_Collection_LocationCollection( $this->proxy->searchLocations( $params ), $this->proxy ); |
---|
274 | return $location_collection; |
---|
275 | } |
---|
276 | |
---|
277 | } |
---|
278 | ?> |
---|