Advertisement
Guest User

Instagram Media Upload

a guest
Dec 16th, 2018
2,965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.09 KB | None | 0 0
  1. function SendRequest($url, $post, $post_data, $user_agent, $cookies) {
  2. $ch = curl_init();
  3. curl_setopt($ch, CURLOPT_URL, 'https://i.instagram.com/api/v1/'.$url);
  4. curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  5. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  6. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  7.  
  8. if($post) {
  9. curl_setopt($ch, CURLOPT_POST, true);
  10. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
  11. }
  12.  
  13. if($cookies) {
  14. curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
  15. } else {
  16. curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
  17. }
  18.  
  19. $response = curl_exec($ch);
  20. $http = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  21. curl_close($ch);
  22.  
  23. return array($http, $response);
  24. }
  25.  
  26. function GenerateGuid() {
  27. return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  28. mt_rand(0, 65535),
  29. mt_rand(0, 65535),
  30. mt_rand(0, 65535),
  31. mt_rand(16384, 20479),
  32. mt_rand(32768, 49151),
  33. mt_rand(0, 65535),
  34. mt_rand(0, 65535),
  35. mt_rand(0, 65535));
  36. }
  37.  
  38. function GenerateUserAgent() {
  39. $resolutions = array('720x1280', '320x480', '480x800', '1024x768', '1280x720', '768x1024', '480x320');
  40. $versions = array('GT-N7000', 'SM-N9000', 'GT-I9220', 'GT-I9100');
  41. $dpis = array('120', '160', '320', '240');
  42.  
  43. $ver = $versions[array_rand($versions)];
  44. $dpi = $dpis[array_rand($dpis)];
  45. $res = $resolutions[array_rand($resolutions)];
  46.  
  47. return 'Instagram 4.'.mt_rand(1,2).'.'.mt_rand(0,2).' Android ('.mt_rand(10,11).'/'.mt_rand(1,3).'.'.mt_rand(3,5).'.'.mt_rand(0,5).'; '.$dpi.'; '.$res.'; samsung; '.$ver.'; '.$ver.'; smdkc210; en_US)';
  48. }
  49.  
  50. function GenerateSignature($data) {
  51. return hash_hmac('sha256', $data, 'b4a23f5e39b5929e0666ac5de94c89d1618a2916');
  52. }
  53.  
  54. function GetPostData($filename) {
  55. if(!$filename) {
  56. echo "The image doesn't exist ".$filename;
  57. } else {
  58. $post_data = array('device_timestamp' => time(),
  59. 'photo' => '@'.$filename);
  60. return $post_data;
  61. }
  62. }
  63.  
  64.  
  65. // Set the username and password of the account that you wish to post a photo to
  66. $username = 'ig_username';
  67. $password = 'ig_password';
  68.  
  69. // Set the path to the file that you wish to post.
  70. // This must be jpeg format and it must be a perfect square
  71. $filename = 'pictures/test.jpg';
  72.  
  73. // Set the caption for the photo
  74. $caption = "Test caption";
  75.  
  76. // Define the user agent
  77. $agent = GenerateUserAgent();
  78.  
  79. // Define the GuID
  80. $guid = GenerateGuid();
  81.  
  82. // Set the devide ID
  83. $device_id = "android-".$guid;
  84.  
  85. /* LOG IN */
  86. // You must be logged in to the account that you wish to post a photo too
  87. // Set all of the parameters in the string, and then sign it with their API key using SHA-256
  88. $data ='{"device_id":"'.$device_id.'","guid":"'.$guid.'","username":"'.$username.'","password":"'.$password.'","Content-Type":"application/x-www-form-urlencoded; charset=UTF-8"}';
  89. $sig = GenerateSignature($data);
  90. $data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=4';
  91. $login = SendRequest('accounts/login/', true, $data, $agent, false);
  92.  
  93. if(strpos($login[1], "Sorry, an error occurred while processing this request.")) {
  94. echo "Request failed, there's a chance that this proxy/ip is blocked";
  95. } else {
  96. if(empty($login[1])) {
  97. echo "Empty response received from the server while trying to login";
  98. } else {
  99. // Decode the array that is returned
  100. $obj = @json_decode($login[1], true);
  101.  
  102. if(empty($obj)) {
  103. echo "Could not decode the response: ".$body;
  104. } else {
  105. // Post the picture
  106. $data = GetPostData($filename);
  107. $post = SendRequest('media/upload/', true, $data, $agent, true);
  108.  
  109. if(empty($post[1])) {
  110. echo "Empty response received from the server while trying to post the image";
  111. } else {
  112. // Decode the response
  113. $obj = @json_decode($post[1], true);
  114.  
  115. if(empty($obj)) {
  116. echo "Could not decode the response";
  117. } else {
  118. $status = $obj['status'];
  119.  
  120. if($status == 'ok') {
  121. // Remove and line breaks from the caption
  122. $caption = preg_replace("/\r|\n/", "", $caption);
  123.  
  124. $media_id = $obj['media_id'];
  125. $device_id = "android-".$guid;
  126. $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"}';
  127. $sig = GenerateSignature($data);
  128. $new_data = 'signed_body='.$sig.'.'.urlencode($data).'&ig_sig_key_version=4';
  129.  
  130. // Now, configure the photo
  131. $conf = SendRequest('media/configure/', true, $new_data, $agent, true);
  132.  
  133. if(empty($conf[1])) {
  134. echo "Empty response received from the server while trying to configure the image";
  135. } else {
  136. if(strpos($conf[1], "login_required")) {
  137. echo "You are not logged in. There's a chance that the account is banned";
  138. } else {
  139. $obj = @json_decode($conf[1], true);
  140. $status = $obj['status'];
  141.  
  142. if($status != 'fail') {
  143. echo "Success";
  144. } else {
  145. echo 'Fail';
  146. }
  147. }
  148. }
  149. } else {
  150. echo "Status isn't okay";
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement