Reputation: 823
I'm trying to login to Walmart using CURL, the code works perfectly. However whenever i activate the CURLOPT_POST
I'm getting an error Access Denied.
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.
Upvotes: 0
Views: 6520
Reputation: 69937
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. If a 403 error is returned, the credentials are incorrect.
<?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 = '[email protected]';
$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);
Upvotes: 1