Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Logowanie do Instagrama bez użycia API
Forum PHP.pl > Forum > PHP
Manderigon
Witam.
Napisałem skrypt, który loguje się do Instagrama bez użycia API i chciałbym, aby skrypt również miał możliwość obserwacji innych osób. Niestety problem stwarza AJAX, który również jest wykonywany. W efekcie wykonywania skryptu nie dostaję nic. Do linii 40 śmiga wszystko jak powinno.
Kod:

  1. <?php
  2.  
  3.  
  4. $c = curl_init();
  5. curl_setopt($c, CURLOPT_URL, 'https://instagram.com/accounts/login/?force_classic_login');
  6. curl_setopt($c, CURLOPT_REFERER, 'https://instagram.com/accounts/login/?force_classic_login');
  7. curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
  8. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  9. curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  10. curl_setopt($c, CURLOPT_COOKIEFILE, 'cookiess.txt');
  11. curl_setopt($c, CURLOPT_COOKIEJAR, 'cookiess.txt');
  12. $page = curl_exec($c);
  13. curl_close($c);
  14.  
  15. preg_match_all('/<input type="hidden" name="csrfmiddlewaretoken" value="([A-z0-9]{32})"\/>/', $page, $token);
  16.  
  17. $c = curl_init();
  18. curl_setopt($c, CURLOPT_URL, 'https://instagram.com/accounts/login/?force_classic_login');
  19. curl_setopt($c, CURLOPT_REFERER, 'https://instagram.com/accounts/login/?force_classic_login');
  20. curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  21. curl_setopt($c, CURLOPT_POST, true);
  22. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  23. curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
  24. curl_setopt($c, CURLOPT_HTTPHEADER, array(
  25. 'Origin: <a href="https://instagram.com'" target="_blank">https://instagram.com'</a>,
  26. 'Accept-Encoding: gzip, deflate',
  27. 'Accept-Language: en-US,en;q=0.8',
  28. 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36',
  29. 'Content-Type: application/x-www-form-urlencoded',
  30. 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  31. 'Cache-Control: max-age=0',
  32. 'Connection: keep-alive'
  33. ));
  34. curl_setopt($c, CURLOPT_POSTFIELDS, "csrfmiddlewaretoken=".$token[1][0]."&username=ughjktr&password=dataurodzenia2");
  35. curl_setopt($c, CURLOPT_COOKIEFILE, 'cookiess.txt');
  36. curl_setopt($c, CURLOPT_COOKIEJAR, 'cookiess.txt');
  37. $page = curl_exec($c);
  38. curl_close($c);
  39.  
  40.  
  41. $c = curl_init();
  42. curl_setopt($c, CURLOPT_URL, 'https://instagram.com/web/friendships/223116496/follow/');
  43. curl_setopt($c, CURLOPT_REFERER, 'https://instagram.com/djeb_1/');
  44. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  45. curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  46. curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
  47. curl_setopt($c, CURLOPT_HTTPHEADER, array(
  48. 'Origin: <a href="https://instagram.com'" target="_blank">https://instagram.com'</a>,
  49. 'Accept-Encoding: gzip, deflate',
  50. 'X-Instagram-AJAX: 1',
  51. 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36',
  52. 'Accept-Language: en-US,en;q=0.8',
  53. 'X-CSRFToken: '.$token[1][0],
  54. 'X-Requested-With: XMLHttpRequest',
  55. 'Connection: keep-alive',
  56. 'Content-Length: 0'
  57. ));
  58. curl_setopt($c, CURLOPT_COOKIEFILE, 'cookiess.txt');
  59. curl_setopt($c, CURLOPT_COOKIEJAR, 'cookiess.txt');
  60. $page = curl_exec($c);
  61. curl_close($c);
  62.  
  63.  
  64. $c = curl_init();
  65. curl_setopt($c, CURLOPT_URL, 'https://instagram.com/ajax/bz');
  66. curl_setopt($c, CURLOPT_REFERER, 'https://instagram.com/djeb_1/');
  67. curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
  68. curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
  69. curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
  70. curl_setopt($c, CURLOPT_POST, true);
  71. curl_setopt($c, CURLOPT_HTTPHEADER, array(
  72. 'Origin: <a href="https://instagram.com'" target="_blank">https://instagram.com'</a>,
  73. 'Accept-Encoding: gzip, deflate',
  74. 'X-Instagram-AJAX: 1',
  75. 'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36',
  76. 'Content-Type: application/json; charset=UTF-8',
  77. 'Accept-Language: en-US,en;q=0.8',
  78. 'X-CSRFToken: '.$token[1][0],
  79. 'Accept: application/json, text/javascript, */*; q=0.01',
  80. 'Connection: keep-alive',
  81. 'X-Requested-With: XMLHttpRequest'
  82. ));
  83. curl_setopt($c, CURLOPT_POSTFIELDS, '{""q"":[{""page_id"":""2sw885"",""posts"":[[""slipstream:action"",{""description"":""followUserAttempt"",""event_name"":""action"",""extra"":""{\\""gk\\"":{},\\""source\\"":\\""profilePage\\""}"",""hostname"":""instagram.com"",""path"":""/djeb_1/"",""referer"":""https://instagram.com/p/7F5uR-F2YP/?taken-by=djeb_1"",""url"":""https://instagram.com/djeb_1/""},1441119979015,0],[""slipstream:action"",{""description"":""followUserSuccess"",""event_name"":""action"",""extra"":""{\\""gk\\"":{},\\""source\\"":\\""profilePage\\""}"",""hostname"":""instagram.com"",""path"":""/djeb_1/"",""referer"":""https://instagram.com/p/7F5uR-F2YP/?taken-by=djeb_1"",""url"":""https://instagram.com/djeb_1/""},1441119979477,0]],""trigger"":""slipstream:action""}],""ts"":1441119980877}');
  84. curl_setopt($c, CURLOPT_COOKIEFILE, 'cookiess.txt');
  85. curl_setopt($c, CURLOPT_COOKIEJAR, 'cookiess.txt');
  86. $page = curl_exec($c);
  87. curl_close($c);
  88.  
  89. echo $page;
  90.  
  91. ?>


oraz wykonywane zapytania:

Kod
curl "https://instagram.com/web/friendships/223116496/follow/" -X POST -H "Cookie: mid=VeV2pwAEAAFzOFSRssLQ0oNH5Zei; sessionid=IGSCc929c229abe18cbf4f88f2fa86996dc3928cb35750344680b9b901ab7542ba2a"%"3AoK4mQH104a01IiK5MpHPLfULZQ6kOjdM"%"3A"%"7B"%"22_token_ver"%"22"%"3A1"%"2C"%"22_auth_user_id"%"22"%"3A2149829613"%"2C"%"22_token"%"22"%"3A"%"222149829613"%"3A7xiCtr5OmnuUkRRx8fT2kL2Cy0WTlKhu"%"3A16acc902f30747fdc27655e886d82b726ec79bc51f0b66a841b736e01a2b83f5"%"22"%"2C"%"22_auth_user_backend"%"22"%"3A"%"22accounts.backends.CaseInsensitiveModelBackend"%"22"%"2C"%"22last_refreshed"%"22"%"3A1441112449.04923"%"2C"%"22_platform"%"22"%"3A4"%"7D; ig_pr=1; ig_vw=1920; csrftoken=0179af5ebf94270a0e9e42c0e29d3676; ds_user_id=2149829613" -H "Origin: https://instagram.com" -H "Accept-Encoding: gzip, deflate" -H "X-Instagram-AJAX: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36" -H "Accept-Language: en-US,en;q=0.8" -H "Accept: */*" -H "Referer: https://instagram.com/djeb_1/" -H "X-CSRFToken: 0179af5ebf94270a0e9e42c0e29d3676" -H "X-Requested-With: XMLHttpRequest" -H "Connection: keep-alive" -H "Content-Length: 0" --compressed


