php curl到instagram返回奇数结果
这就是我得到的问题是你硬编码php curl到instagram返回奇数结果,php,curl,instagram,Php,Curl,Instagram,这就是我得到的问题是你硬编码接受编码的方式:gzip,deflate,这使得curl确实发送编码头,但它没有打开curl的解码功能,因此你得到了原始数据,而不需要curl为你解码 删除'Accept-Encoding:gzip,deflate',并添加curl_setopt($ch,CURLOPT_Encoding,'gzip,deflate'),curl将为您解码(前提是curl是使用gzip和deflate支持编译的)——或者更好的做法是,只需执行curl\u setopt($ch,CURL
接受编码的方式:gzip,deflate
,这使得curl确实发送编码头,但它没有打开curl的解码功能,因此你得到了原始数据,而不需要curl为你解码
删除'Accept-Encoding:gzip,deflate'
,并添加curl_setopt($ch,CURLOPT_Encoding,'gzip,deflate')
,curl将为您解码(前提是curl是使用gzip和deflate支持编译的)——或者更好的做法是,只需执行curl\u setopt($ch,CURLOPT\u ENCODING')
,而curl将自动列出所有支持的编码,这样您就不会遇到curl未使用gzip支持编译的编码问题
另一方面,您可能希望使用CURLOPT_USERAGENT,而不是手动设置user-agent头。否则,UA字符串将与此1请求一起发送,并在下一个请求时重置,而CURLOPT_USERAGENT将一直保留到curl_close($ch)
编辑:在我对这篇文章的第一次修订中,我写了CURLOPT_POSTFIELDS而不是CURLOPT_编码,对不起,修复了这个问题
编辑2:在另一个不相关的注释中,您的用户名/密码编码错误。而不是$sPost=“username=”.$usuario。“&password=”$密码代码>,做什么
$sPost=http\u build\u查询(数组('username'=>$usuario,'password'=>$password))
,否则密码或用户名中带有&或=或空的帐户将无法正常工作。应该接受@hanshenrik发布的答案。但是,如果您只是想要一个简单的解决方案,并且不存在错误,请从headers数组中删除'Accept-Encoding:gzip,deflate'
我不明白你所说的“密码或用户名中带有&or=或null的其他帐户无法正常工作”是什么意思?你能用另一种说法吗?@Pablo用密码Adam&Eve=Human
做一个账户,你就会明白我的意思了。它将尝试使用错误的密码登录,因为&
和=
编码不正确。正如http_build_查询所给出的那样,正确的编码实际上是Adam%26Eve%3DHuman
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/pruebalogininsta2.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-Encoding: gzip, deflate',
//'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
'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 = "username=".$usuario . "&password=". $password ;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);
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;