D
D
dllweb2016-10-24 15:27:08
PHP
dllweb, 2016-10-24 15:27:08

Authentication on instagram via curl, why doesn't it work?

Hello everyone, and here I am again with the same question, I still can’t log in to instagram

function getCookie($str){
  
    
    // Разбор куков
    $pattern_exec_cookie = "~Set-Cookie: (.*?);~ui";
    preg_match_all($pattern_exec_cookie, $str, $c);
    $cookie = "";
    foreach($c[0] as $rc){
      $cookie .= str_replace("Set-Cookie: ", "", $rc);
    }

    $cookie_array = explode(";", $cookie);
    $cookie_str = "";
    foreach($cookie_array as $ca){
      $ex = explode("=", $ca);
        if(isset($ex[1]) and !empty($ex[1]) and $ex[1] != '""'){
          $cookie_str .= $ex[0]."=".$ex[1]."; ";
        }
      }
    return trim($cookie_str);
}

$login = "login";
$passw = "Jyv6ddacca";

$user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13 ( .NET CLR 3.5.30729)';
$url_auth = "https://www.instagram.com/accounts/login";
$url_auth_ajax = "https://www.instagram.com/accounts/login/ajax/";

      $ch = curl_init();
      curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_TIMEOUT, 30);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_HEADER, true);
      curl_setopt($ch, CURLINFO_HEADER_OUT, true);
      curl_setopt($ch, CURLOPT_URL, $url_auth);

      $curl_exec = curl_exec($ch);
      $bot_cookie = getCookie($curl_exec);

      var_dump($bot_cookie);
      
      curl_close($ch);
      // file_put_contents("insta_cookie.txt", trim($cookie_str)."\n", FILE_APPEND);	
  

      $data = array(
      "username"=>$login,
      "password"=>$passw
      );
      
      $pattern = "~csrftoken=.*?;~ui";
      preg_match($pattern, $bot_cookie, $token);
      $ctoken = explode("=", str_replace(";", "", $token[0]));
      $csrf = $ctoken[1];
      
      
      $ch1 = curl_init();
      curl_setopt($ch1, CURLOPT_USERAGENT, $user_agent);
      curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
      curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);
      curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch1, CURLOPT_NOBODY, 1);	
      curl_setopt($ch1, CURLOPT_TIMEOUT, 30);
      curl_setopt($ch1, CURLOPT_POST, true);
      curl_setopt($ch1, CURLOPT_POSTFIELDS, $data);
      curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch1, CURLOPT_HEADER, true);
      curl_setopt($ch1, CURLINFO_HEADER_OUT, true);
      curl_setopt($ch1, CURLOPT_HTTPHEADER, array(
      'authority: www.instagram.com',
      'method: POST',
      'path: /accounts/login/ajax/', 
      'scheme: https',
      'content-length: 44',
      'accept: */*',
      'accept-language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4,und;q=0.2',
      'content-type: application/x-www-form-urlencoded',
      'cookie: s_network=; ig_pr=1; ig_vw=1920; ds_user_id=2008956070; '.$bot_cookie,
      'origin: https://www.instagram.com',
      'referer: https://www.instagram.com/',
      'user-agent: '.$user_agent,
      'x-csrftoken: '.$csrf,
      'x-instagram-ajax: 1',
      'x-requested-with: XMLHttpRequest')
      );
      curl_setopt($ch1, CURLOPT_URL, $url_auth_ajax);

      
      $result_auth = curl_exec($ch1);
      file_put_contents("insta_cookie.txt", $result_auth."\n", FILE_APPEND);
      $info = curl_getinfo($ch1);
      var_dump($info);
      curl_close($ch1);

Result
HTTP/1.1 200 OK
Strict-Transport-Security: max-age=86400
Content-Language: ru
Expires: Sat, 01 Jan 2000 00:00:00 GMT
Vary: Cookie, Accept-Language
Pragma: no-cache
Cache-Control: private, no-cache, no-store, must-revalidate
Date: Mon, 24 Oct 2016 12:26:36 GMT
Content-Type: application/json
Set-Cookie: csrftoken=uPBpNRhUyeVowYioX3C8dgIdpynQUShn; expires=Mon, 23-Oct-2017 12:26:36 GMT; Max-Age=31449600; Path=/; secure
Connection: keep-alive
Content-Length: 52

{"status": "ok", "authenticated": false, "user": ""}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nomoo, 2016-10-25
@nomoo

Everything seems to be here: stackoverflow.com/a/37711728

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question