Skip to content

Commit

Permalink
Merge branch 'get_user_pages'
Browse files Browse the repository at this point in the history
  • Loading branch information
jstolpe committed Jun 14, 2022
2 parents 401b56e + ca0da8a commit 26ae9f1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Instagram/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,30 @@ public function setPrevNextLinks( &$response ) {
public function setAccessToken( $accessToken ) {
$this->accessToken = $accessToken;
}

/**
* Calculate next link based on the cursors.
*
* @param string $type type of link after or before.
* @param array $response Instagram api response.
* @param array $params specific request params.
* @return void
*/
public function calcLinkFromCursor( $type, &$response, $endpoint, $params ) {
if ( isset( $response[Fields::PAGING][Fields::CURSORS][$type] ) ) { // we have paging
// set the after cursor
$params[$type] = $response[Fields::PAGING][Fields::CURSORS][$type];

// create our request
$request = new Request( Request::METHOD_GET, $endpoint, $params, $this->graphVersion, $this->accessToken );

// set paging type based
$pagingOrder = Params::AFTER == $type ? Params::NEXT : Params::PREVIOUS;

// set paging next to the url for the next request
$response[Fields::PAGING][$pagingOrder] = $request->getUrl();
}
}
}

?>
1 change: 1 addition & 0 deletions src/Instagram/Request/Params.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
class Params {
const ACCESS_TOKEN = 'access_token';
const AFTER = 'after';
const BEFORE = 'before';
const CAPTION = 'caption';
const CHILDREN = 'children';
const CLIENT_ID = 'client_id';
Expand Down
33 changes: 33 additions & 0 deletions src/Instagram/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
* @version 1.0
*/
class User extends Instagram {
/**
* @const Instagram endpoint for the request.
*/
const ENDPOINT = 'me/accounts';

/**
* @var string $userId Instagram user id.
*/
Expand Down Expand Up @@ -95,6 +100,34 @@ public function getSelf( $params = array() ) {
// return response
return $response;
}

/**
* Get the users facebook pages.
*
* @param array $params params for the GET request.
* @return Instagram response.
*/
public function getUserPages( $params = array() ) {
$getParams = array( // parameters for our endpoint
'endpoint' => '/' . self::ENDPOINT,
'params' => $params
);

// ig get request
$response = $this->get( $getParams );

// calculate the next link for paging
$this->calcLinkFromCursor( Params::AFTER, $response, $getParams['endpoint'], $params );

// calcuate the before link
$this->calcLinkFromCursor( Params::BEFORE, $response, $getParams['endpoint'], $params );

// set prev and next links
$this->setPrevNextLinks( $response );

// return response
return $response;
}
}

?>

0 comments on commit 26ae9f1

Please sign in to comment.