当前位置:  开发笔记 > 编程语言 > 正文

Instagram以编程方式登录

如何解决《Instagram以编程方式登录》经验,为你挑选了1个好方法。



1> Vaha..:

我非常尊重@FatihKısa的代码,非常好.我已经尝试过这个代码,但是现在它可能不起作用,因为有一些Instagram服务器端的变化.我用他的代码玩了2天,并强迫它与我的小改动一起工作.这段代码非常重要的一部分是Instagram只接受带有curl referrer的帖子表格,其中包含cookies数据(csrftoken和mid).同样重要的是,您必须使用https://www.instagram.com/accounts/login/?force_classic_login,仅使用WWW并在cookie创建后删除有关curl info的字符串:

#Netscape HTTP Cookie文件

#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('/
/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('/ $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);

推荐阅读
pan2502851807
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有