×

Warning

JUser: :_load: Unable to load user with ID: 676

How to auto-follow, auto-comment and auto-like a post on Instagram Page with PHP/cURL (2)

instagram

In the last article, I use a script to autopost image to Instagram. In this article, here is a script to auto-follow, auto-like and auto-comment a batch of Instagram posting based on specific tag search results.
You need to set a group of targeted tag (or keywords). The script will randomly pick up one tag each time and search for most recent postings with this tag. To avoid being detected by Instagram, the script will only auto-follow, auto-like and auto-comment not more than 40 (you can set your number in the script) postings each batch.
Owners of the posting with high like count (example 300 like count) most likely NOT interested to know who come to like their postings. So to get the best results from the script, the code will avoid postings that already have high like count and only like those postings with like count less than 20 (you can set your number in the script). This is to get the attention of the posting owners so that they can take next cause of action, such as follow you back or say thank you to your comment. As such, your followers will increase. I will show you the results in the last part of this article.
If you are already following a user, the script will not follow that user again. It will then only like the posting of that user. This is to avoid the same user sees repeating comment from us.

So if you like what the script can do for you, here is the code!

Note: Check out the code sample at bottom of this article.

There are three files in this tutorial.

1. instagram_config.php

$searchtag = array(
'instagram',
'aquamarine',
'wifi', 
'lovestory', 
);

 


This is where you set of all your targeted Instagram tags. I set four tags in this example. You should add more. I have more than 150 tags in my actual file.

2. instagram.class.php
This is the file you don't have to make any changes. It contains class for you to login Instagram, search tag, check your relationship with the posting's owner, auto follow new user, auto like and auto comment the posting.

public function Login($username, $password) {
$this->username = $username;
$this->password = $password; 
$this->guid = $this->GenerateGuid();
$device_id = "android-" . $this->guid; 
$data = '{"device_id":"'.$device_id.'","guid":"'.$this->guid.'","username":"'. $this->username.'","password":"'.$this->password.'","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}';
$sig = $this->GenerateSignature($data);
$data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6'; 
$myid = $this->Request('accounts/login/', true, $data, false); 
$decode = json_decode($myid[1], true); 
$this->my_uid = $decode['logged_in_user']['pk'];
return $myid;
}

 

Use your username and password to login to Instagram.

$this->Request('accounts/login/', true, $data, false); 

 

This is the private API for Instagram login.

public function SearchTag($tag) {
$device_id = "android-".$this->guid;
$data = '{"device_id":"'.$device_id.'","guid":"'. $this->guid .'","timezone_offset":"43200","uid":"'.$this->my_uid.'","q":"'.$tag.'","count":"50","source_type":"5","filter_type":"0","extra":"{}","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}'; 
$sig = $this->GenerateSignature($data);
$new_data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6'; 
return $this->Request('feed/tag/'.$tag.'/', true, $new_data, true); 
}

 

SearchTag object will take one tag and return the most recent 50 image postings.

public function IsFriend($user_id) {
$device_id = "android-".$this->guid;
$data = '{"device_id":"'.$device_id.'","guid":"'. $this->guid .'","uid":"'.$this->my_uid.'","module_name":"feed_timeline","user_id":"'.$user_id.'","source_type":"5","filter_type":"0","extra":"{}","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}'; 
$sig = $this->GenerateSignature($data);
$new_data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6';
return $this->Request('friendships/show/'.$user_id.'/', true, $new_data, true); 
}

 

For each image posting, we check if you have already following the owner.

public function PostFollow($user_id) {
$device_id = "android-".$this->guid;
$data = '{"device_id":"'.$device_id.'","guid":"'. $this->guid .'","uid":"'.$this->my_uid.'","module_name":"feed_timeline","user_id":"'.$user_id.'","source_type":"5","filter_type":"0","extra":"{}","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}'; 
$sig = $this->GenerateSignature($data);
$new_data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6';
return $this->Request('friendships/create/'.$user_id.'/', true, $new_data, true); 
}

 

