addrmwn

instagramapi

Nov 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.54 KB | None | 0 0
  1. <?php
  2. /*****************************************************************
  3. Created : 2017-02-08
  4. Author : Mr. Khwanchai Kaewyos (LookHin)
  5. E-mail : khwanchai@gmail.com
  6. Website : https://www.unzeen.com
  7. Facebook : https://www.facebook.com/LookHin
  8. Source Code On Github : https://github.com/LookHin/instagram-photo-video-upload-api
  9. Rewrite Code From : https://github.com/mgp25/Instagram-API
  10. *****************************************************************/
  11. class InstagramUpload{
  12. private $username;
  13. private $password;
  14. private $csrftoken;
  15. private $phone_id;
  16. private $guid;
  17. private $uid;
  18. private $device_id;
  19. private $cookies;
  20. private $api_url = 'https://i.instagram.com/api/v1';
  21. private $ig_sig_key = '5ad7d6f013666cc93c88fc8af940348bd067b68f0dce3c85122a923f4f74b251';
  22. private $sig_key_version = '4';
  23. private $x_ig_capabilities = '3ToAAA==';
  24. private $android_version = 18;
  25. private $android_release = '4.3';
  26. private $android_manufacturer = "Huawei";
  27. private $android_model = "EVA-L19";
  28. private $headers = array();
  29. private $user_agent = "Instagram 10.3.2 Android (18/4.3; 320dpi; 720x1280; Huawei; HWEVA; EVA-L19; qcom; en_US)";
  30. public function __construct(){
  31. $this->guid = $this->generateUUID();
  32. $this->phone_id = $this->generateUUID();
  33. $this->device_id = $this->generateDeviceId();
  34. $this->upload_id = $this->generateUploadId();
  35. $this->headers[] = "X-IG-Capabilities: ".$this->x_ig_capabilities;
  36. $this->headers[] = "X-IG-Connection-Type: WIFI";
  37. }
  38. public function Login($username="", $password=""){
  39. $this->username = $username;
  40. $this->password = $password;
  41. $this->csrftoken = $this->GetToken();
  42. $arrUidAndCooike = $this->GetLoginUidAndCookie();
  43. $this->uid = $arrUidAndCooike[0];
  44. $this->cookies = $arrUidAndCooike[1];
  45. }
  46. public function UploadPhoto($image, $caption){
  47. $this->UploadPhotoApi($image);
  48. $this->ConfigPhotoApi($caption);
  49. }
  50. public function UploadVideo($video, $image, $caption){
  51. $this->UploadVideoApi($video);
  52. $this->UploadPhotoApi($image);
  53. sleep(20);
  54. $this->ConfigVideoApi($caption);
  55. }
  56. private function GetToken(){
  57. $strUrl = $this->api_url."/si/fetch_headers/?challenge_type=signup";
  58. $ch = curl_init();
  59. curl_setopt($ch, CURLOPT_URL,$strUrl);
  60. curl_setopt($ch, CURLOPT_HEADER, true);
  61. curl_setopt($ch, CURLOPT_POST, false);
  62. curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
  63. curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
  64. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  65. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  66. $result = curl_exec($ch);
  67. curl_close ($ch);
  68. preg_match_all("|csrftoken=(.*);|U",$result,$arrOut, PREG_PATTERN_ORDER);
  69. $csrftoken = $arrOut[1][0];
  70. if($csrftoken != ""){
  71. return $csrftoken;
  72. }else{
  73. print $result;
  74. exit;
  75. }
  76. }
  77. private function GetLoginUidAndCookie(){
  78. $arrPostData = array();
  79. $arrPostData['login_attempt_count'] = "0";
  80. $arrPostData['_csrftoken'] = $this->csrftoken;
  81. $arrPostData['phone_id'] = $this->phone_id;
  82. $arrPostData['guid'] = $this->guid;
  83. $arrPostData['device_id'] = $this->device_id;
  84. $arrPostData['username'] = $this->username;
  85. $arrPostData['password'] = $this->password;
  86. $strUrl = $this->api_url."/accounts/login/";
  87. $ch = curl_init();
  88. curl_setopt($ch, CURLOPT_URL,$strUrl);
  89. curl_setopt($ch, CURLOPT_HEADER, true);
  90. curl_setopt($ch, CURLOPT_POST, true);
  91. curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
  92. curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
  93. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->generateSignature(json_encode($arrPostData)));
  94. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  95. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  96. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  97. $result = curl_exec($ch);
  98. curl_close ($ch);
  99. list($header, $body) = explode("\r\n\r\n", $result, 2);
  100. preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $header, $matches);
  101. $cookies = implode(";", $matches[1]);
  102. $arrResult = json_decode($body, true);
  103. if($arrResult['status'] == "ok"){
  104. $uid = $arrResult['logged_in_user']['pk'];
  105. return array($uid, $cookies);
  106. }else{
  107. print $body;
  108. exit;
  109. }
  110. }
  111. private function UploadPhotoApi($file){
  112. $arrPostData = array();
  113. $arrPostData['_uuid'] = $this->upload_id;
  114. $arrPostData['_csrftoken'] = $this->csrftoken;
  115. $arrPostData['upload_id'] = $this->upload_id;
  116. $arrPostData['image_compression'] = '{"lib_name":"jt","lib_version":"1.3.0","quality":"100"}';
  117. $arrPostData['photo'] = curl_file_create(realpath($file));
  118. $strUrl = $this->api_url."/upload/photo/";
  119. $ch = curl_init();
  120. curl_setopt($ch, CURLOPT_URL,$strUrl);
  121. curl_setopt($ch, CURLOPT_HEADER, false);
  122. curl_setopt($ch, CURLOPT_POST, true);
  123. curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
  124. curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
  125. curl_setopt($ch, CURLOPT_POSTFIELDS, $arrPostData);
  126. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  127. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  128. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  129. curl_setopt($ch, CURLOPT_COOKIE, $this->cookies);
  130. $result = curl_exec($ch);
  131. curl_close ($ch);
  132. $arrResult = json_decode($result, true);
  133. if($arrResult['status'] == "ok"){
  134. return true;
  135. }else{
  136. print $result;
  137. exit;
  138. }
  139. }
  140. private function UploadVideoApi($file){
  141. $arrPostData = array();
  142. $arrPostData['_uuid'] = $this->upload_id;
  143. $arrPostData['_csrftoken'] = $this->csrftoken;
  144. $arrPostData['upload_id'] = $this->upload_id;
  145. $arrPostData['media_type'] = '2';
  146. $strUrl = $this->api_url."/upload/video/";
  147. $ch = curl_init();
  148. curl_setopt($ch, CURLOPT_URL,$strUrl);
  149. curl_setopt($ch, CURLOPT_HEADER, false);
  150. curl_setopt($ch, CURLOPT_POST, true);
  151. curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
  152. curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
  153. curl_setopt($ch, CURLOPT_POSTFIELDS, $arrPostData);
  154. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  155. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  156. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  157. curl_setopt($ch, CURLOPT_COOKIE, $this->cookies);
  158. $result = curl_exec($ch);
  159. curl_close ($ch);
  160. $arrResult = json_decode($result, true);
  161. $uploadUrl = $arrResult['video_upload_urls'][3]['url'];
  162. $job = $arrResult['video_upload_urls'][3]['job'];
  163. $headers = $this->headers;
  164. $headers[] = "Session-ID: ".$this->upload_id;
  165. $headers[] = "job: ".$job;
  166. $headers[] = "Content-Disposition: attachment; filename=\"video.mp4\"";
  167. $ch = curl_init();
  168. curl_setopt($ch, CURLOPT_URL,$uploadUrl);
  169. curl_setopt($ch, CURLOPT_HEADER, false);
  170. curl_setopt($ch, CURLOPT_POST, true);
  171. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  172. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
  173. curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
  174. curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents(realpath($file)));
  175. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  176. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  177. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  178. curl_setopt($ch, CURLOPT_COOKIE, $this->cookies);
  179. $result = curl_exec($ch);
  180. curl_close ($ch);
  181. if($arrResult['status'] == "ok"){
  182. return true;
  183. }else{
  184. print $result;
  185. exit;
  186. }
  187. }
  188. private function ConfigPhotoApi($caption){
  189. $arrPostData = array();
  190. $arrPostData['media_folder'] = "Instagram";
  191. $arrPostData['source_type'] = "4";
  192. $arrPostData['filter_type'] = "0";
  193. $arrPostData['_csrftoken'] = $this->csrftoken;
  194. $arrPostData['_uid'] = $this->uid;
  195. $arrPostData['_uuid'] = $this->upload_id;
  196. $arrPostData['upload_id'] = $this->upload_id;
  197. $arrPostData['caption'] = $caption;
  198. $arrPostData['device']['manufacturer'] = $this->android_manufacturer;
  199. $arrPostData['device']['model'] = $this->android_model;
  200. $arrPostData['device']['android_version'] = $this->android_version;
  201. $arrPostData['device']['android_release'] = $this->android_release;
  202. $strUrl = $this->api_url."/media/configure/";
  203. $ch = curl_init();
  204. curl_setopt($ch, CURLOPT_URL,$strUrl);
  205. curl_setopt($ch, CURLOPT_HEADER, false);
  206. curl_setopt($ch, CURLOPT_POST, true);
  207. curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
  208. curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
  209. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->generateSignature(json_encode($arrPostData)));
  210. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  211. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  212. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  213. curl_setopt($ch, CURLOPT_COOKIE, $this->cookies);
  214. $result = curl_exec($ch);
  215. curl_close ($ch);
  216. $arrResult = json_decode($result, true);
  217. if($arrResult['status'] == "ok"){
  218. return true;
  219. }else{
  220. print $result;
  221. exit;
  222. }
  223. }
  224. private function ConfigVideoApi($caption){
  225. $arrPostData = array();
  226. $arrPostData['source_type'] = "3";
  227. $arrPostData['filter_type'] = "0";
  228. $arrPostData['poster_frame_index'] = "0";
  229. $arrPostData['length'] = "0.00";
  230. $arrPostData['"length":0'] = '"length":0.00';
  231. $arrPostData['audio_muted'] = "false";
  232. $arrPostData['video_result'] = "deprecated";
  233. $arrPostData['_csrftoken'] = $this->csrftoken;
  234. $arrPostData['_uid'] = $this->uid;
  235. $arrPostData['_uuid'] = $this->upload_id;
  236. $arrPostData['upload_id'] = $this->upload_id;
  237. $arrPostData['caption'] = $caption;
  238. $arrPostData['device']['manufacturer'] = $this->android_manufacturer;
  239. $arrPostData['device']['model'] = $this->android_model;
  240. $arrPostData['device']['android_version'] = $this->android_version;
  241. $arrPostData['device']['android_release'] = $this->android_release;
  242. $strUrl = $this->api_url."/media/configure/?video=1";
  243. $ch = curl_init();
  244. curl_setopt($ch, CURLOPT_URL,$strUrl);
  245. curl_setopt($ch, CURLOPT_HEADER, false);
  246. curl_setopt($ch, CURLOPT_POST, true);
  247. curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
  248. curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
  249. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->generateSignature(json_encode($arrPostData)));
  250. curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
  251. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  252. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  253. curl_setopt($ch, CURLOPT_COOKIE, $this->cookies);
  254. $result = curl_exec($ch);
  255. curl_close ($ch);
  256. $arrResult = json_decode($result, true);
  257. if($arrResult['status'] == "ok"){
  258. return true;
  259. }else{
  260. print $result;
  261. exit;
  262. }
  263. }
  264. private function generateUUID(){
  265. $uuid = sprintf(
  266. '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
  267. mt_rand(0, 0xffff),
  268. mt_rand(0, 0xffff),
  269. mt_rand(0, 0xffff),
  270. mt_rand(0, 0x0fff) | 0x4000,
  271. mt_rand(0, 0x3fff) | 0x8000,
  272. mt_rand(0, 0xffff),
  273. mt_rand(0, 0xffff),
  274. mt_rand(0, 0xffff)
  275. );
  276. return $uuid;
  277. }
  278. private function generateDeviceId(){
  279. return 'android-'.substr(md5(time()), 16);
  280. }
  281. private function generateSignature($data){
  282. $hash = hash_hmac('sha256', $data, $this->ig_sig_key);
  283. return 'ig_sig_key_version='.$this->sig_key_version.'&signed_body='.$hash.'.'.urlencode($data);
  284. }
  285. function generateUploadId(){
  286. return number_format(round(microtime(true) * 1000), 0, '', '');
  287. }
  288. }
  289. ?>
Add Comment
Please, Sign In to add comment