Skip to content

Commit

Permalink
Merge pull request #138 from e-beach/master
Browse files Browse the repository at this point in the history
Fix getAccountById failure using follow url.
  • Loading branch information
raiym authored Jul 12, 2017
2 parents e0de8aa + 6d6722c commit 02c334f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
5 changes: 5 additions & 0 deletions src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,9 @@ public static function getGraphQlUrl($queryId, $parameters)
}
return $url;
}

public static function getFollowUrl($accountId){
$url = str_replace('{{accountId}}', urlencode($accountId), Endpoints::FOLLOW_URL);
return $url;
}
}
26 changes: 9 additions & 17 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,14 @@ private static function parseCookies($rawCookies)

public function getAccountById($id)
{
// Use the follow page to get the account. The follow url will redirect to the home page for the user,
// which has the username embedded in the url.

if (!is_numeric($id)) {
throw new \InvalidArgumentException('User id must be integer or integer wrapped in string');
}

$p['id'] = $id;
$p['first'] = 1;
$url = Endpoints::getGraphQlUrl(InstagramQueryId::USER_MEDIAS, $p);
$url = Endpoints::getFollowUrl($id);
$response = Request::get($url, $this->generateHeaders($this->userSession));

if ($response->code !== 200) {
Expand All @@ -314,21 +315,12 @@ public function getAccountById($id)
$cookies = self::parseCookies($response->headers['Set-Cookie']);
$this->userSession['csrftoken'] = $cookies['csrftoken'];

$userArray = json_decode($response->raw_body, true);
if ($userArray['status'] === 'fail') {
throw new InstagramException($userArray['message']);
}
if (!isset($userArray['data']['user']) || $userArray['data']['user'] == null) {
throw new InstagramNotFoundException("User with this id not found");
}
$edges = $userArray['data']['user']['edge_owner_to_timeline_media']['edges'];
if (sizeof($edges) == 0) {
throw new InstagramNotFoundException("User exists but library could not pull information about user");
}
$shortcode = $edges[0]['node']['shortcode'];
// Get the username from the response url.
$responseUrl = $response->headers['Location'];
$urlParts = explode('/', rtrim($responseUrl, '/'));
$username = end($urlParts);

$media = $this->getMediaByCode($shortcode);
return $media->owner;
return self::getAccount($username);
}

// TODO: use new
Expand Down

0 comments on commit 02c334f

Please sign in to comment.