This is the request to auto-follow a user.

public function PostComment($caption, $media_id) {
$caption = preg_replace("/\r|\n/", "", $caption);
$device_id = "android-".$this->guid;
$data = '{"device_id":"'.$device_id.'","guid":"'. $this->guid .'","uid":"'.$this->my_uid.'","comment_text":"'.trim($caption).'","source_type":"5","filter_type":"0","extra":"{}","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}'; 
$sig = $this->GenerateSignature($data);
$new_data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6';
return $this->Request('media/'.$media_id.'/comment/', true, $new_data, true); 
}

 

The is the request to auto-comment on the image posting.

public function PostLike($media_id) {
$device_id = "android-".$this->guid;
$data = '{"device_id":"'.$device_id.'","guid":"'. $this->guid .'","uid":"'.$this->my_uid.'","module_name":"feed_timeline","d":"0","source_type":"5","filter_type":"0","extra":"{}","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}'; 
$sig = $this->GenerateSignature($data);
$new_data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6';
return $this->Request('media/'.$media_id.'/like/', true, $new_data, true); 
}

 

This is the request to auto-loke the image posting.


3. instagram_follow.php
This is the main file of the script. You can set cron job to run this file every few hours. I set to 6 hours each day, so my server will run this script 4 times per day.
You need to set your Instagram username and password here so that the script will login for you. Then it will randomly pull one of the tags and search for image postings come with this tag. It then checks had you already followed the owner of each posting. If not follow yet, then auto-follow this person, auto-like and auto-comment on his/her post.

$username = 'xxxx'; // your username
$password = 'yyyy'; // your password

 

Enter your username and password here.

$response = $insta->Login($username, $password);

 

So first we login into Instagram.

$search = $searchtag[array_rand($searchtag)];
$res = $insta->SearchTag($search);

 

We randomly select one of the targeted tag in the array and get most recent postings which carry the tag.

