Advertisement
dandyraka

Post to IG

Nov 3rd, 2015
913
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.95 KB | None | 0 0
  1. <?php
  2. /*
  3.     (2014) Main source -> http://lancenewman.me/posting-a-photo-to-instagram-without-a-phone/
  4.    
  5.     I just managed to sniff Instagram traffic and fixed the code
  6.     -- Have fun - batuhan.org - Batuhan Katırcı
  7.     --- for your questions, comment @ http://batuhan.org/instagram-photo-upload-with-php/
  8.    
  9.    
  10. */
  11. function SendRequest($url, $post, $post_data, $user_agent, $cookies) {
  12.     $ch = curl_init();
  13.     curl_setopt($ch, CURLOPT_URL, 'https://instagram.com/api/v1/'.$url);
  14.     curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  15.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  16.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  17.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  18.     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  19.     if($post) {
  20.         curl_setopt($ch, CURLOPT_POST, true);
  21.         curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  22.     }
  23.     if($cookies) {
  24.         curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');            
  25.     } else {
  26.         curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
  27.     }
  28.     $response = curl_exec($ch);
  29.     $http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  30.    
  31.     curl_close($ch);
  32.    
  33.     return array($http, $response);
  34. }
  35. function GenerateGuid() {
  36.     return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  37.             mt_rand(0, 65535),
  38.             mt_rand(0, 65535),
  39.             mt_rand(0, 65535),
  40.             mt_rand(16384, 20479),
  41.             mt_rand(32768, 49151),
  42.             mt_rand(0, 65535),
  43.             mt_rand(0, 65535),
  44.             mt_rand(0, 65535));
  45. }
  46. function GenerateSignature($data) {
  47.     return hash_hmac('sha256', $data, '25eace5393646842f0d0c3fb2ac7d3cfa15c052436ee86b5406a8433f54d24a5');
  48. }
  49. function GetPostData($filename) {
  50.     if(!$filename) {
  51.         echo "The image doesn't exist ".$filename;
  52.     } else {
  53.         $post_data = array('device_timestamp' => time(),
  54.                             'photo' => '@'.$filename);
  55.         return $post_data;
  56.     }
  57. }
  58. // Set the username and password of the account that you wish to post a photo to
  59. $username = '';
  60. $password = '';
  61. // Set the path to the file that you wish to post.
  62. // This must be jpeg format and it must be a perfect square
  63. $filename = '400x400.jpg';
  64. // Set the caption for the photo
  65. $caption = "Testcaption";
  66. // Define the user agent
  67. $agent = 'Instagram 6.21.2 Android (19/4.4.2; 480dpi; 1152x1920; Meizu; MX4; mx4; mt6595; en_US)';
  68. // Define the GuID
  69. $guid = GenerateGuid();
  70. // Set the devide ID
  71. $device_id = "android-".$guid;
  72. /* LOG IN */
  73. // You must be logged in to the account that you wish to post a photo too
  74. // Set all of the parameters in the string, and then sign it with their API key using SHA-256
  75. $data = '{"device_id":"'.$device_id.'","guid":"'.$guid.'","username":"'.$username.'","password":"'.$password.'","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}';
  76. $sig = GenerateSignature($data);
  77. $data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=6';
  78. $login = SendRequest('accounts/login/', true, $data, $agent, false);
  79. if(strpos($login[1], "Sorry, an error occurred while processing this request.")) {
  80.     echo "Request failed, there's a chance that this proxy/ip is blocked";
  81. } else {            
  82.     if(empty($login[1])) {
  83.         echo "Empty response received from the server while trying to login";
  84.     } else {            
  85.         // Decode the array that is returned
  86.         $obj = @json_decode($login[1], true);
  87.         if(empty($obj)) {
  88.             echo "Could not decode the response: ".$body;
  89.         } else {
  90.             // Post the picture
  91.             $data = GetPostData($filename);
  92.             $post = SendRequest('media/upload/', true, $data, $agent, true);    
  93.             if(empty($post[1])) {
  94.                 echo "Empty response received from the server while trying to post the image";
  95.             } else {
  96.                 // Decode the response
  97.                 $obj = @json_decode($post[1], true);
  98.                 if(empty($obj)) {
  99.                     echo "Could not decode the response";
  100.                 } else {
  101.                     $status = $obj['status'];
  102.                     if($status == 'ok') {
  103.                         // Remove and line breaks from the caption
  104.                         $caption = preg_replace("/\r|\n/", "", $caption);
  105.                         $media_id = $obj['media_id'];
  106.                         $device_id = "android-".$guid;
  107.                         $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"}';  
  108.                         $sig = GenerateSignature($data);
  109.                         $new_data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=4';
  110.                         // Now, configure the photo
  111.                         $conf = SendRequest('media/configure/', true, $new_data, $agent, true);
  112.                         if(empty($conf[1])) {
  113.                             echo "Empty response received from the server while trying to configure the image";
  114.                         } else {
  115.                             if(strpos($conf[1], "login_required")) {
  116.                                 echo "You are not logged in. There's a chance that the account is banned";
  117.                             } else {
  118.                                 $obj = @json_decode($conf[1], true);
  119.                                 $status = $obj['status'];
  120.                                 if($status != 'fail') {
  121.                                     echo "Success";
  122.                                 } else {
  123.                                     echo 'Fail';
  124.                                 }
  125.                             }
  126.                         }
  127.                     } else {
  128.                         echo "Status isn't okay";
  129.                     }
  130.                 }
  131.             }
  132.         }
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement