CURL帖子拒绝访问 [英] Access Denied for CURL Post

查看:941
本文介绍了CURL帖子拒绝访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用CURL登录Walmart,代码完美地工作。但是每当我激活 CURLOPT_POST 时,我得到一个错误访问被拒绝。

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.

这是我试过的代码:

<?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.

推荐答案

这是一个工作示例。

您需要首先请求主页建立基本Cookie,然后将凭据发布到登录页面。如果返回403错误,则凭据不正确。

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 = '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);

这篇关于CURL帖子拒绝访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