if ($i<40) {
if ($like_count < 20 ) {
$fakecomment = $hello[array_rand($hello)].' '.'@'. $username . '' . $praise[array_rand($praise)];

 

To avoid detection, the script will process 40 images each time. If you are aggressive, then can set to a higher number. Again, I take no responsibility if your account banned by Instagram. :)

We also target for image posting with low like count to get attention from image owner. In this case, less than 20.
Then we construct fake comment such as
Hi @xxxx, so nice or
Hello @yyyy, great stuff or
@zzzz, cool picture etc.

if (!$friend['following'] && !$friend['is_private'] && !$friend['outgoing_request']) {
$res=$insta->PostFollow($user_id);
echo "<br> after follow";
if (!$haslike) {
echo "<br> All auto comment, post, Like";
sleep(rand(3,10));
$res=$insta->PostLike($media_id);
sleep(rand(3,10));
$res=$insta->PostComment($fakecomment, $media_id); 
sleep(rand(3,10)); 
}

 

Then we check our relationship with the image posting owner. If we had not followed this user before, or his/her account is not private and we never send follow request before, and we have not like this image posting, then request auto-follow, auto-like and auto-comment. The is a random 3 to 10 seconds of delay between each action to avoid detection by Instagram.

} else {
if (!$haslike) {
echo "<br> Like only";
sleep(rand(3,10));
$res=$insta->PostLike($media_id); 
}

 

Lastly, if we had followed this person already, then just like the post. No more fake comment.

So here's the results.

auto following

After running the script, check the new following.  You should see some new faces.

autofollow results1

In one of the new following, check the most recent posting of this user. You should able to find the tag ("Instagram" in this case), your fake comment and the like on this post.

 

autofollow results2

Same thing on another following.

 

reaction from users

Wait for a while. Because we follow, post a comment and like their posting, some users are kind enough to like back our posting, as well as thanks us. Happy!?

followers before autofollow

My followers and following a month plus ago. Then I run auto-post and auto-follow script for 1.5 months.

followers after autofollow

Ahh, let you see the bigger picture. (oopp, I changed the display language)

So good luck everyone! Happy instagram!

 

Code:

1. Instagram_config.php

<?php

$searchtag = array(
	'instagram',
	'aquamarine',
	'wifi', 
	'lovestory', 
);


$hello = array(	
	'Hi',
	'Hello',
	'',
);

$praise = array(	
	', so nice.',
	', great stuff.',
	', cool picture',
);
?>

 2. instagram_class.php

<?php

Class Instagram
{
public $username;
public $password;
private $guid;
private $my_uid;
private $userAgent = 'Instagram 6.21.2 Android (19/4.4.2; 480dpi; 1152x1920; Meizu; MX4; mx4; mt6595; en_US)';
private $instaSignature ='25eace5393646842f0d0c3fb2ac7d3cfa15c052436ee86b5406a8433f54d24a5';
private $instagramUrl = 'https://i.instagram.com/api/v1/';

function __construct()	{	
    if (!extension_loaded('curl')) trigger_error('php_curl extension is not loaded', E_USER_ERROR);	
}

function __destruct()	{

}	
	
private function Request($url, $post, $post_data, $cookies) {	
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->instagramUrl . $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $this->userAgent);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

    if($post) {
        curl_setopt($ch, CURLOPT_POST, 1);
		if ((version_compare(PHP_VERSION, '5.5') >= 0)) {
		} 		
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    }
	
    if($cookies) {
        curl_setopt($ch, CURLOPT_COOKIEFILE,   dirname(__FILE__). '/cookies.txt');            
    } else {
        curl_setopt($ch, CURLOPT_COOKIEJAR,  dirname(__FILE__). '/cookies.txt');
    }
    $response = curl_exec($ch);
    $http = curl_getinfo($ch, CURLINFO_HTTP_CODE);    
    curl_close($ch);    
    return array($http, $response);
}

private function GenerateGuid() {
    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', 
            mt_rand(0, 65535), 
            mt_rand(0, 65535), 
            mt_rand(0, 65535), 
            mt_rand(16384, 20479), 
            mt_rand(32768, 49151), 
            mt_rand(0, 65535), 
            mt_rand(0, 65535), 
            mt_rand(0, 65535));
}

private function GenerateSignature($data) {
    return hash_hmac('sha256', $data, $this->instaSignature); 
}

public function Login($username, $password) {
    $this->username = $username;
    $this->password = $password;	
	$this->guid = $this->GenerateGuid();
	$device_id = "android-" . $this->guid;	
	$data = '{"device_id":"'.$device_id.'","guid":"'.$this->guid.'","username":"'. $this->username.'","password":"'.$this->password.'","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}';
	$sig = $this->GenerateSignature($data);
	$data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6';	
	$myid = $this->Request('accounts/login/', true, $data, false);	
	$decode = json_decode($myid[1], true); 
    $this->my_uid = $decode['logged_in_user']['pk'];
	print_r($this->my_uid);	
	return $myid;
}

public function IsFriend($user_id) {
    $device_id = "android-".$this->guid;
    $data = '{"device_id":"'.$device_id.'","guid":"'. $this->guid .'","uid":"'.$this->my_uid.'","module_name":"feed_timeline","user_id":"'.$user_id.'","source_type":"5","filter_type":"0","extra":"{}","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}';   
    $sig = $this->GenerateSignature($data);
    $new_data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6';
	return $this->Request('friendships/show/'.$user_id.'/', true, $new_data, true);	
}		
		
public function PostComment($caption, $media_id) {
    $caption = preg_replace("/\r|\n/", "", $caption);
    $device_id = "android-".$this->guid;
    $data = '{"device_id":"'.$device_id.'","guid":"'. $this->guid .'","uid":"'.$this->my_uid.'","comment_text":"'.trim($caption).'","source_type":"5","filter_type":"0","extra":"{}","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}';   
    $sig = $this->GenerateSignature($data);
    $new_data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6';
	return $this->Request('media/'.$media_id.'/comment/', true, $new_data, true);	
} 

public function PostLike($media_id) {
    $device_id = "android-".$this->guid;
    $data = '{"device_id":"'.$device_id.'","guid":"'. $this->guid .'","uid":"'.$this->my_uid.'","module_name":"feed_timeline","d":"0","source_type":"5","filter_type":"0","extra":"{}","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}';   
    $sig = $this->GenerateSignature($data);
    $new_data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6';
	return $this->Request('media/'.$media_id.'/like/', true, $new_data, true);		
} 

public function PostFollow($user_id) {
    $device_id = "android-".$this->guid;
    $data = '{"device_id":"'.$device_id.'","guid":"'. $this->guid .'","uid":"'.$this->my_uid.'","module_name":"feed_timeline","user_id":"'.$user_id.'","source_type":"5","filter_type":"0","extra":"{}","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}';   
    $sig = $this->GenerateSignature($data);
    $new_data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6';
	return $this->Request('friendships/create/'.$user_id.'/', true, $new_data, true);	
}

public function SearchTag($tag) {
    $device_id = "android-".$this->guid;
    $data = '{"device_id":"'.$device_id.'","guid":"'. $this->guid .'","timezone_offset":"43200","uid":"'.$this->my_uid.'","q":"'.$tag.'","count":"50","source_type":"5","filter_type":"0","extra":"{}","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}';   
    $sig = $this->GenerateSignature($data);
    $new_data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6';	
	return $this->Request('feed/tag/'.$tag.'/', true, $new_data, true);		
}

}
?>

 3. instagram_follow.php

