From 14b78ea5f49cc8017d3252a4b6b681cdddaa29fe Mon Sep 17 00:00:00 2001 From: Dgbusiness Date: Thu, 30 Mar 2023 10:46:23 -0400 Subject: [PATCH] getAccount() fixed to get all missing user data. --- src/InstagramScraper/Endpoints.php | 2 +- src/InstagramScraper/Instagram.php | 11 +++++++---- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/InstagramScraper/Endpoints.php b/src/InstagramScraper/Endpoints.php index bd09225f..89e7ece7 100644 --- a/src/InstagramScraper/Endpoints.php +++ b/src/InstagramScraper/Endpoints.php @@ -6,7 +6,7 @@ class Endpoints { const BASE_URL = 'https://www.instagram.com'; const LOGIN_URL = 'https://www.instagram.com/accounts/login/ajax/'; - const ACCOUNT_PAGE = 'https://www.instagram.com/{username}/'; + const ACCOUNT_PAGE = 'https://www.instagram.com/api/v1/users/web_profile_info/?username={username}'; const MEDIA_LINK = 'https://www.instagram.com/p/{code}'; const ACCOUNT_MEDIAS = 'https://www.instagram.com/graphql/query/?query_hash=e769aa130647d2354c40ea6a439bfc08&variables={variables}'; const ACCOUNT_TAGGED_MEDIAS = 'https://www.instagram.com/graphql/query/?query_hash=be13233562af2d229b008d2976b998b5&variables={variables}'; diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index ade8da51..382bb548 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -415,6 +415,9 @@ private function generateHeaders($session, $gisToken = null) } + //user agent to retrieve account data + $this->setUserAgent("Instagram 219.0.0.12.117 Android"); + if ($this->getUserAgent()) { $headers['user-agent'] = $this->getUserAgent(); @@ -738,11 +741,11 @@ public function getAccount($username) throw new InstagramAgeRestrictedException('Account with given username is age-restricted.'); } - if (!isset($userArray['entry_data']['ProfilePage'][0]['graphql']['user'])) { + if (!isset($userArray['entry_data']['ProfilePage'][0]['graphql']['user']) && !isset($userArray['data']['user'])) { throw new InstagramException('Response code is ' . $response->code . ': ' . static::httpCodeToString($response->code) . '.' . 'Something went wrong. Please report issue.', $response->code, static::getErrorBody($response->body)); } - return Account::create($userArray['entry_data']['ProfilePage'][0]['graphql']['user']); + return Account::create($userArray['entry_data']['ProfilePage'][0]['graphql']['user']?? $userArray['data']['user']); } public function getAccountInfo($username) @@ -772,7 +775,7 @@ private static function extractSharedDataFromBody($body) if (preg_match_all('#\_sharedData \= (.*?)\;\<\/script\>#', $body, $out)) { return json_decode($out[1][0], true, 512, JSON_BIGINT_AS_STRING); } - return null; + return json_decode($body, true, 512, JSON_BIGINT_AS_STRING); } private function isAccountAgeRestricted($userArray, $body) @@ -1430,7 +1433,7 @@ public function getMediasByTag($tag, $count = 12, $maxId = '', $minTimestamp = n if (empty($nodes)) { return $medias; } - $maxId = $arr[$rootKey]['recent']['next_max_id']; // $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['page_info']['end_cursor']; + $maxId = $arr[$rootKey]['recent']['next_max_id']?? ''; // $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['page_info']['end_cursor']; $hasNextPage = $arr[$rootKey]['recent']['more_available']; // $arr[$rootKey]['hashtag']['edge_hashtag_to_media']['page_info']['has_next_page']; } return $medias;