Skip to content

Commit

Permalink
Merge pull request #434 from sakalauskas/patch-1
Browse files Browse the repository at this point in the history
Fix error "Undefined index: Set-Cookie and csrf token"
  • Loading branch information
raiym authored Dec 20, 2018
2 parents 1316eb3 + 9b89c14 commit 235477e
Showing 1 changed file with 22 additions and 29 deletions.
51 changes: 22 additions & 29 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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];
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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'];
Expand All @@ -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 = [];
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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']);
}
Expand Down Expand Up @@ -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;",
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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) {
Expand Down

0 comments on commit 235477e

Please sign in to comment.