[英]Access Denied for CURL Post
I'm trying to login to Walmart using CURL, the code works perfectly. 我正在尝试使用CURL登录到Walmart,该代码可以正常运行。 However whenever i activate the
CURLOPT_POST
I'm getting an error Access Denied. 但是,每当我激活
CURLOPT_POST
我都会收到错误的访问被拒绝。
This is the code that I tried: 这是我尝试的代码:
<?php
$user_agent = "Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20140319 Firefox/24.0 Iceweasel/24.4.0";
$curl_crack = curl_init();
CURL_SETOPT($curl_crack,CURLOPT_URL,"https://www.walmart.com/account/login");
CURL_SETOPT($curl_crack,CURLOPT_USERAGENT,$user_agent);
//CURL_SETOPT($curl_crack,CURLOPT_PROXY,"183.78.169.60:37899");
//CURL_SETOPT($curl_crack,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
CURL_SETOPT($curl_crack,CURLOPT_POST,True);
CURL_SETOPT($curl_crack,CURLOPT_POSTFIELDS,"login-username=test&login-password=test");
CURL_SETOPT($curl_crack,CURLOPT_RETURNTRANSFER,True);
CURL_SETOPT($curl_crack,CURLOPT_FOLLOWLOCATION,True);
CURL_SETOPT($curl_crack,CURLOPT_COOKIEFILE,"cookie.txt"); //Put the full path of the cookie file if you want it to write on it
CURL_SETOPT($curl_crack,CURLOPT_COOKIEJAR,"cookie.txt"); //Put the full path of the cookie file if you want it to write on it
CURL_SETOPT($curl_crack,CURLOPT_TIMEOUT,30);
echo $exec = curl_exec($curl_crack);
?>
I don't know what I'm doing wrong, please help me. 我不知道我在做什么错,请帮助我。
Here is a working example. 这是一个工作示例。
You need to first request the home page to establish base cookies, and then post the credentials to the login page. 您需要首先请求主页建立基本cookie,然后将凭据发布到登录页面。 If a 403 error is returned, the credentials are incorrect.
如果返回403错误,则凭据不正确。
<?php
$base_url = 'https://www.walmart.com/';
$login_url = 'https://www.walmart.com/account/api/signin';
$user_agent = "Mozilla/5.0 (X11; Linux i686; rv:24.0) Gecko/20140319 Firefox/24.0 Iceweasel/24.4.0";
$username = 'user@example.com';
$password = 'password';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $base_url);
curl_setopt($ch, CURLOPT_USERAGENT,$user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,True);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
curl_setopt($ch, CURLOPT_COOKIEFILE, '');
curl_setopt($ch, CURLOPT_TIMEOUT,30);
//curl_setopt($ch, CURLOPT_VERBOSE, 1);
//curl_setopt($ch, CURLOPT_STDERR, fopen('php://output', 'r'));
//curl_setopt($ch,CURLOPT_PROXY,"183.78.169.60:37899");
//curl_setopt($ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
$resp = curl_exec($ch);
$headers = array(
'X-Requested-With: XMLHttpRequest',
);
$post = array(
'username' => $username,
'password' => $password,
'login-captcha-value' => '',
'sensor-data' => '',
'clearPCID' => '1',
);
curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$exec = curl_exec($ch);
$info = curl_getinfo($ch);
if ($info['http_code'] != 200) {
echo "Login failed! HTTP code {$info['http_code']}<br>\n";
var_dump($exec);
exit;
}
echo "Login successful!<br>\n";
// you are now logged in, use $ch to request pages as the logged in user
$url = 'https://www.walmart.com/account';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 0);
$account = curl_exec($ch);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.