Вход в Instagram программно

Я пытаюсь программно войти в Instagram через свой собственный сайт, потому что хочу получить прямые сообщения от Instagram (для этого требуется логин, поскольку он не поддерживается в API Instagram (пока)). Но на странице входа в Instagram требуется авторизация файлов cookie.

Я продолжаю получать сообщение о том, что страница не может быть загружена и что мне может потребоваться включить файлы cookie.

Есть ли способ войти в систему программно через PHP в Instagram?

Это то, что у меня есть до сих пор.

$ch = curl_init('https://instagram.com/accounts/login/'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); $result = curl_exec($ch); preg_match_all('/^Set-Cookie:\s*([^\r\n]*)/mi', $result, $ms); $cookies = array(); foreach ($ms[1] as $m) { list($name, $value) = explode('=', $m, 2); $cookies[$name] = $value; } $ccode = substr($cookies['ccode'], 0, 2); $mid = array_shift(explode(';', $cookies['mid'])); $csfrtoken = array_shift(explode(';', $cookies['csrftoken'])); $header = array(); $header[] = 'Accept: */*'; $header[] = 'Accept-Encoding: gzip,deflate'; $header[] = 'Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4'; $header[] = 'Connection: keep-alive'; $header[] = 'Content-Length: 46'; $header[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8'; $header[] = 'X-Instagram-AJAX: 1'; $header[] = 'X-Requested-With: XMLHttpRequest'; $ch = curl_init('https://instagram.com/accounts/login/ajax/'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=*****&password=*****&intent='); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36'); curl_setopt($ch, CURLOPT_HTTPHEADER, $header); curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/test.txt'); curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/test.txt'); curl_setopt($ch, CURLOPT_COOKIESESSION, true); curl_setopt($ch, CURLOPT_COOKIE, 'mid='.$mid.'; ccode='.$ccode.'; csrftoken='.$csfrtoken.';'); curl_setopt($ch, CURLOPT_ENCODING, ''); $response = curl_exec($ch); 

Большое уважение к @Fatih Kısa за его код, очень хорошая работа. Я пробовал этот код, но пока это не работает, возможно, из-за некоторых изменений на сервере Instagram. Я сыграл 2 дня с его кодом и заставил его работать с моими небольшими изменениями. Очень важная часть этого кода заключается в том, что Instagram принимает только постформу с curl referrer, которая содержит данные cookie (csrftoken и mid). Также важная часть заключается в том, что вы должны использовать https://www.instagram.com/accounts/login/?force_classic_login , только с WWW и удалить после создания файлов cookie строки о curl-информации:

#Netscape HTTP Cookie File

#http: //curl.haxx.se/docs/http-cookies.html

# Этот файл был создан libcurl! Редактируйте на свой страх и риск.

Вот рабочий код, наслаждайтесь!

 $username = "yourname"; $password = "yourpass"; $useragent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/50.0.2661.102 Chrome/50.0.2661.102 Safari/537.36"; $cookie=$username.".txt"; @unlink(dirname(__FILE__)."/".$cookie); $url="https://www.instagram.com/accounts/login/?force_classic_login"; $ch = curl_init(); $arrSetHeaders = array( "User-Agent: $useragent", 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language: en-US,en;q=0.5', 'Accept-Encoding: deflate, br', 'Connection: keep-alive', 'cache-control: max-age=0', ); curl_setopt($ch, CURLOPT_HTTPHEADER, $arrSetHeaders); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/".$cookie); curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/".$cookie); curl_setopt($ch, CURLOPT_USERAGENT, $useragent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $page = curl_exec($ch); curl_close($ch); // try to find the actual login form if (!preg_match('/<form method="POST" id="login-form" class="adjacent".*?<\/form>/is', $page, $form)) { die('Failed to find log in form!'); } $form = $form[0]; // find the action of the login form if (!preg_match('/action="([^"]+)"/i', $form, $action)) { die('Failed to find login form url'); } $url2 = $action[1]; // this is our new post url // find all hidden fields which we need to send with our login, this includes security tokens $count = preg_match_all('/<input type="hidden"\s*name="([^"]*)"\s*value="([^"]*)"/i', $form, $hiddenFields); $postFields = array(); // turn the hidden fields into an array for ($i = 0; $i < $count; ++$i) { $postFields[$hiddenFields[1][$i]] = $hiddenFields[2][$i]; } // add our login values $postFields['username'] = $username; $postFields['password'] = $password; $post = ''; // convert to string, this won't work as an array, form will not accept multipart/form-data, only application/x-www-form-urlencoded foreach($postFields as $key => $value) { $post .= $key . '=' . urlencode($value) . '&'; } $post = substr($post, 0, -1); preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $page, $matches); $cookieFileContent = ''; foreach($matches[1] as $item) { $cookieFileContent .= "$item; "; } $cookieFileContent = rtrim($cookieFileContent, '; '); $cookieFileContent = str_replace('sessionid=; ', '', $cookieFileContent); $oldContent = file_get_contents(dirname(__FILE__)."/".$cookie); $oldContArr = explode("\n", $oldContent); if(count($oldContArr)) { foreach($oldContArr as $k => $line) { if(strstr($line, '# ')) { unset($oldContArr[$k]); } } $newContent = implode("\n", $oldContArr); $newContent = trim($newContent, "\n"); file_put_contents( dirname(__FILE__)."/".$cookie, $newContent ); } $arrSetHeaders = array( 'origin: https://www.instagram.com', 'authority: www.instagram.com', 'upgrade-insecure-requests: 1', 'Host: www.instagram.com', "User-Agent: $useragent", 'content-type: application/x-www-form-urlencoded', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language: en-US,en;q=0.5', 'Accept-Encoding: deflate, br', "Referer: $url", "Cookie: $cookieFileContent", 'Connection: keep-alive', 'cache-control: max-age=0', ); $ch = curl_init(); curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/".$cookie); curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/".$cookie); curl_setopt($ch, CURLOPT_USERAGENT, $useragent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $arrSetHeaders); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); sleep(5); $page = curl_exec($ch); preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $page, $matches); $cookies = array(); foreach($matches[1] as $item) { parse_str($item, $cookie1); $cookies = array_merge($cookies, $cookie1); } var_dump($page); curl_close($ch); с $username = "yourname"; $password = "yourpass"; $useragent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/50.0.2661.102 Chrome/50.0.2661.102 Safari/537.36"; $cookie=$username.".txt"; @unlink(dirname(__FILE__)."/".$cookie); $url="https://www.instagram.com/accounts/login/?force_classic_login"; $ch = curl_init(); $arrSetHeaders = array( "User-Agent: $useragent", 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language: en-US,en;q=0.5', 'Accept-Encoding: deflate, br', 'Connection: keep-alive', 'cache-control: max-age=0', ); curl_setopt($ch, CURLOPT_HTTPHEADER, $arrSetHeaders); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/".$cookie); curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/".$cookie); curl_setopt($ch, CURLOPT_USERAGENT, $useragent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $page = curl_exec($ch); curl_close($ch); // try to find the actual login form if (!preg_match('/<form method="POST" id="login-form" class="adjacent".*?<\/form>/is', $page, $form)) { die('Failed to find log in form!'); } $form = $form[0]; // find the action of the login form if (!preg_match('/action="([^"]+)"/i', $form, $action)) { die('Failed to find login form url'); } $url2 = $action[1]; // this is our new post url // find all hidden fields which we need to send with our login, this includes security tokens $count = preg_match_all('/<input type="hidden"\s*name="([^"]*)"\s*value="([^"]*)"/i', $form, $hiddenFields); $postFields = array(); // turn the hidden fields into an array for ($i = 0; $i < $count; ++$i) { $postFields[$hiddenFields[1][$i]] = $hiddenFields[2][$i]; } // add our login values $postFields['username'] = $username; $postFields['password'] = $password; $post = ''; // convert to string, this won't work as an array, form will not accept multipart/form-data, only application/x-www-form-urlencoded foreach($postFields as $key => $value) { $post .= $key . '=' . urlencode($value) . '&'; } $post = substr($post, 0, -1); preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $page, $matches); $cookieFileContent = ''; foreach($matches[1] as $item) { $cookieFileContent .= "$item; "; } $cookieFileContent = rtrim($cookieFileContent, '; '); $cookieFileContent = str_replace('sessionid=; ', '', $cookieFileContent); $oldContent = file_get_contents(dirname(__FILE__)."/".$cookie); $oldContArr = explode("\n", $oldContent); if(count($oldContArr)) { foreach($oldContArr as $k => $line) { if(strstr($line, '# ')) { unset($oldContArr[$k]); } } $newContent = implode("\n", $oldContArr); $newContent = trim($newContent, "\n"); file_put_contents( dirname(__FILE__)."/".$cookie, $newContent ); } $arrSetHeaders = array( 'origin: https://www.instagram.com', 'authority: www.instagram.com', 'upgrade-insecure-requests: 1', 'Host: www.instagram.com', "User-Agent: $useragent", 'content-type: application/x-www-form-urlencoded', 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language: en-US,en;q=0.5', 'Accept-Encoding: deflate, br', "Referer: $url", "Cookie: $cookieFileContent", 'Connection: keep-alive', 'cache-control: max-age=0', ); $ch = curl_init(); curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/".$cookie); curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/".$cookie); curl_setopt($ch, CURLOPT_USERAGENT, $useragent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, $arrSetHeaders); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_REFERER, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); sleep(5); $page = curl_exec($ch); preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $page, $matches); $cookies = array(); foreach($matches[1] as $item) { parse_str($item, $cookie1); $cookies = array_merge($cookies, $cookie1); } var_dump($page); curl_close($ch); 

Похоже, что вы направились в правильном направлении, в вашем примере кажется, что вы получили аутентифицированный ответ, но последующий запрос на аутентифицированную страницу не работает должным образом.

Я подозреваю, что instagram активно предотвращает это, выполняя проверку с помощью вызова Ajax или чего-то подобного.

В качестве альтернативы вы можете посмотреть что-то вроде zombie.js .

Это безголовый виртуальный браузер, где вы можете посещать страницы и взаимодействовать с элементами на нем, но вы не можете их просматривать.

Простой пример использования zombie.js выглядит следующим образом:

 var Browser = require('zombie'); var browser = Browser.create(); browser.visit('https://instagram.com/', function() { browser.wait(10000, function(){ browser.fill('input[name="username"]', 'myusername'); browser.fill('input[type="password"]', 'mypasswd'); browser.pressButton('Log in', function() { browser.visit('https://instagram.com/accounts/edit/', function() { console.log(browser.html()); }); }); }); }); 

Надеюсь, поможет.

Вы можете использовать классическую форму входа с параметром «force_classic_login».

 <?php $username = "username"; $password = "password"; $useragent = "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13"; $cookie=$username.".txt"; $url="https://instagram.com/accounts/login/?force_classic_login"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/".$cookie); curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/".$cookie); curl_setopt($ch, CURLOPT_USERAGENT, $useragent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); $page = curl_exec($ch); // try to find the actual login form if (!preg_match('/<form method="POST" id="login-form" class="adjacent".*?<\/form>/is', $page, $form)) { throw Instagram_Manager('Failed to find log in form!'); } $form = $form[0]; // find the action of the login form if (!preg_match('/action="([^"]+)"/i', $form, $action)) { throw Instagram_Manager('Failed to find login form url'); } $url2 = $action[1]; // this is our new post url // find all hidden fields which we need to send with our login, this includes security tokens $count = preg_match_all('/<input type="hidden"\s*name="([^"]*)"\s*value="([^"]*)"/i', $form, $hiddenFields); $postFields = array(); // turn the hidden fields into an array for ($i = 0; $i < $count; ++$i) { $postFields[$hiddenFields[1][$i]] = $hiddenFields[2][$i]; } // add our login values $postFields['username'] = $username; $postFields['password'] = $password; $post = ''; // convert to string, this won't work as an array, form will not accept multipart/form-data, only application/x-www-form-urlencoded foreach($postFields as $key => $value) { $post .= $key . '=' . urlencode($value) . '&'; } $post = substr($post, 0, -1); // set additional curl options using our previous options curl_setopt($ch, CURLOPT_URL, "https://instagram.com/".$url2); curl_setopt($ch, CURLOPT_REFERER, $url); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $page = curl_exec($ch); // connect to profile edit page $url="https://instagram.com/accounts/edit/"; curl_setopt($ch, CURLOPT_URL, $url); echo curl_exec($ch); ?>