Skip to content

Commit

Permalink
Add removeFollower method (#950)
Browse files Browse the repository at this point in the history
* Add removeFollower method

* Add removeFollower Example
  • Loading branch information
ErfanBahramali authored Aug 26, 2021
1 parent 9bdde66 commit e6d9997
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
39 changes: 39 additions & 0 deletions examples/removeFollower.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use InstagramScraper\Instagram;
use Phpfastcache\Helper\Psr16Adapter;

require __DIR__ . '/../vendor/autoload.php';

$username = 'username';
$password = 'password';

$instagram = Instagram::withCredentials(
new \GuzzleHttp\Client(),
$username,
$password,
new Psr16Adapter('Files')
);

$instagram->login();

$instagram->saveSession();


$accountId = $instagram->getAccountInfo($username)->getId();

$followers = $instagram->getFollowers($accountId);

if (isset($followers[0])) {

$lastFollower = $followers[0];
$lastFollowerId = $lastFollower['id'];
$lastFollowerUsername = $lastFollower['username'];

$instagram->removeFollower($lastFollowerId);

print_r("{$lastFollowerUsername} Removed.");
} else {

print_r('Follower Not Found.');
}
6 changes: 6 additions & 0 deletions src/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Endpoints
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/';
const REMOVE_FOLLOWER_URL = 'https://www.instagram.com/web/friendships/{{accountId}}/remove_follower/';
const USER_FEED = 'https://www.instagram.com/graphql/query/?query_id=17861995474116400&fetch_media_item_count=12&fetch_media_item_cursor=&fetch_comment_count=4&fetch_like=10';
const USER_FEED2 = 'https://www.instagram.com/?__a=1';
const USER_FEED_hash = 'https://www.instagram.com/graphql/query/?query_hash=3f01472fb28fb8aca9ad9dbc9d4578ff';
Expand Down Expand Up @@ -157,6 +158,11 @@ public static function getUnfollowUrl($accountId)
return $url;
}

public static function getRemoveFollowerUrl($accountId)
{
return str_replace('{{accountId}}', urlencode($accountId), static::REMOVE_FOLLOWER_URL);
}

public static function getFollowersJsonLink($accountId, $count, $after = '')
{
$url = str_replace('{{accountId}}', urlencode($accountId), static::FOLLOWERS_URL);
Expand Down
21 changes: 21 additions & 0 deletions src/InstagramScraper/Instagram.php
Original file line number Diff line number Diff line change
Expand Up @@ -2132,6 +2132,27 @@ public function unfollow($accountId){
}
}

/**
* @param string $accountId Account id of the profile to query
*
* @return void
* @throws InstagramException
*/
public function removeFollower($accountId)
{
$response = Request::post(Endpoints::getRemoveFollowerUrl($accountId), $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 ($jsonResponse['status'] !== 'ok') {
throw new InstagramException('Response status is ' . $jsonResponse['status'] . '. Body: ' . static::getErrorBody($response->body) . ' Something went wrong. Please report issue.', $response->code);
}
}

/**
* @param int|string|Media $mediaId
* @param int|string $text
Expand Down

0 comments on commit e6d9997

Please sign in to comment.