curl "https://instagram.com/ajax/bz" -H "Cookie: mid=VeV2pwAEAAFzOFSRssLQ0oNH5Zei; sessionid=IGSCc929c229abe18cbf4f88f2fa86996dc3928cb35750344680b9b901ab7542ba2a"%"3AoK4mQH104a01IiK5MpHPLfULZQ6kOjdM"%"3A"%"7B"%"22_token_ver"%"22"%"3A1"%"2C"%"22_auth_user_id"%"22"%"3A2149829613"%"2C"%"22_token"%"22"%"3A"%"222149829613"%"3A7xiCtr5OmnuUkRRx8fT2kL2Cy0WTlKhu"%"3A16acc902f30747fdc27655e886d82b726ec79bc51f0b66a841b736e01a2b83f5"%"22"%"2C"%"22_auth_user_backend"%"22"%"3A"%"22accounts.backends.CaseInsensitiveModelBackend"%"22"%"2C"%"22last_refreshed"%"22"%"3A1441112449.04923"%"2C"%"22_platform"%"22"%"3A4"%"7D; ig_pr=1; ig_vw=1920; csrftoken=0179af5ebf94270a0e9e42c0e29d3676; ds_user_id=2149829613" -H "Origin: https://instagram.com" -H "Accept-Encoding: gzip, deflate" -H "X-Instagram-AJAX: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36" -H "Content-Type: application/json; charset=UTF-8" -H "Accept-Language: en-US,en;q=0.8" -H "Accept: application/json, text/javascript, */*; q=0.01" -H "Referer: https://instagram.com/djeb_1/" -H "X-CSRFToken: 0179af5ebf94270a0e9e42c0e29d3676" -H "X-Requested-With: XMLHttpRequest" -H "Connection: keep-alive" --data-binary "{""q"":[{""page_id"":""2sw885"",""posts"":[[""slipstream:action"",{""description"":""followUserAttempt"",""event_name"":""action"",""extra"":""{\\""gk\\"":{},\\""source\\"":\\""profilePage\\""}"",""hostname"":""instagram.com"",""path"":""/djeb_1/"",""referer"":""https://instagram.com/p/7F5uR-F2YP/?taken-by=djeb_1"",""url"":""https://instagram.com/djeb_1/""},1441119979015,0],[""slipstream:action"",{""description"":""followUserSuccess"",""event_name"":""action"",""extra"":""{\\""gk\\"":{},\\""source\\"":\\""profilePage\\""}"",""hostname"":""instagram.com"",""path"":""/djeb_1/"",""referer"":""https://instagram.com/p/7F5uR-F2YP/?taken-by=djeb_1"",""url"":""https://instagram.com/djeb_1/""},1441119979477,0]],""trigger"":""slipstream:action""}],""ts"":1441119980877}" --compressed


Proszę o pomoc.
Pozdrawiam.

P.S Nie zwracajcie uwagi na znaczniki przy Origin, za każdym razem jak je usuwam to pojawiają się na nowo w edytorze na forum.
markuz
Jak nie potrafie sobie poradzić z jakimś requestem to zazwyczaj otwieram konsolę w przeglądarce, wchodzę w zakładkę Network (Firefox) wykonuje ruchy które mnie interesują (logowanie itp.) a następnie dla każdego requestu "Copy as cURL" - potem wklejam to do PHP :
  1. exec("curl xxxx", $output);
  2. var_export($output);

I modyfikuje już bezpośrednio komendy curl - tak jest łatwiej, możesz spróbować smile.gif
Manderigon
Wykonywane zapytania CURL zamieściłem na samym dole w pierwszym poście.

Efekt kodu 1:

  1. exec('curl "https://instagram.com/web/friendships/223116496/follow/" -X POST -H "Cookie: mid=VeV2pwAEAAFzOFSRssLQ0oNH5Zei; sessionid=IGSCc929c229abe18cbf4f88f2fa86996dc3928cb35750344680b9b901ab7542ba2a"%"3AoK4mQH104a01IiK5MpHPLfULZQ6kOjdM"%"3A"%"7B"%"22_token_ver"%"22"%"3A1"%"2C"%"22_auth_user_id"%"22"%"3A2149829613"%"2C"%"22_token"%"22"%"3A"%"222149829613"%"3A7xiCtr5OmnuUkRRx8fT2kL2Cy0WTlKhu"%"3A16acc902f30747fdc27655e886d82b726ec79bc51f0b66a841b736e01a2b83f5"%"22"%"2C"%"22_auth_user_backend"%"22"%"3A"%"22accounts.backends.CaseInsensitiveModelBackend"%"22"%"2C"%"22last_refreshed"%"22"%"3A1441112449.04923"%"2C"%"22_platform"%"22"%"3A4"%"7D; ig_pr=1; ig_vw=1920; csrftoken=0179af5ebf94270a0e9e42c0e29d3676; ds_user_id=2149829613" -H "Origin: <a href="https://instagram.com&quot;" target="_blank">https://instagram.com"</a> -H "Accept-Encoding: gzip, deflate" -H "X-Instagram-AJAX: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36" -H "Accept-Language: en-US,en;q=0.8" -H "Accept: */*" -H "Referer: <a href="https://instagram.com/djeb_1/&quot;" target="_blank">https://instagram.com/djeb_1/"</a> -H "X-CSRFToken: 0179af5ebf94270a0e9e42c0e29d3676" -H "X-Requested-With: XMLHttpRequest" -H "Connection: keep-alive" -H "Content-Length: 0" --compressed', $output);
  2. var_export($output);


