当前位置: 首页 > 面试题库 >

如何使用Curl和SSL和cookie登录

司寇瑾瑜
2023-03-14
问题内容

我一直在尝试使用curl登录barnesandnoble.com移动网站,到目前为止还没有运气。我返回的页面没有任何错误,它再次将我的电子邮件默认设置为登录页面的电子邮件输入表单框(使用print
$ result返回的表单)。

通过更改LOGINURL指向eBay的登录名,相同的代码实际上可以使我正确地进入ebay。

唯一的区别是barnesandnobles是https://,而ebay登录是http://

另外,我相信barnes网站是asp / aspx,所以我不知道该如何处理cookie和_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不会帮您完成此操作)。其次,我认为x可能需要用作登录URL一部分的值。

这是成功登录的解决方案。请注意,我重新使用了原始的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;
}

这对我有用,希望对您有所帮助。



 类似资料:
  • 问题内容: 我对PHP Curl和cookie身份验证有一些问题。 我有一个 Connector.php 文件,该文件对另一台服务器上的用户进行身份验证并返回当前用户的cookie。 问题是我想用curl认证成千上万的用户,但是一次只能认证一个用户并保存COOKIES。 Connector.php的代码是这样的: 这是认证过程: 用户输入用户名和密码: 根据先前使用登录服务保存的cookie返回有

  • 问题内容: 我有这段代码可以使用带有curl的Simple DOM Parser登录到Google。我尝试添加cookiejar文件,但无济于事。我不断收到消息: 您的浏览器的cookie功能被关闭。 请打开它。 关于如何解决这个问题的任何想法? 这是我的代码供参考: 问题答案: 这是一些经过修改的代码。 它首先请求登录页面获取初始cookie,并提取登录表单所需的值。接下来,它执行登录服务的发布

  • 我想使用客户端证书对每个目录的身份验证进行故障排除。我特别想知道服务器发送哪些可接受的客户端证书。 如何调试SSL握手,最好使用cURL?

  • 问题内容: 我正在用PHP构建一个REST Web服务客户端,此刻我正在使用curl来向该服务发出请求。 如何使用curl发出经过身份验证的请求(http基本)?我必须自己添加标题吗? 问题答案: 你要这个: Zend有一个REST客户端和zend_http_client,我敢肯定PEAR有某种包装。但是它很容易自己完成。 因此,整个请求可能如下所示:

  • 问题内容: 我正在一个springMVC项目中,其中用户身份验证基于Spring安全性。 这个想法是要拥有一个移动(android)应用程序,以便能够将某种数据发送到后端。 因此,在开始开发android之前,我决定使用cURL模拟登录表单的情况。 我们网站上的登录表单如下: 我使用以下命令: 但是我将获得登录页面,换句话说,我无法根据模拟情况通过用户身份验证。 只需注意:对于每一个请求,spri

  • 问题内容: 提交表单后,我正在从网站上抓取一些内容。问题是该脚本有时会失败,例如,每5次中有2次脚本失败。我正在使用php curl,COOKIEFILE和COOKIEJAR处理cookie。但是,当我观察到浏览器的已发送标头(从浏览器访问目标网站并使用实时http标头)和php发送的标头时,发现有很多区别。 我的浏览器发送的cookie变量比php curl多得多。我认为这种差异可能是因为jav