diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index 847e829a..c5122ea4 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -657,8 +657,7 @@ public function getMediaCommentsByCode($code, $count = 10, $maxId = null) 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.', $response->code); } - $cookies = static::parseCookies($response->headers['Set-Cookie']); - $this->userSession['csrftoken'] = $cookies['csrftoken']; + $cookies = static::parseCookies($response->headers); $jsonResponse = $this->decodeRawBodyToJson($response->raw_body); $nodes = $jsonResponse['data']['shortcode_media']['edge_media_to_comment']['edges']; foreach ($nodes as $commentArray) { @@ -682,12 +681,14 @@ public function getMediaCommentsByCode($code, $count = 10, $maxId = null) /** * We work only on https in this case if we have same cookies on Secure and not - we will choice Secure cookie * - * @param string $rawCookies + * @param array $headers * * @return array */ - private static function parseCookies($rawCookies) + private static function parseCookies($headers) { + $rawCookies = isset($headers['Set-Cookie']) ? $headers['Set-Cookie'] : isset($headers['set-cookie']) ? $headers['set-cookie'] : []; + if (!is_array($rawCookies)) { $rawCookies = [$rawCookies]; } @@ -712,6 +713,11 @@ private static function parseCookies($rawCookies) } $cookies = $secure_cookies + $not_secure_cookies; + + if (isset($cookies['csrftoken'])) { + $this->userSession['csrftoken'] = $cookies['csrftoken']; + } + return $cookies; } @@ -749,7 +755,6 @@ public function getMediaLikesByCode($code, $count = 10, $maxId = null) throw new InstagramException('Response code is ' . $response->code . '. Body: ' . $response->body . ' Something went wrong. Please report issue.', $response->code); } $cookies = self::parseCookies($response->headers['Set-Cookie']); - $this->userSession['csrftoken'] = $cookies['csrftoken']; $jsonResponse = $this->decodeRawBodyToJson($response->raw_body); @@ -838,8 +843,7 @@ public function getMediasByTag($tag, $count = 12, $maxId = '', $minTimestamp = n throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code); } - $cookies = static::parseCookies($response->headers['Set-Cookie']); - $this->userSession['csrftoken'] = $cookies['csrftoken']; + $cookies = static::parseCookies($response->headers); $arr = $this->decodeRawBodyToJson($response->raw_body); @@ -900,8 +904,7 @@ public function getPaginateMediasByTag($tag, $maxId = '') throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code); } - $cookies = static::parseCookies($response->headers['Set-Cookie']); - $this->userSession['csrftoken'] = $cookies['csrftoken']; + $cookies = static::parseCookies($response->headers); $arr = $this->decodeRawBodyToJson($response->raw_body); @@ -956,8 +959,7 @@ public function getCurrentTopMediasByTagName($tagName) throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.'); } - $cookies = static::parseCookies($response->headers['Set-Cookie']); - $this->userSession['csrftoken'] = $cookies['csrftoken']; + $cookies = static::parseCookies($response->headers); $jsonResponse = $this->decodeRawBodyToJson($response->raw_body); $medias = []; $nodes = (array)@$jsonResponse['graphql']['hashtag']['edge_hashtag_to_top_posts']['edges']; @@ -984,8 +986,7 @@ public function getCurrentTopMediasByLocationId($facebookLocationId) if ($response->code !== static::HTTP_OK) { throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code); } - $cookies = static::parseCookies($response->headers['Set-Cookie']); - $this->userSession['csrftoken'] = $cookies['csrftoken']; + $cookies = static::parseCookies($response->headers); $jsonResponse = $this->decodeRawBodyToJson($response->raw_body); $nodes = $jsonResponse['location']['top_posts']['nodes']; $medias = []; @@ -1014,8 +1015,7 @@ public function getMediasByLocationId($facebookLocationId, $quantity = 24, $offs if ($response->code !== static::HTTP_OK) { throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code); } - $cookies = static::parseCookies($response->headers['Set-Cookie']); - $this->userSession['csrftoken'] = $cookies['csrftoken']; + $cookies = static::parseCookies($response->headers); $arr = $this->decodeRawBodyToJson($response->raw_body); $nodes = $arr['graphql']['location']['edge_location_to_media']['edges']; foreach ($nodes as $mediaArray) { @@ -1053,8 +1053,7 @@ public function getLocationById($facebookLocationId) throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code); } - $cookies = static::parseCookies($response->headers['Set-Cookie']); - $this->userSession['csrftoken'] = $cookies['csrftoken']; + $cookies = static::parseCookies($response->headers); $jsonResponse = $this->decodeRawBodyToJson($response->raw_body); return Location::create($jsonResponse['graphql']['location']); } @@ -1272,11 +1271,8 @@ public function login($force = false, $support_two_step_verification = false) if (isset($match[1])) { $csrfToken = $match[1]; } - if (isset($response->headers['Set-Cookie'])): - $cookies = static::parseCookies($response->headers['Set-Cookie']); - else: - $cookies = static::parseCookies($response->headers['set-cookie']); - endif; + $cookies = static::parseCookies($response->headers); + $mid = $cookies['mid']; $headers = [ 'cookie' => "ig_cb=1; csrftoken=$csrfToken; mid=$mid;", @@ -1304,11 +1300,8 @@ public function login($force = false, $support_two_step_verification = false) } } - if (isset($response->headers['Set-Cookie'])): - $cookies = static::parseCookies($response->headers['Set-Cookie']); - else: - $cookies = static::parseCookies($response->headers['set-cookie']); - endif; + $cookies = static::parseCookies($response->headers); + $cookies['mid'] = $mid; $cachedString->set($cookies); static::$instanceCache->save($cachedString); @@ -1343,7 +1336,7 @@ public function isLoggedIn($session) if ($response->code !== static::HTTP_OK) { return false; } - $cookies = static::parseCookies($response->headers['Set-Cookie']); + $cookies = static::parseCookies($response->headers); if (!isset($cookies['ds_user_id'])) { return false; } @@ -1358,7 +1351,7 @@ public function isLoggedIn($session) */ private function verifyTwoStep($response, $cookies) { - $new_cookies = static::parseCookies($response->headers['Set-Cookie']); + $new_cookies = static::parseCookies($response->headers); $cookies = array_merge($cookies, $new_cookies); $cookie_string = ''; foreach ($cookies as $name => $value) {