php curl登录instagram进入重定向循环 [英] php curl login to instagram goes into redirection loop
问题描述
我使用curl和php登录instagram,它进入重定向循环,在该循环中回显与此类似的文本,每次回显之间几乎没有变化
I'm logging into instagram with curl and php and it goes in a redirection loop where a text similar to this is echoed with few variations between each echo
> HTTP/1.1 301 Moved Permanently Location: https://www.instagram.com/
> Content-Type: text/plain Server: proxygen Date: Mon, 16 Jan 2017
> 12:53:26 GMT Connection: keep-alive Content-Length: 0
>
> HTTP/1.1 200 OK Content-Type: text/html Vary: Cookie, Accept-Language,
> Accept-Encoding Content-Language: en Pragma: no-cache Expires: Sat, 01
> Jan 2000 00:00:00 GMT Strict-Transport-Security: max-age=86400 Date:
> Mon, 16 Jan 2017 12:53:27 GMT X-Frame-Options: SAMEORIGIN
> Content-Encoding: gzip Cache-Control: private, no-cache, no-store,
> must-revalidate Set-Cookie: sessionid=; expires=Thu, 01-Jan-1970
> 00:00:00 GMT; Max-Age=0; Path=/; HttpOnly; Domain=instagram.com
> Set-Cookie: csrftoken=uWHWfgmVVhdROoG3HsyIevXMq4mcEGVU; expires=Mon,
> 15-Jan-2018 12:53:27 GMT; Max-Age=31449600; Path=/; Secure Connection:
> keep-alive Content-Length: 3373
我正在使用的代码是
<?php
include_once('simple_html_dom.php');
$usuario = "username";
$password = "password";
$url = 'https://www.instagram.com/';
$url_login = 'https://www.instagram.com/accounts/login/ajax/';
$user_agent = array("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 ",
"(KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36");
$ch = curl_init();
$headers = [
'Accept-Encoding: gzip, deflate',
'Accept-Language: en-US;q=0.6,en;q=0.4',
'Connection: keep-alive',
'Content-Length: 0',
'Host: www.instagram.com',
'Origin: https://www.instagram.com',
'Referer: https://www.instagram.com/',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36',
'X-Instagram-AJAX: 1',
'X-Requested-With: XMLHttpRequest'
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie/pruebalogininsta3.txt");
curl_setopt($ch, CURLOPT_REFERER, $sTarget);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
$html = curl_exec($ch);
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $html, $matches);
$cookies = array();
foreach($matches[1] as $item) {
parse_str($item, $cookie);
$cookies = array_merge($cookies, $cookie);
}
$headers = [
'Accept-Language: en-US;q=0.6,en;q=0.4',
'Connection: keep-alive',
'Content-Length: 0',
'Host: www.instagram.com',
'Origin: https://www.instagram.com',
'Referer: https://www.instagram.com/',
'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36',
'X-Instagram-AJAX: 1',
'X-Requested-With: XMLHttpRequest'
];
$cadena_agregar_vector = 'X-CSRFToken:'. $cookies["csrftoken"];
$headers[] = $cadena_agregar_vector ;
$sPost=http_build_query(array('username'=>$usuario,'password'=>$password));
# Creo que falta agregar la variable POST para que mande un POST y no un GET
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url_login);
$html2 = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "http://www.instagram.com/");
$html4 = curl_exec($ch);
echo $html4;
?>
推荐答案
您犯了一些错误,
1:手动添加'Accept-Encoding:gzip,deflate',
标头。如果服务器选择实际使用gzip / deflate,则响应主体将看起来全是乱码(二进制压缩数据),如果要在curl中使用压缩,请改用 CURLOPT_ENCODING
,卷曲会为您解压缩。 (至少您在第二次调用中正确完成了此操作)
1: you add the 'Accept-Encoding: gzip, deflate',
header manually. should the server chose to actually use gzip/deflate, the response body would look all garbled (binary compressed data), if you want to use compression in curl, use CURLOPT_ENCODING
instead, and curl will decompress it for you. (at least you do it correctly in the 2nd call)
2:添加推荐人:https://www.instagram.com/
,只是稍后用 curl_setopt($ ch,CURLOPT_REFERER,$ sTarget);
覆盖它,使自定义标头被忽略,并且$ sTarget似乎被覆盖
2: you add the Referer: https://www.instagram.com/
, only to overwrite it moments later with curl_setopt($ch, CURLOPT_REFERER, $sTarget);
, making the custom header ignored, AND $sTarget seems to be an undefined variable.
3:您在http标头中使用大写字母。据我所知,登录协议在任何标题中均不使用大写字母。 (例如,您将其命名为 X-Instagram-AJAX:1
,但登录页面将其命名为 x-instagram-ajax:1
)
3: you use captial letters in your http headers. the login protocol does not use capital letters in any headers, as far as i can see. (for example, you call it X-Instagram-AJAX: 1
, but the login page calls it x-instagram-ajax: 1
)
使用来自> https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php ,
这是一个有效的示例代码:
here's a working example code:
<?php
declare(strict_types = 1);
require_once('hhb_.inc.php');
$hc = new hhb_curl ();
$hc->_setComfortableOptions ();
$username = 'nigeriansdddd';
$password = '3fc2p4xy049q3om@my10minutemail.com';
// get a cookie session and login page etc
$hc->exec ( 'https://www.instagram.com/' );
// hhb_var_dump($hc->getStdErr(),$hc->getResponseBody());
$html = $hc->getResponseBody ();
$token = getCsrfToken ( $html );
$hc->setopt_array ( array (
CURLOPT_URL => 'https://www.instagram.com/accounts/login/ajax/',
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array (
'x-csrftoken: ' . $token,
'x-instagram-ajax: 1',
'x-requested-with: XMLHttpRequest',
'accept-language: en-US,en;q=0.8,nb;q=0.6',
'origin: https://www.instagram.com',
'referer: https://www.instagram.com/'
)
,
CURLOPT_POSTFIELDS => http_build_query ( array (
'username' => $username,
'password' => $password
) )
) );
$hc->exec ();
// hhb_var_dump ( $hc->getStdErr (), $hc->getResponseBody () );
$jsonRAW = $hc->getResponseBody ();
$json = json_decode ( $jsonRAW, true );
if (! is_array ( $json ) || $json ['status'] !== 'ok' || $json ['authenticated'] !== true) {
throw new RuntimeException ( 'failed to login. last curl request: ' . $hc->getStdErr () . $hc->getResponseBody () );
}
$hc->exec ( 'https://www.instagram.com/' );
$html = $hc->getResponseBody ();
if (false === strpos ( $html, $username )) {
throw new RuntimeException ( 'failed to confirm login. last curl request: ' . $hc->getStdErr () . $hc->getResponseBody () );
}
hhb_var_dump ( $hc->getStdErr (), $hc->getResponseBody () );
function getCsrfToken(string $html): string {
$matches = array ();
$rexret = preg_match ( '/\"csrf_token\"\s*\:\s*\"([^\"]*)/', $html, $matches );
if ($rexret !== 1) {
throw new RuntimeException ( 'failed to find the csrf token!' );
}
// hhb_var_dump($rexret,$matches);
$csrftoken = $matches [1];
return $csrftoken;
}
(请注意:帐户, nigeriansdddd
/ 3fc2p4xy049q3om@my10minutemail.com
只是一个虚拟的一次性帐户,我不在乎该帐户是否受到威胁,这显然是在我将凭据发布到此处时发生的)
(note: the account, nigeriansdddd
/ 3fc2p4xy049q3om@my10minutemail.com
is just a dummy throwaway account, i don't care if the account is compromised, which obviously happens when i post the credentials here)
这篇关于php curl登录instagram进入重定向循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!