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 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP