Skip to content

Commit

Permalink
Merge pull request #3 from postaddictme/master
Browse files Browse the repository at this point in the history
Update with fork origin
  • Loading branch information
Mulkave authored Mar 16, 2018
2 parents 13fa13a + c527b5b commit bc5e31a
Show file tree
Hide file tree
Showing 18 changed files with 1,045 additions and 323 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ $instagram->login(); // will use cached session if you can force login $instagra
$account = $instagram->getAccountById(3);
echo $account->getUsername();
```
Using proxy for requests:

```php
$instagram = new Instagram();
Instagram::setProxy([
'address' => '111.112.113.114',
'port' => '8080',
'tunnel' => true,
'timeout' => 30,
]);
// Request with proxy
$account = $instagram->getAccount('kevin');
Instagram::disableProxy();
// Request without proxy
$account = $instagram->getAccount('kevin');
```

## Installation

Expand Down
4 changes: 2 additions & 2 deletions examples/getAccountById.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
echo "Profile picture url: {$account->getProfilePicUrl()}\n";
echo "External link: {$account->getExternalUrl()}\n";
echo "Number of published posts: {$account->getMediaCount()}\n";
echo "Number of followers: {$account->getFollowsCount()}\n";
echo "Number of follows: {$account->getFollowedByCount()}\n";
echo "Number of followers: {$account->getFollowedByCount()}\n";
echo "Number of follows: {$account->getFollowsCount()}\n";
echo "Is private: {$account->isPrivate()}\n";
echo "Is verified: {$account->isVerified()}\n";
13 changes: 13 additions & 0 deletions examples/getAccountFollowings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', 'path/to/cache/folder');
$instagram->login();
sleep(2); // Delay to mimic user

$username = 'kevin';
$followers = [];
$account = $instagram->getAccount($username);
sleep(1);
$followers = $instagram->getFollowing($account->getId(), 1000, 100, true); // Get 1000 followings of 'kevin', 100 a time with random delay between requests
echo '<pre>' . json_encode($followers, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . '</pre>';
39 changes: 39 additions & 0 deletions examples/getSidecarMediaByUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

function printMediaInfo(\InstagramScraper\Model\Media $media, $padding = '') {
echo "${padding}Id: {$media->getId()}\n";
echo "${padding}Shotrcode: {$media->getShortCode()}\n";
echo "${padding}Created at: {$media->getCreatedTime()}\n";
echo "${padding}Caption: {$media->getCaption()}\n";
echo "${padding}Number of comments: {$media->getCommentsCount()}\n";
echo "${padding}Number of likes: {$media->getLikesCount()}\n";
echo "${padding}Get link: {$media->getLink()}\n";
echo "${padding}High resolution image: {$media->getImageHighResolutionUrl()}\n";
echo "${padding}Media type (video/image/sidecar): {$media->getType()}\n";
}

// If account is public you can query Instagram without auth
$instagram = new \InstagramScraper\Instagram();

// If account is private and you subscribed to it firstly login
$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
$instagram->login();

$media = $instagram->getMediaByUrl('https://www.instagram.com/p/BQ0lhTeAYo5');
echo "Media info:\n";
printMediaInfo($media);

$padding = ' ';
echo "Sidecar medias info:\n";
foreach ($media->getSidecarMedias() as $sidecarMedia) {
printMediaInfo($sidecarMedia, $padding);
echo "\n";
}

$account = $media->getOwner();
echo "Account info:\n";
echo "Id: {$account->getId()}\n";
echo "Username: {$account->getUsername()}\n";
echo "Full name: {$account->getFullName()}\n";
echo "Profile pic url: {$account->getProfilePicUrl()}\n";
8 changes: 8 additions & 0 deletions examples/getStories.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

$instagram = \InstagramScraper\Instagram::withCredentials('username', 'password', '/path/to/cache/folder');
$instagram->login();

$stories = $instagram->getStories();
print_r($stories);
7 changes: 3 additions & 4 deletions src/InstagramScraper.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
<?php

require_once __DIR__ . '/InstagramScraper/Instagram.php';
require_once __DIR__ . '/InstagramScraper/Endpoints.php';
require_once __DIR__ . '/InstagramScraper/InstagramQueryId.php';
require_once __DIR__ . '/InstagramScraper/Traits/ArrayLikeTrait.php';
require_once __DIR__ . '/InstagramScraper/Traits/InitializerTrait.php';
require_once __DIR__ . '/InstagramScraper/Model/AbstractModel.php';
require_once __DIR__ . '/InstagramScraper/Model/Account.php';
require_once __DIR__ . '/InstagramScraper/Model/CarouselMedia.php';
require_once __DIR__ . '/InstagramScraper/Model/Comment.php';
require_once __DIR__ . '/InstagramScraper/Model/Location.php';
require_once __DIR__ . '/InstagramScraper/Model/Media.php';
require_once __DIR__ . '/InstagramScraper/Model/Tag.php';
require_once __DIR__ . '/InstagramScraper/Model/AbstractModel.php';
require_once __DIR__ . '/InstagramScraper/Exception/InstagramException.php';
require_once __DIR__ . '/InstagramScraper/Exception/InstagramAuthException.php';
require_once __DIR__ . '/InstagramScraper/Exception/InstagramNotFoundException.php';
require_once __DIR__ . '/InstagramScraper/Traits/ArrayLikeTrait.php';
require_once __DIR__ . '/InstagramScraper/Traits/InitializerTrait.php';
87 changes: 61 additions & 26 deletions src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Endpoints
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_MEDIAS = 'https://instagram.com/graphql/query/?query_id=17888483320059182&id={user_id}&first=30&after={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}}';
Expand All @@ -17,7 +17,8 @@ class Endpoints
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 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 LIKES_BY_SHORTCODE = 'https://www.instagram.com/graphql/query/?query_id=17864450716183058&variables={"shortcode":"{{shortcode}}","first":{{count}},"after":"{{likeId}}"}';
const FOLLOWING_URL = 'https://www.instagram.com/graphql/query/?query_id=17874545323001329&id={{accountId}}&first={{count}}&after={{after}}';
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/';
Expand All @@ -33,95 +34,129 @@ class Endpoints

const GRAPH_QL_QUERY_URL = 'https://www.instagram.com/graphql/query/?query_id={{queryId}}';


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 = '')
public static function getAccountMediasJsonLink($userId, $maxId = '')
{
$url = str_replace('{username}', urlencode($username), Endpoints::ACCOUNT_MEDIAS);
return str_replace('{max_id}', urlencode($maxId), $url);
$url = str_replace('{user_id}', urlencode($userId), 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 getLastLikesByCode($code, $count, $lastLikeID)
{
$url = str_replace('{{shortcode}}', urlencode($code), static::LIKES_BY_SHORTCODE);
$url = str_replace('{{count}}', urlencode($count), $url);
$url = str_replace('{{likeId}}', urlencode($lastLikeID), $url);

return $url;
}

public static function getGraphQlUrl($queryId, $parameters)
{
$url = str_replace('{{queryId}}', urlencode($queryId), Endpoints::GRAPH_QL_QUERY_URL);
foreach ($parameters as $key => $value) {
$url .= "&$key=$value";
$url = str_replace('{{queryId}}', urlencode($queryId), static::GRAPH_QL_QUERY_URL);
if (!empty($parameters)) {
$query_string = http_build_query($parameters);
$url .= '&' . $query_string;
}
return $url;
}

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 === '') {

if ($after === '') {
$url = str_replace('&after={{after}}', '', $url);
} else {
$url = str_replace('{{after}}', urlencode($after), $url);
}
else {

return $url;
}

public static function getFollowingJsonLink($accountId, $count, $after = '')
{
$url = str_replace('{{accountId}}', urlencode($accountId), static::FOLLOWING_URL);
$url = str_replace('{{count}}', urlencode($count), $url);

if ($after === '') {
$url = str_replace('&after={{after}}', '', $url);
} else {
$url = str_replace('{{after}}', urlencode($after), $url);
}


return $url;
}

public static function getUserStoriesLink()
{
$url = self::getGraphQlUrl(InstagramQueryId::USER_STORIES, ['variables' => json_encode([])]);
return $url;
}

public static function getStoriesLink($variables)
{
$url = self::getGraphQlUrl(InstagramQueryId::STORIES, ['variables' => json_encode($variables)]);
return $url;
}
}
Loading

0 comments on commit bc5e31a

Please sign in to comment.