Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Instagram photo upload php
<?php
/*
(2014) Main source -> http://lancenewman.me/posting-a-photo-to-instagram-without-a-phone/
I just managed to sniff Instagram traffic and fixed the code
-- Have fun - batuhan.org - Batuhan Katırcı
--- for your questions, comment @ http://batuhan.org/instagram-photo-upload-with-php/
*/
function SendRequest($url, $post, $post_data, $user_agent, $cookies) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://instagram.com/api/v1/'.$url);
curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if($post) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
}
if($cookies) {
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
} else {
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
}
$response = curl_exec($ch);
$http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array($http, $response);
}
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));
}
function GenerateSignature($data) {
return hash_hmac('sha256', $data, '25eace5393646842f0d0c3fb2ac7d3cfa15c052436ee86b5406a8433f54d24a5');
}
function GetPostData($filename) {
if(!$filename) {
echo "The image doesn't exist ".$filename;
} else {
$post_data = array('device_timestamp' => time(),
'photo' => '@'.$filename);
return $post_data;
}
}
// Set the username and password of the account that you wish to post a photo to
$username = '';
$password = '';
// Set the path to the file that you wish to post.
// This must be jpeg format and it must be a perfect square
$filename = '400x400.jpg';
// Set the caption for the photo
$caption = "Testcaption";
// Define the user agent
$agent = 'Instagram 6.21.2 Android (19/4.4.2; 480dpi; 1152x1920; Meizu; MX4; mx4; mt6595; en_US)';
// Define the GuID
$guid = GenerateGuid();
// Set the devide ID
$device_id = "android-".$guid;
/* LOG IN */
// You must be logged in to the account that you wish to post a photo too
// Set all of the parameters in the string, and then sign it with their API key using SHA-256
$data = '{"device_id":"'.$device_id.'","guid":"'.$guid.'","username":"'.$username.'","password":"'.$password.'","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}';
$sig = GenerateSignature($data);
$data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6';
$login = SendRequest('accounts/login/', true, $data, $agent, false);
if(strpos($login[1], "Sorry, an error occurred while processing this request.")) {
echo "Request failed, there's a chance that this proxy/ip is blocked";
} else {
if(empty($login[1])) {
echo "Empty response received from the server while trying to login";
} else {
// Decode the array that is returned
$obj = @json_decode($login[1], true);
if(empty($obj)) {
echo "Could not decode the response: ".$body;
} else {
// Post the picture
$data = GetPostData($filename);
$post = SendRequest('media/upload/', true, $data, $agent, true);
if(empty($post[1])) {
echo "Empty response received from the server while trying to post the image";
} else {
// Decode the response
$obj = @json_decode($post[1], true);
if(empty($obj)) {
echo "Could not decode the response";
} else {
$status = $obj['status'];
if($status == 'ok') {
// Remove and line breaks from the caption
$caption = preg_replace("/\r|\n/", "", $caption);
$media_id = $obj['media_id'];
$device_id = "android-".$guid;
$data = '{"device_id":"'.$device_id.'","guid":"'.$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 = GenerateSignature($data);
$new_data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=4';
// Now, configure the photo
$conf = SendRequest('media/configure/', true, $new_data, $agent, true);
if(empty($conf[1])) {
echo "Empty response received from the server while trying to configure the image";
} else {
if(strpos($conf[1], "login_required")) {
echo "You are not logged in. There's a chance that the account is banned";
} else {
$obj = @json_decode($conf[1], true);
$status = $obj['status'];
if($status != 'fail') {
echo "Success";
} else {
echo 'Fail';
}
}
}
} else {
echo "Status isn't okay";
}
}
}
}
}
}
@amieiro

This comment has been minimized.

Show comment
Hide comment
@amieiro

amieiro Aug 26, 2015

I have forked this (https://gist.github.com/amieiro/4d3514d737691e36378c) because there is a problem if the PHP version >= 5.6, because the default CURLOPT_SAFE_UPLOAD value in the curl_setopt() function has change from false to true. http://php.net/manual/en/function.curl-setopt.php
Github doesn't support Gist pull requests. Can you change the Gist?
Thank you.

amieiro commented Aug 26, 2015

I have forked this (https://gist.github.com/amieiro/4d3514d737691e36378c) because there is a problem if the PHP version >= 5.6, because the default CURLOPT_SAFE_UPLOAD value in the curl_setopt() function has change from false to true. http://php.net/manual/en/function.curl-setopt.php
Github doesn't support Gist pull requests. Can you change the Gist?
Thank you.

@blackstalk

This comment has been minimized.

Show comment
Hide comment
@blackstalk

blackstalk Jan 9, 2016

As of today, i have been able to make this work by updating CURLOPT_URL to:
curl_setopt($ch, CURLOPT_URL, 'https://i.instagram.com/api/v1/'.$url);

blackstalk commented Jan 9, 2016

As of today, i have been able to make this work by updating CURLOPT_URL to:
curl_setopt($ch, CURLOPT_URL, 'https://i.instagram.com/api/v1/'.$url);

@nadersawalha

This comment has been minimized.

Show comment
Hide comment
@nadersawalha

nadersawalha Feb 8, 2016

keep giving me this error
Parse error: syntax error, unexpected T_ELSE in instagram-photo-upload-batuhanorg.php on line 135
??

nadersawalha commented Feb 8, 2016

keep giving me this error
Parse error: syntax error, unexpected T_ELSE in instagram-photo-upload-batuhanorg.php on line 135
??

@Fayozjon

This comment has been minimized.

Show comment
Hide comment
@Fayozjon

Fayozjon Mar 23, 2016

It works ))
Thanks!
Need to update url to i.instagram
@blackstalk Good job dude!

