Skip to content

Commit a2a508b

Browse files
authored
search users by following and followers (#979)
1 parent dcf7dd7 commit a2a508b

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

examples/searchUsersByFollowers.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
use Phpfastcache\Helper\Psr16Adapter;
3+
4+
require __DIR__ . '/../vendor/autoload.php';
5+
6+
$instagram = \InstagramScraper\Instagram::withCredentials(new \GuzzleHttp\Client(), 'username', 'password', new Psr16Adapter('Files'));
7+
$instagram->login();
8+
sleep(2); // Delay to mimic user
9+
10+
$username = 'kevin';
11+
$search_result = [];
12+
$account = $instagram->getAccount($username);
13+
sleep(1);
14+
$search_result = $instagram->searchFollowers($account->getId(), 'andy'); // Search users with a nickname 'andy' by followers 'kevin'
15+
echo '<pre>' . json_encode($search_result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . '</pre>';

src/InstagramScraper/Endpoints.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class Endpoints
2222
const LIKES_BY_SHORTCODE = 'https://www.instagram.com/graphql/query/?query_id=17864450716183058&variables={"shortcode":"{{shortcode}}","first":{{count}},"after":"{{likeId}}"}';
2323
const FOLLOWING_URL = 'https://www.instagram.com/graphql/query/?query_id=17874545323001329&id={{accountId}}&first={{count}}&after={{after}}';
2424
const FOLLOWERS_URL = 'https://www.instagram.com/graphql/query/?query_id=17851374694183129&id={{accountId}}&first={{count}}&after={{after}}';
25+
const FOLLOWING_URL_V1 = 'https://i.instagram.com/api/v1/friendships/{{accountId}}/following/';
26+
const FOLLOWERS_URL_V1 = 'https://i.instagram.com/api/v1/friendships/{{accountId}}/followers/';
2527
const FOLLOW_URL = 'https://www.instagram.com/web/friendships/{{accountId}}/follow/';
2628
const UNFOLLOW_URL = 'https://www.instagram.com/web/friendships/{{accountId}}/unfollow/';
2729
const REMOVE_FOLLOWER_URL = 'https://www.instagram.com/web/friendships/{{accountId}}/remove_follower/';
@@ -215,6 +217,20 @@ public static function getFollowingJsonLink($accountId, $count, $after = '')
215217
return $url;
216218
}
217219

220+
public static function getFollowersUrl_v1($accountId)
221+
{
222+
$url = str_replace('{{accountId}}', urlencode($accountId), static::FOLLOWERS_URL_V1);
223+
224+
return $url;
225+
}
226+
227+
public static function getFollowingUrl_v1($accountId)
228+
{
229+
$url = str_replace('{{accountId}}', urlencode($accountId), static::FOLLOWING_URL_V1);
230+
231+
return $url;
232+
}
233+
218234
public static function getUserStoriesLink($variables=[])
219235
{
220236
$url = self::getGraphQlUrl(InstagramQueryId::USER_STORIES, ['variables' => json_encode($variables)]);

src/InstagramScraper/Instagram.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,6 +1796,80 @@ public function getPaginateAllFollowing($accountId, $pageSize = 20, $nextPage =
17961796
'accounts' => $accounts
17971797
];
17981798
}
1799+
1800+
/**
1801+
* Search users by followers
1802+
* @param string $accountId Account id of the profile to query
1803+
* @param string $query Query to search by followers
1804+
*
1805+
* @return array
1806+
* @throws InstagramException
1807+
*/
1808+
public function searchFollowers($accountId, $query = '')
1809+
{
1810+
$response = Request::get(
1811+
Endpoints::getFollowersUrl_v1($accountId),
1812+
array_merge(
1813+
['x-ig-app-id' => self::X_IG_APP_ID],
1814+
$this->generateHeaders($this->userSession)
1815+
),
1816+
array(
1817+
"search_surface" => "follow_list_page",
1818+
"query" => $query,
1819+
"enable_groups" => "true"
1820+
)
1821+
);
1822+
1823+
if ($response->code !== static::HTTP_OK) {
1824+
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
1825+
}
1826+
1827+
$jsonResponse = $this->decodeRawBodyToJson($response->raw_body);
1828+
1829+
if ($jsonResponse['status'] !== 'ok') {
1830+
throw new InstagramException('Response status is ' . $jsonResponse['status'] . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
1831+
}
1832+
1833+
return $jsonResponse;
1834+
}
1835+
1836+
/**
1837+
* Search users by following
1838+
* @param string $accountId Account id of the profile to query
1839+
* @param string $query Query to search by following
1840+
*
1841+
* @return array
1842+
* @throws InstagramException
1843+
*/
1844+
public function searchFollowing($accountId, $query = '')
1845+
{
1846+
$response = Request::get(
1847+
Endpoints::getFollowingUrl_v1($accountId),
1848+
array_merge(
1849+
['x-ig-app-id' => self::X_IG_APP_ID],
1850+
$this->generateHeaders($this->userSession)
1851+
),
1852+
array(
1853+
"includes_hashtags" => "false",
1854+
"search_surface" => "follow_list_page",
1855+
"query" => $query,
1856+
"enable_groups" => "true"
1857+
)
1858+
);
1859+
1860+
if ($response->code !== static::HTTP_OK) {
1861+
throw new InstagramException('Response code is ' . $response->code . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
1862+
}
1863+
1864+
$jsonResponse = $this->decodeRawBodyToJson($response->raw_body);
1865+
1866+
if ($jsonResponse['status'] !== 'ok') {
1867+
throw new InstagramException('Response status is ' . $jsonResponse['status'] . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
1868+
}
1869+
1870+
return $jsonResponse;
1871+
}
1872+
17991873
/**
18001874
* @param array $reel_ids - array of instagram user ids
18011875
* @return array

0 commit comments

Comments
 (0)