Kod
array ( 0 => '{"status":"ok"}', )



Efekt kodu 2:
  1. exec('curl "https://instagram.com/ajax/bz" -H "Cookie: mid=VeV2pwAEAAFzOFSRssLQ0oNH5Zei; sessionid=IGSCc929c229abe18cbf4f88f2fa86996dc3928cb35750344680b9b901ab7542ba2a"%"3AoK4mQH104a01IiK5MpHPLfULZQ6kOjdM"%"3A"%"7B"%"22_token_ver"%"22"%"3A1"%"2C"%"22_auth_user_id"%"22"%"3A2149829613"%"2C"%"22_token"%"22"%"3A"%"222149829613"%"3A7xiCtr5OmnuUkRRx8fT2kL2Cy0WTlKhu"%"3A16acc902f30747fdc27655e886d82b726ec79bc51f0b66a841b736e01a2b83f5"%"22"%"2C"%"22_auth_user_backend"%"22"%"3A"%"22accounts.backends.CaseInsensitiveModelBackend"%"22"%"2C"%"22last_refreshed"%"22"%"3A1441112449.04923"%"2C"%"22_platform"%"22"%"3A4"%"7D; ig_pr=1; ig_vw=1920; csrftoken=0179af5ebf94270a0e9e42c0e29d3676; ds_user_id=2149829613" -H "Origin: <a href="https://instagram.com&quot;" target="_blank">https://instagram.com"</a> -H "Accept-Encoding: gzip, deflate" -H "X-Instagram-AJAX: 1" -H "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.132 Safari/537.36" -H "Content-Type: application/json; charset=UTF-8" -H "Accept-Language: en-US,en;q=0.8" -H "Accept: application/json, text/javascript, */*; q=0.01" -H "Referer: <a href="https://instagram.com/djeb_1/&quot;" target="_blank">https://instagram.com/djeb_1/"</a> -H "X-CSRFToken: 0179af5ebf94270a0e9e42c0e29d3676" -H "X-Requested-With: XMLHttpRequest" -H "Connection: keep-alive" --data-binary "{""q"":[{""page_id"":""2sw885"",""posts"":[[""slipstream:action"",{""description"":""followUserAttempt"",""event_name"":""action"",""extra"":""{\\""gk\\"":{},\\""source\\"":\\""profilePage\\""}"",""hostname"":""instagram.com"",""path"":""/djeb_1/"",""referer"":""https://instagram.com/p/7F5uR-F2YP/?taken-by=djeb_1"",""url"":""https://instagram.com/djeb_1/""},1441119979015,0],[""slipstream:action"",{""description"":""followUserSuccess"",""event_name"":""action"",""extra"":""{\\""gk\\"":{},\\""source\\"":\\""profilePage\\""}"",""hostname"":""instagram.com"",""path"":""/djeb_1/"",""referer"":""https://instagram.com/p/7F5uR-F2YP/?taken-by=djeb_1"",""url"":""https://instagram.com/djeb_1/""},1441119979477,0]],""trigger"":""slipstream:action""}],""ts"":1441119980877}" --compressed', $output);
  2. var_export($output);


Kod
array ( )
redeemer
1. "<a href="https://instagram.com'" target="_blank">https://instagram.com'</a>" zły nagłówek.
2. curl_setopt($c, CURLOPT_POSTFIELDS, ...) tutaj ustawiasz zły string (chyba)

Prawdopodobnie również token CSRF jest generowany per request i za każdym razem się zmienia.
Xelah
Sprawdź co dokładnie się dzieje przy wykonywaniu tego request-a.

  1. curl_setopt($c, CURLOPT_VERBOSE, true);
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.