Fayozjon commented Mar 23, 2016

It works ))
Thanks!
Need to update url to i.instagram
@blackstalk Good job dude!

@oratiostuff

This comment has been minimized.

Show comment
Hide comment
@oratiostuff

oratiostuff Mar 23, 2016

Good afternoon, I wonder if anyone post video on instagram

oratiostuff commented Mar 23, 2016

Good afternoon, I wonder if anyone post video on instagram

@codecious

This comment has been minimized.

Show comment
Hide comment
@codecious

codecious Apr 2, 2016

it gives success respond but no photo uploaded ?

codecious commented Apr 2, 2016

it gives success respond but no photo uploaded ?

@TonyStark

This comment has been minimized.

Show comment
Hide comment
@TonyStark

TonyStark Apr 13, 2016

Its workig thanks

TonyStark commented Apr 13, 2016

Its workig thanks

@ibasaw

This comment has been minimized.

Show comment
Hide comment
@ibasaw

ibasaw Apr 14, 2016

array(2) { [0]=> int(200) [1]=> string(84) "{"status":"ok","errors":{"error":["Sorry, there was a problem with your request."]}}" } Status isn't okay

ibasaw commented Apr 14, 2016

array(2) { [0]=> int(200) [1]=> string(84) "{"status":"ok","errors":{"error":["Sorry, there was a problem with your request."]}}" } Status isn't okay
@kiwina

This comment has been minimized.

Show comment
Hide comment
@kiwina

kiwina Apr 30, 2016

'photo' => new CURLFile($filename) FOR php 5.6+

kiwina commented Apr 30, 2016

'photo' => new CURLFile($filename) FOR php 5.6+

@bladeism

This comment has been minimized.

Show comment
Hide comment
@bladeism

bladeism May 8, 2016

This is weird I always get Status isn't okay

But in my other account it is doing fine.

bladeism commented May 8, 2016

This is weird I always get Status isn't okay

But in my other account it is doing fine.

@Camerton

This comment has been minimized.

Show comment
Hide comment
@Camerton

Camerton Aug 23, 2016

it shows answer that invalid domain! Does it work now?

Camerton commented Aug 23, 2016

it shows answer that invalid domain! Does it work now?

@Aylshamjake

This comment has been minimized.

Show comment
Hide comment
@Aylshamjake

Aylshamjake Aug 26, 2016

For those of you using php version 5.6 or higher, you will need the following to make the uploader work.

curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);

This is because in versions 5.5 and lower, this is set to false by default... Hope it helps.

Aylshamjake commented Aug 26, 2016

For those of you using php version 5.6 or higher, you will need the following to make the uploader work.

curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);

This is because in versions 5.5 and lower, this is set to false by default... Hope it helps.

@pablodalma93

This comment has been minimized.

Show comment
Hide comment
@pablodalma93

pablodalma93 Sep 8, 2016

Hi, it's not working for me, it says "{"status": "fail", "message": "Please update your Instagram app to continue posting photos"}" } , it's failing in this line specifically $conf = SendRequest('media/configure/', true, $new_data, $agent, true);

what it could be?

pablodalma93 commented Sep 8, 2016

Hi, it's not working for me, it says "{"status": "fail", "message": "Please update your Instagram app to continue posting photos"}" } , it's failing in this line specifically $conf = SendRequest('media/configure/', true, $new_data, $agent, true);

what it could be?

@bazingarj

This comment has been minimized.

Show comment
Hide comment
@bazingarj

bazingarj Sep 19, 2016

Array ( [0] => 400 [1] => {"status": "fail", "message": "login_required"} ) Status isn't okay

bazingarj commented Sep 19, 2016

Array ( [0] => 400 [1] => {"status": "fail", "message": "login_required"} ) Status isn't okay

@Istanbouly

This comment has been minimized.

Show comment
Hide comment
@Istanbouly

Istanbouly Nov 21, 2016

am using php 7 and its not working for me I already change the CURLOPT_SAFE_UPLOAD to false but still not working it gives me these errors please help

Warning: curl_setopt(): Disabling safe uploads is no longer supported in D:\xammp\htdocs\instagram\index.php on line 19

Notice: Undefined variable: body in D:\xammp\htdocs\instagram\index.php on line 89
Could not decode the response:

Istanbouly commented Nov 21, 2016

am using php 7 and its not working for me I already change the CURLOPT_SAFE_UPLOAD to false but still not working it gives me these errors please help

Warning: curl_setopt(): Disabling safe uploads is no longer supported in D:\xammp\htdocs\instagram\index.php on line 19

