doulu4316 2014-05-23 06:31
浏览 10
已采纳

我无法使用curl登录页面

I need to log on a page using curl to get a value from the page, but I don't get any result at all. Have I missed something or is it maybe impossible to implement?

<?
$u = "******************";
$p = "******************";

$token = file_get_contents("https://secure.izettle.com/portal/login");
preg_match('/content=\"(.*?)\" name=\"csrf-token/', $token, $t);
$authenticity_token = $t[1];
echo $authenticity_token;

$fields = array("user[email_address]" => $u, "user[password]" => $p, "authenticity_token" => $authenticity_token );
$ch = curl_init();

//Set curl options
$options = array(
    CURLOPT_URL => "https://secure.izettle.com/portal/login",
    CURLOPT_COOKIEJAR => "cookie.txt",
    CURLOPT_COOKIEFILE => "cookie.txt",
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => $fields
);

curl_setopt_array($ch, $options);
curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, "https://secure.izettle.com/portal/reports?date=2014-05-21");
$page = curl_exec($ch);

if(!curl_exec($ch)){
    die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
}

echo $page.'<br>';
  • 写回答

1条回答 默认 最新

  • douyan1613 2014-05-23 07:59
    关注

    Is it possible for split up the code and try. Not sure it will work. Assumes the second form method is "post"

    <?php
            $u = "******************";
            $p = "******************";
    
            $fields = array("user[email_address]" => $u, "user[password]" => $p);
            $ch = curl_init();
    
            //Set curl options
            $options = array(
                CURLOPT_URL => "https://secure.izettle.com/portal/login",
                CURLOPT_COOKIEJAR => "cookie.txt",
                CURLOPT_COOKIEFILE => "cookie.txt",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POST => true,
                CURLOPT_POSTFIELDS => $fields,
                CURLOPT_FOLLOWLOCATION =>true
    
    
            );
    
            curl_setopt_array($ch, $options);
            $res = curl_exec($ch);
            unset($ch);
    
            //***************************************************************************** 
    
            $qry_str = "?date=2014-05-21";
    
    
             $ch = curl_init();
    
        // Set query data here with the URL
        curl_setopt($ch, CURLOPT_URL, 'secure.izettle.com/portal/…' . $qry_str);
        curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
        curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, '3');
        $content = trim(curl_exec($ch));
        curl_close($ch);
        print_r($content);
            ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化