forked from msigley/WP-Instagram-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
40 lines (33 loc) · 1.41 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
/* Endpoint API */
function instagram_get_user_items( $query_args=array() ) {
$Instagram_WP_API = Instagram_WP_API::object();
if( empty( $query_args['limit'] ) || 0 > $query_args['limit'] )
$query_args['limit'] = 25;
if( $query_args['limit'] > 1000 )
$query_args['limit'] = 1000;
if( empty( $query_args['fields'] ) )
$query_args['fields'] = 'id,caption,media_type,media_url,permalink,thumbnail_url,timestamp,username';
$response = $Instagram_WP_API->rest->request( "me/media", "get", $query_args );
if( empty($response) )
return false;
//Handle instagram pagination
//Only concatonate max 10 pages for performance and sanity
$response_data = $response->data;
$response->count = count( $response_data );
$i = 0;
while( $response->count < $query_args['limit'] && $i < 10 ) {
$next_page_args = array( 'after' => $response->paging->after );
$next_page_args['limit'] = 100; // 100 is the max limit in one request
$page_response = $Instagram_WP_API->rest->request( "me/media", "get", $next_page_args );
if( !empty($page_response->data) ) {
$response_data = array_merge( $response_data, $page_response->data );
$response->count += count( $page_response->data );
}
$i++;
}
if( $response->count > $query_args['limit'] )
$response_data = array_slice( $response_data, 0, $query_args['limit'], true );
$response->data = $response_data;
return $response;
}