Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions examples/like-unlike-story.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

use Instagram\Api;
use Instagram\Exception\InstagramException;

use Psr\Cache\CacheException;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

require realpath(dirname(__FILE__)) . '/../vendor/autoload.php';
$credentials = include_once realpath(dirname(__FILE__)) . '/credentials.php';

$cachePool = new FilesystemAdapter('Instagram', 0, __DIR__ . '/../cache');

try {
$api = new Api($cachePool);
$api->login($credentials->getLogin(), $credentials->getPassword());

// we need instagram user id
$profile = $api->getProfile('starwars');
sleep(1);
$feedStories = $api->getStories($profile->getId());

if (count($feedStories->getStories())) {
/** @var \Instagram\Model\StoryMedia $story */
foreach ($feedStories->getStories() as $story) {

/** Like Story */
$likeStory = $api->likeStory($story->getId());
echo "Like story {$story->getId()} : {$likeStory} \n";

/** Unlike Story *
$unlikeStory = $api->unlikeStory($story->getId());
echo "Unlike story {$story->getId()} : {$unlikeStory} \n";
/**/

}
} else {
echo 'No stories' . "\n";
}

} catch (InstagramException $e) {
print_r($e->getMessage());
} catch (CacheException $e) {
print_r($e->getMessage());
}
41 changes: 32 additions & 9 deletions examples/login-with-cookies.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

/** 1. Get cookies from file */
$sessionId = $cachePool->getItem(Session::SESSION_KEY . '.' . CacheHelper::sanitizeUsername($credentials->getLogin()))
->get()
->getCookieByName('sessionId');
->get()
->getCookieByName('sessionId');

// Generate CookieJar from instagram cookie 'sessionid'
$cookieJar = new CookieJar(false, [$sessionId]);
Expand All @@ -39,25 +39,48 @@
"Discard" => false,
"HttpOnly" => true,
]);

// Generate CookieJar from instagram cookie 'sessionid'
$cookieJar = new CookieJar(false, [$sessionId]);
*/
*/

try {
$api = new Api();
$api = new Api($cachePool);

// Optionals for set user agent and language
$api->setUserAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.57 Safari/537.36');
$api->setLanguage('id-ID');

$api->loginWithCookies($cookieJar);


/** You can use below code to auto save session
$api->loginWithCookies($cookieJar, true, $credentials->getLogin());
*/

$profile = $api->getProfile('robertdowneyjr');

dd($profile);
} catch (InstagramAuthException $e) {
print_r($e->getMessage());
} catch (InstagramException $e) {
print_r($e->getMessage());
}


/** Note :
* If you want to save your own cookies manually
* you can use the method below
*/

/** login with method loginWithCookies */
// $newCookieJar = $api->loginWithCookies($cookieJar);

/** Save cookieJar into file */
// $unixStringForCookieIdentification = $credentials->getLogin(); // can be replaced with any string just for cookie identification
// $sessionData = $cachePool
// ->getItem(Session::SESSION_KEY . '.' . CacheHelper::sanitizeUsername($unixStringForCookieIdentification));
// ->set($newCookieJar);
// $cachePool->save($sessionData);

/** use of cookies in subsequent requests after login with cookies */
// $api->login($unixStringForCookieIdentification, '');
// dd($api->getProfile('robertdowneyjr'));
44 changes: 44 additions & 0 deletions examples/seen-story.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

use Instagram\Api;
use Instagram\Exception\InstagramException;

use Psr\Cache\CacheException;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;

require realpath(dirname(__FILE__)) . '/../vendor/autoload.php';
$credentials = include_once realpath(dirname(__FILE__)) . '/credentials.php';

$cachePool = new FilesystemAdapter('Instagram', 0, __DIR__ . '/../cache');

try {
$api = new Api($cachePool);
$api->login($credentials->getLogin(), $credentials->getPassword());

// we need instagram user id
$profile = $api->getProfile('starwars');
sleep(1);
$feedStories = $api->getStories($profile->getId());

if (count($feedStories->getStories())) {
/** @var \Instagram\Model\StoryMedia $story */
foreach ($feedStories->getStories() as $story) {
$storyId = $story->getId();
$ownerId = intval($feedStories->getOwner()->id);
$storyTakenAt = strtotime($story->getTakenAtDate()->format("Y-m-d h:i:sa"));
$storySeenAt = time();

$seenStory = $api->seenStory($storyId, $ownerId, $storyTakenAt, $storySeenAt);
echo "Seen story {$storyId} : {$seenStory} \n";
}
} else {
echo 'No stories' . "\n";
}

} catch (InstagramException $e) {
print_r($e->getMessage());
} catch (CacheException $e) {
print_r($e->getMessage());
}
74 changes: 68 additions & 6 deletions src/Instagram/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
use GuzzleHttp\Cookie\{SetCookie, CookieJar};
use Instagram\Auth\{Checkpoint\ImapClient, Login, Session};
use Instagram\Exception\{InstagramException, InstagramAuthException};
use Instagram\Hydrator\{LocationHydrator,
use Instagram\Hydrator\{
LocationHydrator,
MediaHydrator,
MediaCommentsHydrator,
ProfileAlternativeHydrator,
Expand All @@ -23,7 +24,8 @@
LiveHydrator,
TimelineFeedHydrator
};
use Instagram\Model\{Location,
use Instagram\Model\{
Location,
Media,
MediaDetailed,
MediaComments,
Expand All @@ -39,7 +41,8 @@
TaggedMediasFeed,
TimelineFeed
};
use Instagram\Transport\{CommentPost,
use Instagram\Transport\{
CommentPost,
JsonMediaDetailedDataFeed,
JsonMediasDataFeed,
JsonMediaCommentsFeed,
Expand All @@ -59,7 +62,8 @@
LocationData,
LiveData,
ReelsDataFeed,
TimelineDataFeed
TimelineDataFeed,
StoryInteraction
};
use Psr\Cache\CacheItemPoolInterface;
use Instagram\Utils\{InstagramHelper, OptionHelper};
Expand Down Expand Up @@ -117,9 +121,12 @@ public function setLanguage(string $language): void
/**
* @param \GuzzleHttp\Cookie\CookieJar $cookies
*
* return \GuzzleHttp\Cookie\CookieJar
*
* @throws Exception\InstagramAuthException
*/
public function loginWithCookies(CookieJar $cookies): void

public function loginWithCookies(CookieJar $cookies, bool $saveCookies = false, string $sessionKey = null): void
{
$login = new Login($this->client, '', '', null, $this->challengeDelay);

Expand All @@ -134,6 +141,17 @@ public function loginWithCookies(CookieJar $cookies): void
// Get New Cookies
$cookies = $login->withCookies($session->toArray());

if ($saveCookies) {
if (!($this->cachePool instanceof CacheItemPoolInterface))
throw new InstagramAuthException('You must set cachePool to save this session, example: \n$cachePool = new \Symfony\Component\Cache\Adapter\FilesystemAdapter("Instagram", 0, __DIR__ . "/../cache"); \n$api = new \Instagram\Api($cachePool);');
if (empty($sessionKey))
throw new InstagramAuthException('You must set sessionKey, Example like your instagram username. \nE.g: (new Instagram\Api())->loginWithCookies($cookies, true, $credentials->getLogin());');

$sessionData = $this->cachePool->getItem(Session::SESSION_KEY . '.' . CacheHelper::sanitizeUsername($sessionKey));
$sessionData->set($cookies);
$this->cachePool->save($sessionData);
}

$this->session = new Session($cookies);
}

Expand Down Expand Up @@ -167,7 +185,6 @@ public function login(string $username, string $password, ?ImapClient $imapClien
$this->logout($username);
$this->login($username, $password, $imapClient);
}

} else {
$cookies = $login->process();
$sessionData->set($cookies);
Expand Down Expand Up @@ -393,6 +410,51 @@ public function getStoriesOfHighlightsFolder(StoryHighlightsFolder $folder): Sto
return $hydrator->getFolder();
}

/**
* @param int $storyId
* @param int $ownerId
* @param int $takenAt
* @param int $seenAt
*
* @return string
*
* @throws Exception\InstagramAuthException
* @throws Exception\InstagramFetchException
*/
public function seenStory(int $storyId, int $ownerId, int $takenAt, int $seenAt): string
{
$storyInteraction = new StoryInteraction($this->client, $this->session);
return $storyInteraction->seen($storyId, $ownerId, $takenAt, $seenAt);
}

/**
* @param int $storyId
*
* @return string
*
* @throws Exception\InstagramAuthException
* @throws Exception\InstagramFetchException
*/
public function likeStory(int $storyId): string
{
$storyInteraction = new StoryInteraction($this->client, $this->session);
return $storyInteraction->like($storyId);
}

/**
* @param int $storyId
*
* @return string
*
* @throws Exception\InstagramAuthException
* @throws Exception\InstagramFetchException
*/
public function unlikeStory(int $storyId): string
{
$storyInteraction = new StoryInteraction($this->client, $this->session);
return $storyInteraction->unlike($storyId);
}

/**
* @param Media $media
*
Expand Down
8 changes: 8 additions & 0 deletions src/Instagram/Auth/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,14 @@ public function withCookies(array $session): CookieJar
}
}

// get all instagram cookies
$manifestRequest = $this->client->request('GET', InstagramHelper::URL_BASE . "data/manifest.json", [
'headers' => [
'user-agent' => OptionHelper::$USER_AGENT,
],
'cookies' => $cookies
]);

return $cookies;
}

Expand Down
Loading