Notice: Undefined variable: body in D:\xammp\htdocs\instagram\index.php on line 89
Could not decode the response:

@mukeshmit

This comment has been minimized.

Show comment
Hide comment
@mukeshmit

mukeshmit Jan 14, 2017

i am using php 7 and in response getting ,
Array ( [0] => 400 [1] => {"message": "Your version of Instagram is out of date. Please upgrade your app to log in to Instagram.", "status": "fail", "error_type": "needs_upgrade"} )

what i should do for this,please suggest.

mukeshmit commented Jan 14, 2017

i am using php 7 and in response getting ,
Array ( [0] => 400 [1] => {"message": "Your version of Instagram is out of date. Please upgrade your app to log in to Instagram.", "status": "fail", "error_type": "needs_upgrade"} )

what i should do for this,please suggest.

@ruslanbakanov

This comment has been minimized.

Show comment
Hide comment
@ruslanbakanov

ruslanbakanov Jan 25, 2017

Array ( [0] => 400 [1] => {"status": "fail", "message": "login_required"} ) Status isn't okay

The same situation :(

ruslanbakanov commented Jan 25, 2017

Array ( [0] => 400 [1] => {"status": "fail", "message": "login_required"} ) Status isn't okay

The same situation :(

@Bahhous

This comment has been minimized.

Show comment
Hide comment
@Bahhous

Bahhous Jan 26, 2017

when i send the post all wath i got is : "Array ( [0] => 0 [1] => ) "
any help

Bahhous commented Jan 26, 2017

when i send the post all wath i got is : "Array ( [0] => 0 [1] => ) "
any help

@alpaltunel

This comment has been minimized.

Show comment
Hide comment
@alpaltunel

alpaltunel Apr 14, 2017

I used the original code but the Instagram banned my account. If you want to use this code create a fake account and try. its best to save guid to a location and send it everytime you send the photo.

alpaltunel commented Apr 14, 2017

I used the original code but the Instagram banned my account. If you want to use this code create a fake account and try. its best to save guid to a location and send it everytime you send the photo.

@golddave

This comment has been minimized.

Show comment
Hide comment
@golddave

golddave Dec 28, 2017

I've been using this for a while but recently it started failing with a "Status isn't okay" message. Any thoughts?

golddave commented Dec 28, 2017

I've been using this for a while but recently it started failing with a "Status isn't okay" message. Any thoughts?

@HusseinAhmed2018

This comment has been minimized.

Show comment
Hide comment
@HusseinAhmed2018

HusseinAhmed2018 Jan 9, 2018

code give me Success but its not upload photo

HusseinAhmed2018 commented Jan 9, 2018

code give me Success but its not upload photo

@golddave

This comment has been minimized.

Show comment
Hide comment
@golddave

golddave Jan 9, 2018

I've added some debug code to my PHP and I see where the error is now. When the script tries to log in I'm getting the following response from the API:
{"message": "checkpoint_required", "checkpoint_url": "https://i.instagram.com/challenge/2157728006/2TQPdErb7M/", "lock": true, "status": "fail", "error_type": "checkpoint_challenge_required"}

I can get past this error by spoofing my IP address in the curl statement but then I get an empty response.

golddave commented Jan 9, 2018

I've added some debug code to my PHP and I see where the error is now. When the script tries to log in I'm getting the following response from the API:
{"message": "checkpoint_required", "checkpoint_url": "https://i.instagram.com/challenge/2157728006/2TQPdErb7M/", "lock": true, "status": "fail", "error_type": "checkpoint_challenge_required"}

I can get past this error by spoofing my IP address in the curl statement but then I get an empty response.

@ZYSHAQ

This comment has been minimized.

Show comment
Hide comment
@ZYSHAQ

ZYSHAQ Feb 8, 2018

Any updates?

ZYSHAQ commented Feb 8, 2018

Any updates?

@spjoshis

This comment has been minimized.

Show comment
Hide comment
@spjoshis

spjoshis Feb 26, 2018

Good example! But what if i do not want to use username and password to upload photo? Can i use access token for this?

spjoshis commented Feb 26, 2018

Good example! But what if i do not want to use username and password to upload photo? Can i use access token for this?

@enggaraziz

This comment has been minimized.

Show comment
Hide comment
@enggaraziz

enggaraziz Apr 3, 2018

any path parameter for upload has change?
i got success but, i'm check on instagram wasn't uploaded.

enggaraziz commented Apr 3, 2018

any path parameter for upload has change?
i got success but, i'm check on instagram wasn't uploaded.

@mattwood1

This comment has been minimized.

Show comment
Hide comment
@mattwood1

mattwood1 May 4, 2018

Is it possible for multiple images at all?

mattwood1 commented May 4, 2018

Is it possible for multiple images at all?

@somosdigitalmentegsm

This comment has been minimized.

Show comment
Hide comment
@somosdigitalmentegsm

somosdigitalmentegsm Jun 29, 2018

Hola amigo quedo excelente jajajajaja https://amistadigital.com/

somosdigitalmentegsm commented Jun 29, 2018

Hola amigo quedo excelente jajajajaja https://amistadigital.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment