Log in Register

Log in

How to auto publish post on Instagram Page with PHP/cURL and without using Instagram API (1)

Instagram is one of the most difficult apps for developers to schedule auto posting without using API. Instagram is specifically designed to support uploads from mobile phones only. Instagram team only gives whitelist access to limited  third-party apps for automatic posting on a case by case basis. Another reason is Instagram team want to fight spam and low quality photos.

I was looking for a way to auto posting from website to Instagram page using PHP/cURL without using API since last two years. Lately I found two websites that are discussing about this topic.

Posting a Photo to Instagram Without a Phone

instagram-photo-upload-batuhanorg.php

Initially I was not able to make the coding working immediately after download the source files due to several issues such as PHP 5.5 vs 5.6 issue as well as new signature key from instagram app. In this tutorial, I will show you a working draft to auto post single photo to Instagram page using PHP/cURL. I am take no responsibility if your account banned by Instagram. :)

** You need to Register before download this file. 

There are three files in this tutorial.

1. simpleimage.class.php

This is the image library discussed at Download and Save Images with PHP/cURL Web Scraper Script.

I use the image class to resize photo to meet the specification required by Instagram. You can use your own preferred image library if you wish. 

2. instagram.class.php

This is the draft instagram class to login and post photo and caption. You need to improve error handling, rewrite class method and customize the code for your own use. 

Class Instagram
{
private $username;
private $password;
private $guid;
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/';

 

First, we set private variables of Class Instagram. I use a fix $userAgent as I intend to auto post one single photo every 4 hours on a single account.

We need to have Instagram signature key to sign requests to their private API. This key is currently working well for me. You can refer to on how to extract new sginature key in future. 

EXTRACTING INSTAGRAM SIGNATURE KEY

EXTRACTING INSTAGRAM SIGNATURE KEY II

We also define Instagram URLs for API. 

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_SAFE_UPLOAD, 1);
		} 		
        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);
}

This is the cURL function for sending request to Instagram. Note that $this->instagramUrl . $url is the final url.
    $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);

The PHP version for XMAPP and live server are different. So this will take care of the CURLOPT_SAFE_UPLOAD setup issue for both.
	    if($post) {
        curl_setopt($ch, CURLOPT_POST, 1);
		if ((version_compare(PHP_VERSION, '5.5') >= 0)) {
			curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 1);
		} 		
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    }

The method will return $http code status and $response from Instagram.

    return array($http, $response);

 

This method return randomly generated globally unique identifier (GUID). 

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));
}

 

Instagram requires signature described in hex representation of a RFC 2104-compliant HMAC with the SHA256 hash algorithm. $data is request data that will be sent to Instagram. $this->instaSignature is the signature key.

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

 

This is used to setup time stamp and photo (with complete directory path). For PHP 5.5 and above, use CURLFile, else "@".

 

private function GetPostData($path)  {

	$post_data = array('device_timestamp' => time());

		if ((version_compare(PHP_VERSION, '5.5') >= 0)) {

				$post_data['photo'] = new CURLFile(realpath($path));

		} else {

                $post_data['photo'] = "@".realpath($path);

        }

    return $post_data;

}

This method login to Instagram before posting photo. It returns http code status and response from Instagram.

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';	
	return $this->Request('accounts/login/', true, $data, false);	
}

 

This method send request to post photo and return http code status and response from Instagram.

private function PostImage($photo) {
    $data = $this->GetPostData($photo);
    return $this->Request('media/upload/', true, $data, true); 	
}

This method send request to post caption of the photo and return http code status and response from Instagram.

private function PostCaption($caption, $media_id) {
    $caption = preg_replace("/\r|\n/", "", $caption);
    $device_id = "android-".$this->guid;
    $data = '{"device_id":"'.$device_id.'","guid":"'. $this->guid .'","media_id":"'.$media_id.'","caption":"'.trim($caption).'","device_timestamp":"'.time().'","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/configure/', true, $new_data, true);		
}

This method calls PostImage and PostCaption method.

public function Post($photo, $caption){
	$response = $this->PostImage($photo);
	if(empty($response[1])) {
		echo "Empty response received from the server while trying to post the image";
		exit();	
	}

	$obj = @json_decode($response[1], true);
	$status = $obj['status'];
	if($status == 'ok') {
		$media_id = $obj['media_id'];		
		$response = $this->PostCaption($caption, $media_id);	
	}		
}

 

Let's look at our main file instagram_autopost.php

<?php

include 'simpleimage.class.php';
include 'instagram.class.php';

$username = 'xxxx';   // your username
$password = 'yyyy';   // your password
$filename = 'zzzz.jpg';   // your sample photo
$caption = " #tag1 #tag2 # tag3 .....";   // your caption

$product_image= getcwd() .'\\uploads\\' . $filename;     // path to your original photo file
$square = getcwd().'\\square\\' . $filename;             // path to your resized photo file
$image = new SimpleImage(); 
$image->load($product_image); 
$image->resize(480,600); 						
$image->save($square, IMAGETYPE_JPEG);  
unset($image);

$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();	
}

$insta->Post($square, $caption);
?>

First, include image and instagram class file.

 

include 'simpleimage.class.php';
include 'instagram.class.php';

Your username, password, image fila name and caption

$username = 'xxxx';   // your username
$password = 'yyyy';   // your password
$filename = 'zzzz.jpg';   // your sample photo
$caption = " #tag1 #tag2 # tag3 .....";   // your caption

At present, Instagram accepts photo sizes below

Square : 1080x1080px, 600x600px

Landscape (horizontal) : 600x450px, 1350x1080px

Portrait (vertical) : 480×600px, 920×1200px for retina display, 1080×1350px.

Resize the original image to fit Instagram requirements. 

$product_image= getcwd() .'\\uploads\\' . $filename;     // path to your original photo file
$square = getcwd().'\\square\\' . $filename;             // path to your resized photo file
$image = new SimpleImage(); 
$image->load($product_image); 
$image->resize(600,600); 						
$image->save($square, IMAGETYPE_JPEG);  
unset($image);

If no problem on login, then proceed to post photo and caption.

$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();	
}

$insta->Post($square, $caption);

This is the sample auto post: 

 

 

 

 

 

 

 

 

Last modified on Thursday, 03 November 2016 05:59
Rate this item
(0 votes)

13 comments

  • Shahmad

    How to post video to instagram?

    posted by Shahmad Monday, 21 November 2016 05:56 Comment Link
  • Chin-Hock Tan

    Hello Shahmad,
    I have not tried video before. You might want to explore this area.

    posted by Chin-Hock Tan Friday, 25 November 2016 09:40 Comment Link
  • Akhilesh

    I am not able to create account. Please send me this code at my email id.

    posted by Akhilesh Monday, 05 December 2016 12:44 Comment Link
  • Chin-Hock Tan

    Hello Akhilesh,
    There was some problems with the setup.
    You can now registered and login to download the files.

    posted by Chin-Hock Tan Tuesday, 06 December 2016 05:22 Comment Link
  • B.M.

    Hi, I can register as new user but page hung after I click submit button.

    posted by B.M. Tuesday, 06 December 2016 15:57 Comment Link
  • John

    Thank you a lot, it worked for me!

    posted by John Saturday, 14 January 2017 21:46 Comment Link
  • Chin-Hock Tan

    Hello John,
    Glad to know that! :)

    posted by Chin-Hock Tan Sunday, 15 January 2017 04:36 Comment Link
  • Aditya

    Works perfectly, but I want to know, Can I get some data from a instagram url with out logging. I am creating analytics for website, but as it does not provide what is time, some another user follow my analytics user. I can get that from a url, but it needs sessionid, Can I get that without sessionid. Anyway, thanks for this information.

    posted by Aditya Wednesday, 08 February 2017 06:34 Comment Link
  • ali

    Hi you are amazing ..Where do I upload the photo? I ricieved this erorr : Empty response received from the server while trying to post the image

    posted by ali Saturday, 11 March 2017 09:00 Comment Link
  • Chin-Hock Tan

    Hello ali,
    You post the photo to your instagram account, the one you have the username and password.

    posted by Chin-Hock Tan Wednesday, 15 March 2017 06:59 Comment Link
  • osama fawzi

    when i try to post after several times the account got blocked !!!
    also i have used more than account at once ....like share the same post to 15 accountsusing your code ...some of the accs got locked any solution to stop being locked ??

    posted by osama fawzi Tuesday, 21 March 2017 08:41 Comment Link
  • Chin-Hock Tan

    Hello fawzi,
    You can try to put few seconds delay between postings.
    Also, check the content of $response from
    $response = $insta->Login($username, $password);

    Good luck

    posted by Chin-Hock Tan Wednesday, 22 March 2017 13:09 Comment Link
  • hardik

    Sir, What do I do after downloading these 3 files. Please help me. Once I download these three files, How do i implement them to autoupload/schedule posts on instagram

    posted by hardik Tuesday, 06 June 2017 08:39 Comment Link

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.



Anti-spam: complete the task
back to top