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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探