我尝试使用 php curl 登录 Instagram,但收到类似这样的错误 {"message":"CSRF 令牌丢失或不正确","status":"fail"} 我该如何修复它?
<?php
$url = "https://www.instagram.com/api/v1/web/accounts/login/ajax/";
$ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 105.0.0.11.118 (iPhone11,8; iOS 12_3_1; en_US; en-US; scale=2.00; 828x1792; 165586599)";
$curl = curl_init();
// Set cURL options
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, true);
$response = curl_exec($curl);
// Get the response headers
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $headerSize);
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $header, $matches);
foreach ($matches[1] as $cookie) {
if (strpos($cookie, 'csrftoken=') === 0) {
list(, $csrftoken) = explode('=', $cookie, 2);
break;
}
}
curl_close($curl);
$ch = curl_init();
$headers = array(
'user-agent:' . $ua,
'x-csrftoken:' . $csrftoken
);
$payload = array(
'enc_password' => '#PWD_INSTAGRAM_BROWSER:0:0:password',
'optIntoOneTap' => false,
'queryParams' => json_encode((object)[]),
'username' => 'username'
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HEADER, $header);
$sonuc = curl_exec($ch);
echo "<pre>";
print_r($headers);
echo "</pre>";
echo $sonuc;
curl_close($ch);
?>
当我运行此代码时,输出如下所示:
Array
(
[0] => user-agent:Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 105.0.0.11.118 (iPhone11,8; iOS 12_3_1; en_US; en-US; scale=2.00; 828x1792; 165586599)
[1] => x-csrftoken:V1WIGArKhLUTW1cCUcIGiBjc9sIE0kd4
)
{"message":"CSRF token missing or incorrect","status":"fail"}
我不知道如何修复此错误,如果您能提供帮助,我将非常高兴。
你修好了吗? 请帮助我,我有同样的错误