首页 > 解决方案 > 卷曲登录+登录页面

问题描述

我得到了这个代码,一切正常,cookie被添加到txt文件中,登录发生,登录后显示登录页面一切正常。

我的问题是加载下一页。所以我登录了,cookie 被制作了,我现在需要加载通常隐藏但没有登录的下一页。

我不知道如何:(这是我的代码:

// options
$EMAIL            = '####################';
$PASSWORD         = '####################';
$cookie_file_path = "cookie.txt";
$LOGINURL         = "####################"; 
$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['loginid'] = $EMAIL;
$fields['passwd'] = $PASSWORD;

// get x value that is used in the login url
$x = '';
if (preg_match('/myauthenticate\.jsp\?x=(\d+)/i', $content, $match)) {
    $x = $match[1];
}

$LOGINURL   = "####################/myauthenticate.jsp?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);

echo $result;

function getFormFields($data)
{
        $inputs = getInputs($matches[1]);
        return $inputs;
}

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;
}

所以这一切都有效,但现在我需要关闭该脚本,并开始读取 cookie 文件的下一页加载以查看登录页面的数据。

有人可以帮忙吗:(

标签: phpcurl

解决方案


一个_JAR选项用于在接收 cookie 时存储它们,另一个_FILE选项用于读取 cookie,以便将它们发回。确保apache用户对该文件具有读/写访问权限,例如。作为它的所有者(这似乎很可能是这里的罪魁祸首)。

因此我会说:touch cookie.txt并且chown apache:apache ./cookie.txt......在测试期间cat ./cookie.txt,如果有什么变化,你可以在初始请求之后进行验证。


推荐阅读