<?php
include 'instagram.class.php';
include 'instagram_config.php';

$username = 'xxxx';   // your username
$password = 'yyyy';   // your password

$insta = new instagram();
$response = $insta->Login($username, $password);


if(strpos($response[1], "Sorry")) {
    echo "Request failed, there's a chance that this proxy/ip is blocked";
	print_r($response);
	exit();
}         
if(empty($response[1])) {
    echo "Empty response received from the server while trying to login";
	print_r($response);	
	exit();	
}

$search = $searchtag[array_rand($searchtag)];
$res = $insta->SearchTag($search);

$decode = json_decode($res[1], true); 
$i=0;

foreach($decode['items'] as $data)  {
	$i++;
	$d = explode("_", $data['id']);
	$media_id = $d[0];
	$user_id = $d[1];
	$like_count = $data['like_count'];
	$comment_count = $data['comment_count'];
	$username = $data['user']['username'];
	$haslike = $data['has_liked'];

	if ($i<40)  {
		if ($like_count < 20 )  {
			$fakecomment = $hello[array_rand($hello)].' '.'@'. $username . '' . $praise[array_rand($praise)];
			$fres=$insta->IsFriend($user_id);
			$friend = json_decode($fres[1], true); 

			if (!$friend['following'] && !$friend['is_private'] && !$friend['outgoing_request'])  {
				$res=$insta->PostFollow($user_id);
				echo "<br> after follow";
				if (!$haslike) {
					echo "<br> All auto comment, post, Like";
					sleep(rand(3,10));
					$res=$insta->PostLike($media_id);
					sleep(rand(3,10));
					$res=$insta->PostComment($fakecomment, $media_id);	
					sleep(rand(3,10));	
				}				
			} else {
					if (!$haslike) {
					echo "<br> Like only";
					sleep(rand(3,10));
					$res=$insta->PostLike($media_id);	
					}
			}
		}
	}
	unset($res);	
}

exit();
?>

 

Last modified on Tuesday, 29 December 2020 06:44
Rate this item
(5 votes)
back to top