diff --git a/.gitignore b/.gitignore index 2548680e..038c8506 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ composer.lock .DS_Store phpunit.phar tests/sessions -phpunit.xml \ No newline at end of file +phpunit.xml +settings.json diff --git a/examples/getHighlights.php b/examples/getHighlights.php index d9b4ede9..fa7ad72e 100644 --- a/examples/getHighlights.php +++ b/examples/getHighlights.php @@ -4,7 +4,11 @@ require __DIR__ . '/../vendor/autoload.php'; -$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'user', 'passwd', new Psr16Adapter('Files')); +$settings=json_decode(file_get_contents('settings.json')); +$username = $settings->username; +$password = $settings->password; + +$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), $username, $password, new Psr16Adapter('Files')); $instagram->login(); //$userId = $instagram->getAccount('instagram')->getId(); diff --git a/examples/getStories.php b/examples/getStories.php index bcc8b392..af0fd1bf 100644 --- a/examples/getStories.php +++ b/examples/getStories.php @@ -3,7 +3,12 @@ require __DIR__ . '/../vendor/autoload.php'; -$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files')); + +$settings=json_decode(file_get_contents('settings.json')); +$username = $settings->username; +$password = $settings->password; + +$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), $username, $password, new Psr16Adapter('Files')); $instagram->login(); $stories = $instagram->getStories(); diff --git a/examples/getStoriesFromUserStories.php b/examples/getStoriesFromUserStories.php index c8f007db..307e8337 100644 --- a/examples/getStoriesFromUserStories.php +++ b/examples/getStoriesFromUserStories.php @@ -4,9 +4,13 @@ require __DIR__ . '/../vendor/autoload.php'; use Phpfastcache\Helper\Psr16Adapter; -$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'user', 'passed', new Psr16Adapter('Files')); +$settings=json_decode(file_get_contents('settings.json')); +$username = $settings->username; +$password = $settings->password; + +$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), $username, $password, new Psr16Adapter('Files')); $instagram->login(); -$instagram->saveSession(); +$instagram->saveSession(10*24*3600); // *************************** get storie from unsetStories ********************************** diff --git a/src/InstagramScraper/Instagram.php b/src/InstagramScraper/Instagram.php index 2223ab08..6b262225 100644 --- a/src/InstagramScraper/Instagram.php +++ b/src/InstagramScraper/Instagram.php @@ -139,6 +139,11 @@ private static function getErrorBody($rawError) if (is_object($rawError)) { $str = ''; foreach ($rawError as $key => $value) { + //to avoid erros in an error function make sure all are string before concat operation + if (is_array($value)) $value=json_encode((array)$value); + if (is_object($value)) $value=json_encode((array)$value); + if (is_array($key)) $key=json_encode((array)$key); + if (is_object($key)) $key=json_encode((array)$key); $str .= ' ' . $key . ' => ' . $value . ';'; } return $str; @@ -1683,16 +1688,23 @@ public function getPaginateAllFollowing($accountId, $pageSize = 20, $nextPage = 'accounts' => $accounts ]; } - /** * @param array $reel_ids - array of instagram user ids * @return array * @throws InstagramException */ - public function getStories($reel_ids = null) + public function getStories($reel_ids = null, $highlight_reel_ids=[], $location_ids=[]) { + if (empty($reel_ids) ) $reel_ids = []; + elseif (!is_array($reel_ids)) $reel_ids = [$reel_ids]; + if (!is_array($highlight_reel_ids)) $highlight_reel_ids = [$highlight_reel_ids]; + if (!is_array($location_ids)) $location_ids = [$location_ids]; + //------------------------------------------------------------------------------------------------------------- + // if no arguments: get user stories + //------------------------------------------------------------------------------------------------------------- $variables = ['precomposed_overlay' => false, 'reel_ids' => []]; - if (empty($reel_ids)) { + if ($reel_ids == [] && $highlight_reel_ids == [] && $location_ids==[]) { + $response = Request::get(Endpoints::getUserStoriesLink($variables), $this->generateHeaders($this->userSession)); @@ -1707,36 +1719,44 @@ public function getStories($reel_ids = null) } foreach ($jsonResponse['data']['user']['feed_reels_tray']['edge_reels_tray_to_reel']['edges'] as $edge) { - $variables['reel_ids'][] = $edge['node']['id']; - } - } else { - $variables['reel_ids'] = $reel_ids; - } - - $response = Request::get(Endpoints::getStoriesLink($variables), - $this->generateHeaders($this->userSession)); - - 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); - } - - $jsonResponse = $this->decodeRawBodyToJson($response->raw_body); - - if (empty($jsonResponse['data']['reels_media'])) { - return []; - } + $reel_ids[] = $edge['node']['id']; + } + } + //------------------------------------------------------------------------------------------------------------- + // Get stories from reel_ids (users), highlight_ids and/or location_ids + //------------------------------------------------------------------------------------------------------------- + $variables = [ + 'highlight_reel_ids'=> $highlight_reel_ids, + 'reel_ids'=> $reel_ids, + 'location_ids'=> $location_ids, + 'precomposed_overlay'=> False, + ]; + + $url = Endpoints::HIGHLIGHT_STORIES . '&variables=' . json_encode($variables); + $response = Request::get($url, $this->generateHeaders($this->userSession)); + + 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); + } + + $jsonResponse = $this->decodeRawBodyToJson($response->raw_body); + + if (empty($jsonResponse['data']['reels_media'])) { + return []; + } + + $stories = []; + foreach ($jsonResponse['data']['reels_media'] as $highlight) { + $UserStories = UserStories::create(); + $UserStories->setOwner(Account::create($highlight['owner'])); + foreach ($highlight['items'] as $item) { + $UserStories->addStory(Story::create($item)); + } + $stories[] = $UserStories; + } + return $stories; + } - $stories = []; - foreach ($jsonResponse['data']['reels_media'] as $user) { - $UserStories = UserStories::create(); - $UserStories->setOwner(Account::create($user['user'])); - foreach ($user['items'] as $item) { - $UserStories->addStory(Story::create($item)); - } - $stories[] = $UserStories; - } - return $stories; - } /** * @param bool $force @@ -2170,40 +2190,7 @@ public function getHighlights($userId) */ public function getHighlightStories($highlight_reel_ids) { - if (!is_array($highlight_reel_ids)) { - $highlight_reel_ids = [$highlight_reel_ids]; - } - - $variables = [ - 'highlight_reel_ids'=> $highlight_reel_ids, - 'reel_ids'=> [], - 'location_ids'=> [], - 'precomposed_overlay'=> False, - ]; - - $url = Endpoints::HIGHLIGHT_STORIES . '&variables=' . json_encode($variables); - $response = Request::get($url, $this->generateHeaders($this->userSession)); - - 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); - } - - $jsonResponse = $this->decodeRawBodyToJson($response->raw_body); - - if (empty($jsonResponse['data']['reels_media'])) { - return []; - } - - $stories = []; - foreach ($jsonResponse['data']['reels_media'] as $highlight) { - $UserStories = UserStories::create(); - $UserStories->setOwner(Account::create($highlight['owner'])); - foreach ($highlight['items'] as $item) { - $UserStories->addStory(Story::create($item)); - } - $stories[] = $UserStories; - } - return $stories; + return $this->getStories([], $highlight_reel_ids, []); } /**