From 534a655612c176d9ce564d6d52388f1315050d80 Mon Sep 17 00:00:00 2001 From: "Georges.L" Date: Fri, 20 Oct 2017 13:51:38 +0200 Subject: [PATCH 1/3] Simplified endpoints urls --- src/InstagramScraper/Endpoints.php | 40 +++++++++++++++--------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/InstagramScraper/Endpoints.php b/src/InstagramScraper/Endpoints.php index 3b9c2d44..e5638f58 100644 --- a/src/InstagramScraper/Endpoints.php +++ b/src/InstagramScraper/Endpoints.php @@ -5,33 +5,33 @@ 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 MEDIA_LINK = 'https://www.instagram.com/p/{code}'; - const ACCOUNT_MEDIAS = 'https://www.instagram.com/{username}/media/?max_id={max_id}'; - const ACCOUNT_JSON_INFO = 'https://www.instagram.com/{username}/?__a=1'; - const MEDIA_JSON_INFO = 'https://www.instagram.com/p/{code}/?__a=1'; - const MEDIA_JSON_BY_LOCATION_ID = 'https://www.instagram.com/explore/locations/{{facebookLocationId}}/?__a=1&max_id={{maxId}}'; - const MEDIA_JSON_BY_TAG = 'https://www.instagram.com/explore/tags/{tag}/?__a=1&max_id={max_id}'; - const GENERAL_SEARCH = 'https://www.instagram.com/web/search/topsearch/?query={query}'; + const LOGIN_URL = self:: BASE_URL . '/accounts/login/ajax/'; + const ACCOUNT_PAGE = self:: BASE_URL . '/{username}'; + const MEDIA_LINK = self:: BASE_URL . '/p/{code}'; + const ACCOUNT_MEDIAS = self:: BASE_URL . '/{username}/media/?max_id={max_id}'; + const ACCOUNT_JSON_INFO = self:: BASE_URL . '/{username}/?__a=1'; + const MEDIA_JSON_INFO = self:: BASE_URL . '/p/{code}/?__a=1'; + const MEDIA_JSON_BY_LOCATION_ID = self:: BASE_URL . '/explore/locations/{{facebookLocationId}}/?__a=1&max_id={{maxId}}'; + const MEDIA_JSON_BY_TAG = self:: BASE_URL . '/explore/tags/{tag}/?__a=1&max_id={max_id}'; + const GENERAL_SEARCH = self:: BASE_URL . '/web/search/topsearch/?query={query}'; const ACCOUNT_JSON_INFO_BY_ID = 'ig_user({userId}){id,username,external_url,full_name,profile_pic_url,biography,followed_by{count},follows{count},media{count},is_private,is_verified}'; - const COMMENTS_BEFORE_COMMENT_ID_BY_CODE = 'https://www.instagram.com/graphql/query/?query_id=17852405266163336&shortcode={{shortcode}}&first={{count}}&after={{commentId}}'; + const COMMENTS_BEFORE_COMMENT_ID_BY_CODE = self:: BASE_URL . '/graphql/query/?query_id=17852405266163336&shortcode={{shortcode}}&first={{count}}&after={{commentId}}'; const LAST_LIKES_BY_CODE = 'ig_shortcode({{code}}){likes{nodes{id,user{id,profile_pic_url,username,follows{count},followed_by{count},biography,full_name,media{count},is_private,external_url,is_verified}},page_info}}'; - const FOLLOWING_URL = 'https://www.instagram.com/graphql/query/?query_id=17874545323001329&id={{accountId}}&first={{count}}'; - const FOLLOWERS_URL = 'https://www.instagram.com/graphql/query/?query_id=17851374694183129&id={{accountId}}&first={{count}}&after={{after}}'; - const FOLLOW_URL = 'https://www.instagram.com/web/friendships/{{accountId}}/follow/'; - const UNFOLLOW_URL = 'https://www.instagram.com/web/friendships/{{accountId}}/unfollow/'; - const USER_FEED = 'https://www.instagram.com/graphql/query/?query_id=17861995474116400&fetch_media_item_count=12&fetch_media_item_cursor=&fetch_comment_count=4&fetch_like=10'; - const USER_FEED2 = 'https://www.instagram.com/?__a=1'; - const INSTAGRAM_QUERY_URL = 'https://www.instagram.com/query/'; + const FOLLOWING_URL = self:: BASE_URL . '/graphql/query/?query_id=17874545323001329&id={{accountId}}&first={{count}}'; + const FOLLOWERS_URL = self:: BASE_URL . '/graphql/query/?query_id=17851374694183129&id={{accountId}}&first={{count}}&after={{after}}'; + const FOLLOW_URL = self:: BASE_URL . '/web/friendships/{{accountId}}/follow/'; + const UNFOLLOW_URL = self:: BASE_URL . '/web/friendships/{{accountId}}/unfollow/'; + const USER_FEED = self:: BASE_URL . '/graphql/query/?query_id=17861995474116400&fetch_media_item_count=12&fetch_media_item_cursor=&fetch_comment_count=4&fetch_like=10'; + const USER_FEED2 = self:: BASE_URL . '/?__a=1'; + const INSTAGRAM_QUERY_URL = self:: BASE_URL . '/query/'; const INSTAGRAM_CDN_URL = 'https://scontent.cdninstagram.com/'; - const ACCOUNT_MEDIAS2 = 'https://www.instagram.com/graphql/query/?query_id=17880160963012870&id={{accountId}}&first=10&after='; + const ACCOUNT_MEDIAS2 = self:: BASE_URL . '/graphql/query/?query_id=17880160963012870&id={{accountId}}&first=10&after='; // Look alike?? - const URL_SIMILAR = 'https://www.instagram.com/graphql/query/?query_id=17845312237175864&id=4663052'; + const URL_SIMILAR = self:: BASE_URL . '/graphql/query/?query_id=17845312237175864&id=4663052'; - const GRAPH_QL_QUERY_URL = 'https://www.instagram.com/graphql/query/?query_id={{queryId}}'; + const GRAPH_QL_QUERY_URL = self:: BASE_URL . '/graphql/query/?query_id={{queryId}}'; public static function getAccountPageLink($username) From 1b9c62ec607d87b84c2e06baae6b5c0b24ab4f01 Mon Sep 17 00:00:00 2001 From: "Georges.L" Date: Sat, 4 Nov 2017 14:46:11 +0100 Subject: [PATCH 2/3] Simplified endpoints urls (reverted from commit 534a655612c176d9ce564d6d52388f1315050d80) --- src/InstagramScraper/Endpoints.php | 40 +++++++++++++++--------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/InstagramScraper/Endpoints.php b/src/InstagramScraper/Endpoints.php index e5638f58..3b9c2d44 100644 --- a/src/InstagramScraper/Endpoints.php +++ b/src/InstagramScraper/Endpoints.php @@ -5,33 +5,33 @@ class Endpoints { const BASE_URL = 'https://www.instagram.com'; - const LOGIN_URL = self:: BASE_URL . '/accounts/login/ajax/'; - const ACCOUNT_PAGE = self:: BASE_URL . '/{username}'; - const MEDIA_LINK = self:: BASE_URL . '/p/{code}'; - const ACCOUNT_MEDIAS = self:: BASE_URL . '/{username}/media/?max_id={max_id}'; - const ACCOUNT_JSON_INFO = self:: BASE_URL . '/{username}/?__a=1'; - const MEDIA_JSON_INFO = self:: BASE_URL . '/p/{code}/?__a=1'; - const MEDIA_JSON_BY_LOCATION_ID = self:: BASE_URL . '/explore/locations/{{facebookLocationId}}/?__a=1&max_id={{maxId}}'; - const MEDIA_JSON_BY_TAG = self:: BASE_URL . '/explore/tags/{tag}/?__a=1&max_id={max_id}'; - const GENERAL_SEARCH = self:: BASE_URL . '/web/search/topsearch/?query={query}'; + const LOGIN_URL = 'https://www.instagram.com/accounts/login/ajax/'; + const ACCOUNT_PAGE = 'https://www.instagram.com/{username}'; + const MEDIA_LINK = 'https://www.instagram.com/p/{code}'; + const ACCOUNT_MEDIAS = 'https://www.instagram.com/{username}/media/?max_id={max_id}'; + const ACCOUNT_JSON_INFO = 'https://www.instagram.com/{username}/?__a=1'; + const MEDIA_JSON_INFO = 'https://www.instagram.com/p/{code}/?__a=1'; + const MEDIA_JSON_BY_LOCATION_ID = 'https://www.instagram.com/explore/locations/{{facebookLocationId}}/?__a=1&max_id={{maxId}}'; + const MEDIA_JSON_BY_TAG = 'https://www.instagram.com/explore/tags/{tag}/?__a=1&max_id={max_id}'; + const GENERAL_SEARCH = 'https://www.instagram.com/web/search/topsearch/?query={query}'; const ACCOUNT_JSON_INFO_BY_ID = 'ig_user({userId}){id,username,external_url,full_name,profile_pic_url,biography,followed_by{count},follows{count},media{count},is_private,is_verified}'; - const COMMENTS_BEFORE_COMMENT_ID_BY_CODE = self:: BASE_URL . '/graphql/query/?query_id=17852405266163336&shortcode={{shortcode}}&first={{count}}&after={{commentId}}'; + const COMMENTS_BEFORE_COMMENT_ID_BY_CODE = 'https://www.instagram.com/graphql/query/?query_id=17852405266163336&shortcode={{shortcode}}&first={{count}}&after={{commentId}}'; const LAST_LIKES_BY_CODE = 'ig_shortcode({{code}}){likes{nodes{id,user{id,profile_pic_url,username,follows{count},followed_by{count},biography,full_name,media{count},is_private,external_url,is_verified}},page_info}}'; - const FOLLOWING_URL = self:: BASE_URL . '/graphql/query/?query_id=17874545323001329&id={{accountId}}&first={{count}}'; - const FOLLOWERS_URL = self:: BASE_URL . '/graphql/query/?query_id=17851374694183129&id={{accountId}}&first={{count}}&after={{after}}'; - const FOLLOW_URL = self:: BASE_URL . '/web/friendships/{{accountId}}/follow/'; - const UNFOLLOW_URL = self:: BASE_URL . '/web/friendships/{{accountId}}/unfollow/'; - const USER_FEED = self:: BASE_URL . '/graphql/query/?query_id=17861995474116400&fetch_media_item_count=12&fetch_media_item_cursor=&fetch_comment_count=4&fetch_like=10'; - const USER_FEED2 = self:: BASE_URL . '/?__a=1'; - const INSTAGRAM_QUERY_URL = self:: BASE_URL . '/query/'; + const FOLLOWING_URL = 'https://www.instagram.com/graphql/query/?query_id=17874545323001329&id={{accountId}}&first={{count}}'; + const FOLLOWERS_URL = 'https://www.instagram.com/graphql/query/?query_id=17851374694183129&id={{accountId}}&first={{count}}&after={{after}}'; + const FOLLOW_URL = 'https://www.instagram.com/web/friendships/{{accountId}}/follow/'; + const UNFOLLOW_URL = 'https://www.instagram.com/web/friendships/{{accountId}}/unfollow/'; + const USER_FEED = 'https://www.instagram.com/graphql/query/?query_id=17861995474116400&fetch_media_item_count=12&fetch_media_item_cursor=&fetch_comment_count=4&fetch_like=10'; + const USER_FEED2 = 'https://www.instagram.com/?__a=1'; + const INSTAGRAM_QUERY_URL = 'https://www.instagram.com/query/'; const INSTAGRAM_CDN_URL = 'https://scontent.cdninstagram.com/'; - const ACCOUNT_MEDIAS2 = self:: BASE_URL . '/graphql/query/?query_id=17880160963012870&id={{accountId}}&first=10&after='; + const ACCOUNT_MEDIAS2 = 'https://www.instagram.com/graphql/query/?query_id=17880160963012870&id={{accountId}}&first=10&after='; // Look alike?? - const URL_SIMILAR = self:: BASE_URL . '/graphql/query/?query_id=17845312237175864&id=4663052'; + const URL_SIMILAR = 'https://www.instagram.com/graphql/query/?query_id=17845312237175864&id=4663052'; - const GRAPH_QL_QUERY_URL = self:: BASE_URL . '/graphql/query/?query_id={{queryId}}'; + const GRAPH_QL_QUERY_URL = 'https://www.instagram.com/graphql/query/?query_id={{queryId}}'; public static function getAccountPageLink($username) From 768999bdd225df6ddef559aa41252f90a7d9e33c Mon Sep 17 00:00:00 2001 From: "Georges.L" Date: Sat, 4 Nov 2017 15:00:58 +0100 Subject: [PATCH 3/3] Multiples fixes: - Replaced self class call by static (allows better extending) - Enforced strict comparisons on string - Safely replaced some double quotes by simple quotes --- src/InstagramScraper/Endpoints.php | 28 ++++---- src/InstagramScraper/Instagram.php | 111 +++++++++++++++-------------- 2 files changed, 70 insertions(+), 69 deletions(-) diff --git a/src/InstagramScraper/Endpoints.php b/src/InstagramScraper/Endpoints.php index 3b9c2d44..6442c77f 100644 --- a/src/InstagramScraper/Endpoints.php +++ b/src/InstagramScraper/Endpoints.php @@ -36,68 +36,68 @@ class Endpoints public static function getAccountPageLink($username) { - return str_replace('{username}', urlencode($username), Endpoints::ACCOUNT_PAGE); + return str_replace('{username}', urlencode($username), static::ACCOUNT_PAGE); } public static function getAccountJsonLink($username) { - return str_replace('{username}', urlencode($username), Endpoints::ACCOUNT_JSON_INFO); + return str_replace('{username}', urlencode($username), static::ACCOUNT_JSON_INFO); } public static function getAccountJsonInfoLinkByAccountId($id) { - return str_replace('{userId}', urlencode($id), Endpoints::ACCOUNT_JSON_INFO_BY_ID); + return str_replace('{userId}', urlencode($id), static::ACCOUNT_JSON_INFO_BY_ID); } public static function getAccountMediasJsonLink($username, $maxId = '') { - $url = str_replace('{username}', urlencode($username), Endpoints::ACCOUNT_MEDIAS); + $url = str_replace('{username}', urlencode($username), static::ACCOUNT_MEDIAS); return str_replace('{max_id}', urlencode($maxId), $url); } public static function getMediaPageLink($code) { - return str_replace('{code}', urlencode($code), Endpoints::MEDIA_LINK); + return str_replace('{code}', urlencode($code), static::MEDIA_LINK); } public static function getMediaJsonLink($code) { - return str_replace('{code}', urlencode($code), Endpoints::MEDIA_JSON_INFO); + return str_replace('{code}', urlencode($code), static::MEDIA_JSON_INFO); } public static function getMediasJsonByLocationIdLink($facebookLocationId, $maxId = '') { - $url = str_replace('{{facebookLocationId}}', urlencode($facebookLocationId), Endpoints::MEDIA_JSON_BY_LOCATION_ID); + $url = str_replace('{{facebookLocationId}}', urlencode($facebookLocationId), static::MEDIA_JSON_BY_LOCATION_ID); return str_replace('{{maxId}}', urlencode($maxId), $url); } public static function getMediasJsonByTagLink($tag, $maxId = '') { - $url = str_replace('{tag}', urlencode($tag), Endpoints::MEDIA_JSON_BY_TAG); + $url = str_replace('{tag}', urlencode($tag), static::MEDIA_JSON_BY_TAG); return str_replace('{max_id}', urlencode($maxId), $url); } public static function getGeneralSearchJsonLink($query) { - return str_replace('{query}', urlencode($query), Endpoints::GENERAL_SEARCH); + return str_replace('{query}', urlencode($query), static::GENERAL_SEARCH); } public static function getCommentsBeforeCommentIdByCode($code, $count, $commentId) { - $url = str_replace('{{shortcode}}', urlencode($code), Endpoints::COMMENTS_BEFORE_COMMENT_ID_BY_CODE); + $url = str_replace('{{shortcode}}', urlencode($code), static::COMMENTS_BEFORE_COMMENT_ID_BY_CODE); $url = str_replace('{{count}}', urlencode($count), $url); return str_replace('{{commentId}}', urlencode($commentId), $url); } public static function getLastLikesByCodeLink($code) { - $url = str_replace('{{code}}', urlencode($code), Endpoints::LAST_LIKES_BY_CODE); + $url = str_replace('{{code}}', urlencode($code), static::LAST_LIKES_BY_CODE); return $url; } public static function getGraphQlUrl($queryId, $parameters) { - $url = str_replace('{{queryId}}', urlencode($queryId), Endpoints::GRAPH_QL_QUERY_URL); + $url = str_replace('{{queryId}}', urlencode($queryId), static::GRAPH_QL_QUERY_URL); foreach ($parameters as $key => $value) { $url .= "&$key=$value"; } @@ -106,13 +106,13 @@ public static function getGraphQlUrl($queryId, $parameters) public static function getFollowUrl($accountId) { - $url = str_replace('{{accountId}}', urlencode($accountId), Endpoints::FOLLOW_URL); + $url = str_replace('{{accountId}}', urlencode($accountId), static::FOLLOW_URL); return $url; } public static function getFollowersJsonLink($accountId, $count, $after = '') { - $url = str_replace('{{accountId}}', urlencode($accountId), Endpoints::FOLLOWERS_URL); + $url = str_replace('{{accountId}}', urlencode($accountId), static::FOLLOWERS_URL); $url = str_replace('{{count}}', urlencode($count), $url); if ($after === '') { diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index 79180f10..d4dbe960 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -40,9 +40,9 @@ public static function withCredentials($username, $password, $sessionFolder = nu CacheManager::setDefaultConfig([ 'path' => $sessionFolder, ]); - self::$instanceCache = CacheManager::getInstance('files'); + static::$instanceCache = CacheManager::getInstance('files'); } else { - self::$instanceCache = $sessionFolder; + static::$instanceCache = $sessionFolder; } $instance = new self(); $instance->sessionUsername = $username; @@ -63,17 +63,17 @@ public static function searchTagsByTagName($tag) $response = Request::get(Endpoints::getGeneralSearchJsonLink($tag)); // use a raw constant in the code is not a good idea!! //if ($response->code === 404) { - if (self::HTTP_NOT_FOUND === $response->code) { + if (static::HTTP_NOT_FOUND === $response->code) { throw new InstagramNotFoundException('Account with given username does not exist.'); } // use a raw constant in the code is not a good idea!! //if ($response->code !== 200) { - if (self::HTTP_OK !== $response->code) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + if (static::HTTP_OK !== $response->code) { + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } $jsonResponse = json_decode($response->raw_body, true); - if (!isset($jsonResponse['status']) || $jsonResponse['status'] != 'ok') { + if (!isset($jsonResponse['status']) || $jsonResponse['status'] !== 'ok') { throw new InstagramException('Response code is not equal 200. Something went wrong. Please report issue.'); } @@ -98,13 +98,13 @@ private static function getErrorBody($rawError) return $rawError; } if (is_object($rawError)) { - $str = ""; + $str = ''; foreach ($rawError as $key => $value) { - $str .= " " . $key . " => " . $value . ";"; + $str .= ' ' . $key . ' => ' . $value . ';'; } return $str; } else { - return "Unknown body format"; + return 'Unknown body format'; } } @@ -119,15 +119,15 @@ private static function getErrorBody($rawError) public function searchAccountsByUsername($username) { $response = Request::get(Endpoints::getGeneralSearchJsonLink($username), $this->generateHeaders($this->userSession)); - if (self::HTTP_NOT_FOUND === $response->code) { + if (static::HTTP_NOT_FOUND === $response->code) { throw new InstagramNotFoundException('Account with given username does not exist.'); } - if (self::HTTP_OK !== $response->code) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + if (static::HTTP_OK !== $response->code) { + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } $jsonResponse = json_decode($response->raw_body, true); - if (!isset($jsonResponse['status']) || $jsonResponse['status'] != 'ok') { + if (!isset($jsonResponse['status']) || $jsonResponse['status'] !== 'ok') { throw new InstagramException('Response code is not equal 200. Something went wrong. Please report issue.'); } if (!isset($jsonResponse['users']) || empty($jsonResponse['users'])) { @@ -178,13 +178,13 @@ public function getMedias($username, $count = 20, $maxId = '') $isMoreAvailable = true; while ($index < $count && $isMoreAvailable) { $response = Request::get(Endpoints::getAccountMediasJsonLink($username, $maxId), $this->generateHeaders($this->userSession)); - if (self::HTTP_OK !== $response->code) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + if (static::HTTP_OK !== $response->code) { + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } $arr = json_decode($response->raw_body, true); if (!is_array($arr)) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } // fix - count takes longer/has more overhead if (empty($arr['items']) || !isset($arr['items'])) { @@ -232,13 +232,13 @@ public function getMediaByUrl($mediaUrl) $response = Request::get(rtrim($mediaUrl, '/') . '/?__a=1', $this->generateHeaders($this->userSession)); // use a raw constant in the code is not a good idea!! //if ($response->code === 404) { - if (self::HTTP_NOT_FOUND === $response->code) { + if (static::HTTP_NOT_FOUND === $response->code) { throw new InstagramNotFoundException('Media with given code does not exist or account is private.'); } // use a raw constant in the code is not a good idea!! //if ($response->code !== 200) { - if (self::HTTP_OK !== $response->code) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + if (static::HTTP_OK !== $response->code) { + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } $mediaArray = json_decode($response->raw_body, true); if (!isset($mediaArray['graphql']['shortcode_media'])) { @@ -285,14 +285,14 @@ public function getPaginateMedias($username, $maxId = '') // use a raw constant in the code is not a good idea!! //if ($response->code !== 200) { - if (self::HTTP_OK !== $response->code) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + if (static::HTTP_OK !== $response->code) { + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } $arr = json_decode($response->raw_body, true); if (!is_array($arr)) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } //if (count($arr['items']) === 0) { @@ -328,7 +328,7 @@ public function getPaginateMedias($username, $maxId = '') public function getMediaCommentsById($mediaId, $count = 10, $maxId = null) { $code = Media::getCodeFromId($mediaId); - return self::getMediaCommentsByCode($code, $count, $maxId); + return static::getMediaCommentsByCode($code, $count, $maxId); } /** @@ -346,10 +346,10 @@ public function getMediaCommentsByCode($code, $count = 10, $maxId = null) $index = 0; $hasPrevious = true; while ($hasPrevious && $index < $count) { - if ($remain > self::MAX_COMMENTS_PER_REQUEST) { - $numberOfCommentsToRetreive = self::MAX_COMMENTS_PER_REQUEST; - $remain -= self::MAX_COMMENTS_PER_REQUEST; - $index += self::MAX_COMMENTS_PER_REQUEST; + if ($remain > static::MAX_COMMENTS_PER_REQUEST) { + $numberOfCommentsToRetreive = static::MAX_COMMENTS_PER_REQUEST; + $remain -= static::MAX_COMMENTS_PER_REQUEST; + $index += static::MAX_COMMENTS_PER_REQUEST; } else { $numberOfCommentsToRetreive = $remain; $index += $remain; @@ -363,10 +363,10 @@ public function getMediaCommentsByCode($code, $count = 10, $maxId = null) $response = Request::get($commentsUrl, $this->generateHeaders($this->userSession)); // use a raw constant in the code is not a good idea!! //if ($response->code !== 200) { - if (self::HTTP_OK !== $response->code) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + if (static::HTTP_OK !== $response->code) { + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; $jsonResponse = json_decode($response->raw_body, true); $nodes = $jsonResponse['data']['shortcode_media']['edge_media_to_comment']['edges']; @@ -413,6 +413,7 @@ private static function parseCookies($rawCookies) * * @return Account * @throws InstagramException + * @throws \InvalidArgumentException */ public function getAccountById($id) { @@ -435,10 +436,10 @@ public function getAccountById($id) } if ($response->code !== 302) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->raw_body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->raw_body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; // Get the username from the response url. @@ -459,11 +460,11 @@ public function getAccountById($id) public function getAccount($username) { $response = Request::get(Endpoints::getAccountJsonLink($username), $this->generateHeaders($this->userSession)); - if (self::HTTP_NOT_FOUND === $response->code) { + if (static::HTTP_NOT_FOUND === $response->code) { throw new InstagramNotFoundException('Account with given username does not exist.'); } - if (self::HTTP_OK !== $response->code) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + if (static::HTTP_OK !== $response->code) { + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } $userArray = json_decode($response->raw_body, true); @@ -491,9 +492,9 @@ public function getMediasByTag($tag, $count = 12, $maxId = '', $minTimestamp = n $response = Request::get(Endpoints::getMediasJsonByTagLink($tag, $maxId), $this->generateHeaders($this->userSession)); if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; $arr = json_decode($response->raw_body, true); if (!is_array($arr)) { @@ -549,10 +550,10 @@ public function getPaginateMediasByTag($tag, $maxId = '') $this->generateHeaders($this->userSession)); if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; $arr = json_decode($response->raw_body, true); @@ -604,9 +605,9 @@ public function getTopMediasByTagName($tagName) throw new InstagramNotFoundException('Account with given username does not exist.'); } if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; $jsonResponse = json_decode($response->raw_body, true); $medias = []; @@ -631,9 +632,9 @@ public function getLocationTopMediasById($facebookLocationId) throw new InstagramNotFoundException('Location with this id doesn\'t exist'); } if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; $jsonResponse = json_decode($response->raw_body, true); $nodes = $jsonResponse['location']['top_posts']['nodes']; @@ -661,9 +662,9 @@ public function getLocationMediasById($facebookLocationId, $quantity = 12, $offs $response = Request::get(Endpoints::getMediasJsonByLocationIdLink($facebookLocationId, $offset), $this->generateHeaders($this->userSession)); if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; $arr = json_decode($response->raw_body, true); $nodes = $arr['location']['media']['nodes']; @@ -698,9 +699,9 @@ public function getLocationById($facebookLocationId) throw new InstagramNotFoundException('Location with this id doesn\'t exist'); } if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $this->userSession['csrftoken'] = $cookies['csrftoken']; $jsonResponse = json_decode($response->raw_body, true); return Location::create($jsonResponse['location']); @@ -733,7 +734,7 @@ public function getFollowers($accountId, $count = 20, $pageSize = 20, $delayed = $response = Request::get(Endpoints::getFollowersJsonLink($accountId, $pageSize, $endCursor), $this->generateHeaders($this->userSession)); if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } $jsonResponse = json_decode($response->raw_body, true); @@ -785,14 +786,14 @@ public function login($force = false) throw new InstagramAuthException("User credentials not provided"); } - $cachedString = self::$instanceCache->getItem($this->sessionUsername); + $cachedString = static::$instanceCache->getItem($this->sessionUsername); $session = $cachedString->get(); if ($force || !$this->isLoggedIn($session)) { $response = Request::get(Endpoints::BASE_URL); if ($response->code !== 200) { - throw new InstagramException('Response code is ' . $response->code . '. Body: ' . Instagram::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); + throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $mid = $cookies['mid']; $csrfToken = $cookies['csrftoken']; $headers = ['cookie' => "csrftoken=$csrfToken; mid=$mid;", @@ -816,10 +817,10 @@ public function login($force = false) } } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); $cookies['mid'] = $mid; $cachedString->set($cookies); - self::$instanceCache->save($cachedString); + static::$instanceCache->save($cachedString); $this->userSession = $cookies; } else { $this->userSession = $session; @@ -848,7 +849,7 @@ public function isLoggedIn($session) if ($response->code !== 200) { return false; } - $cookies = self::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers['Set-Cookie']); if (!isset($cookies['ds_user_id'])) { return false; } @@ -860,7 +861,7 @@ public function isLoggedIn($session) */ public function saveSession() { - $cachedString = self::$instanceCache->getItem($this->sessionUsername); + $cachedString = static::$instanceCache->getItem($this->sessionUsername); $cachedString->set($this->userSession); }