我一直在尝试使用 curl 登录 barnesandnoble.com 移动网站到目前为止,一直没有运气。 我找回页面没有错误它将我的电子邮件默认再次进入登录页面的电子邮件输入表单框(在从打印$result返回的表单中)。
相同的代码实际上可以让我正确进入ebay通过将登录网址更改为指向eBay的登录
名唯一的区别是巴内桑贵族是 https://eBay登录 http://
另外,我相信巴恩斯网站是asp/aspx,所以我不知道这将如何以不同的方式处理饼干和_state
任何帮助将不胜感激,因为我一直在尝试为此进行调试过去 16 小时
此外,我的 cookie.txt 是可写的并且可以工作
<?php
$cookie_file_path = "C:/test/cookie.txt";
$LOGINURL = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn";
$agent = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)";
$ch = curl_init();
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
$headers[] = "Content-type: application/x-www-form-urlencoded;charset=UTF-8";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$content = curl_exec($ch);
curl_close($ch);
unset($ch);
// NAME="path_state" value="6657403">
if(stristr($content,"path_state")){
$array1 = explode('path_state" value="',$content);
$content1 = $array1[1];
$array2 = explode('">',$content1);
$content2 = $array2[0];
}
$LOGINURL = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn";
$POSTFIELDS = "d_hidPageStamp=V_3_17&hidViewMode=opSignIn&stage=signIn&previousStage=mainStage&path_state=" . $content2 . "&emailAddress=YOUREMAILHERE@gmail.com&acctPassword=YOURPASSWORD";
$reffer = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn";
$ch = curl_init();
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
$headers[] = "Content-type: application/x-www-form-urlencoded;charset=UTF-8";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec($ch);
print $result;
?>
这是我从您的代码中创建的一个工作示例。 这使用了一个函数getFormFields
我为一个类似的问题(本文底部的第一个参考)编写的函数,该问题登录到Android市场。
我认为您的脚本中可能有一些事情阻止了登录工作。 首先,您应该在帖子字符串中对 URL 参数(例如电子邮件和密码)进行编码(cURL 不会为您执行此操作)。 其次,我认为可能需要用作登录 URL 一部分的x
值。
这是一个成功登录的解决方案。 请注意,我重用了原始的 cURL 句柄。 这不是必需的,但是如果您指定keep-alive,它实际上将重用相同的连接,并且还可以使您不必一遍又一遍地指定相同的选项。
拥有 cookie 后,您可以创建一个新的 cURL 对象并指定 COOKIEFILE 和 COOKIEJAR,您将在不执行第一步的情况下登录。
<?php
// options
$EMAIL = 'you@yoursite.com';
$PASSWORD = 'yourpassword';
$cookie_file_path = "/tmp/cookies.txt";
$LOGINURL = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn";
$agent = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)";
// begin script
$ch = curl_init();
// extra headers
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
// basic curl options for all requests
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
// set first URL
curl_setopt($ch, CURLOPT_URL, $LOGINURL);
// execute session to get cookies and required form inputs
$content = curl_exec($ch);
// grab the hidden inputs from the form required to login
$fields = getFormFields($content);
$fields['emailAddress'] = $EMAIL;
$fields['acctPassword'] = $PASSWORD;
// get x value that is used in the login url
$x = '';
if (preg_match('/op'.asp'?x=('d+)/i', $content, $match)) {
$x = $match[1];
}
//$LOGINURL = "https://cart2.barnesandnoble.com/mobileacct/op.asp?stage=signIn";
$LOGINURL = "https://cart2.barnesandnoble.com/mobileacct/op.asp?x=$x";
// set postfields using what we extracted from the form
$POSTFIELDS = http_build_query($fields);
// change URL to login URL
curl_setopt($ch, CURLOPT_URL, $LOGINURL);
// set post options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
// perform login
$result = curl_exec($ch);
print $result;
function getFormFields($data)
{
if (preg_match('/(<form action="op.*?<'/form>)/is', $data, $matches)) {
$inputs = getInputs($matches[1]);
return $inputs;
} else {
die('didnt find login form');
}
}
function getInputs($form)
{
$inputs = array();
$elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches);
if ($elements > 0) {
for($i = 0; $i < $elements; $i++) {
$el = preg_replace('/'s{2,}/', ' ', $matches[1][$i]);
if (preg_match('/name=(?:["''])?([^"'''s]*)/i', $el, $name)) {
$name = $name[1];
$value = '';
if (preg_match('/value=(?:["''])?([^"'''s]*)/i', $el, $value)) {
$value = $value[1];
}
$inputs[$name] = $value;
}
}
}
return $inputs;
}
这对我有用,希望能帮助你继续前进。
以下是我拥有的其他一些可能有助于学习的 cURL 答案:
- 使用卷曲检索安卓市场我的图书馆
- 使用 curl 的多个操作
- 通过 curl 发送 XML 和标头
- 使用 PHP 和 Curl 登录谷歌,Cookie 关闭?
- 使用 PHP 和 cURL 登录的 Pinterest 不起作用