php curl:返回登录页面

时间:2014-11-14 14:18:48

标签: php curl

我一直在反对这一个。 我正在尝试登录页面并使用php和curl但有两个问题,我怀疑它们是相关的。

  1. Cookie文件仍为空(可能是因为登录失败?)

  2. 尝试登录页面后返回登录页面,错误:登录时间超出。

  3. 首先,我转到页面提取时间敏感变量:

    // $ urlString =“https://domain.com/portal/gas.nsf/Tool

    OpenAgent&action=TNQuery&ordernumber=".$order."&tn=".$tn."&User=".$username."&lang=en_US";
        $loginUrl = 'https://domain.com/login';
        $randnum = rand(5, 15);
        $ckfile = tempnam ($cwd, "cookie.txt");
    
    
    
    $ch = curl_init($loginUrl);
    
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile); //write
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    
    $result = curl_exec ($ch);
    curl_close($ch);
    //var_dump($result);
    
    $dom = new DOMDocument();
    @$dom->loadHTML($result);
    $xml = simplexml_import_dom($dom);
    
    // extract time sensitive variable from the login page
    $genData = $xml->xpath("//input[@name='pageGenTime']");
    $epoch = $genData[0][value];
    

    要发布的数据:

    $postdata = array(
        'HiddenURI' => 'https://domain.com/loadPortal.htm',
        'AUTHMETHOD' => 'UserPassword',
        'pageGenTime' => $epoch,
        'LOCALE' => "en_US",
        'usr_name' => $username,
        'usr_password' => $password
    );
    

    cURL处理程序:

    $ch = curl_init();
    // Now login to the portal
    curl_setopt_array($ch, array(
        CURLOPT_URL => $loginUrl,
        CURLOPT_USERAGENT => $agent,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $postdata,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_COOKIESESSION => true,
        CURLOPT_COOKIEFILE => 'cookie.txt',
        CURLOPT_COOKIEJAR => 'cookie.txt'
    ));
    curl_exec($ch);
    
    $exec = curl_exec($ch);
    $info = curl_getinfo($ch); 
    curl_close ($ch);
    

    print_r返回:

    Array
    (
        [url] => https://domain.com/login
        [content_type] => text/html;charset=iso-8859-1
        [http_code] => 200
        [header_size] => 253
        [request_size] => 304
        [filetime] => -1
        [ssl_verify_result] => 0
        [redirect_count] => 0
        [total_time] => 0.08823
        [namelookup_time] => 2.7E-5
        [connect_time] => 3.1E-5
        [pretransfer_time] => 8.0E-5
        [size_upload] => 744
        [size_download] => 9992
        [speed_download] => 113249
        [speed_upload] => 8432
        [download_content_length] => 9992
        [upload_content_length] => 744
        [starttransfer_time] => 0.026516
        [redirect_time] => 0
        [certinfo] => Array
            (
            )
    
        [primary_ip] => 206.47.249.9
        [primary_port] => 443
        [local_ip] => [concealed]
        [local_port] => 41349
        [redirect_url] => 
    )
    

    很好,HTTP代码200 ..但它实际上并没有登录。 我究竟做错了什么? POST数据是正确的。

    感谢您宝贵的时间!

1 个答案:

答案 0 :(得分:0)

弄清楚问题,它不喜欢的是:CURLOPT_COOKIEFILE => 'cookie.txt', CURLOPT_COOKIEJAR => 'cookie.txt'更改为CURLOPT_COOKIEJAR => $randnum.'cookie.txt', CURLOPT_COOKIEFILE => $randnum.'cookie.txt'

现在正在编写cookie并且登录成功。 var_dump($ exec)确实提供了我需要的额外调试。谢谢@ mike-brant!