首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Curl登录+登录页面

Curl登录+登录页面
EN

Stack Overflow用户
提问于 2018-08-20 08:55:17
回答 1查看 91关注 0票数 0

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

我的问题是加载下一个页面。所以我登录了,cookie做好了,我现在需要加载下一个页面,通常在没有登录的情况下是隐藏的。

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

代码语言:javascript
复制
// 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文件以查看已登录页面的数据。

有没有人能帮帮忙

EN

回答 1

Stack Overflow用户

发布于 2018-08-21 10:44:22

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

因此,我会说:touch cookie.txtchown apache:apache ./cookie.txt ...while在测试期间,如果某些东西发生了变化,您可以在初始请求之后执行cat ./cookie.txt来验证。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51922973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档