为什么我不能提交表格? PHP cURL

时间:2012-08-06 20:18:08

标签: php post curl

我正在尝试编写一个脚本来获取本月的数据使用情况,并导航我正在使用cURL的网址。不幸的是,我不熟悉它。到目前为止,这是我的代码:

<?php

$url = 'https://apps.nwtel.ca/cable_usage/login.jsp' ;
$id = "------------" ;

$ch = curl_init() ;
curl_setopt( $ch, CURLOPT_URL, "$url" ) ;
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ) ;
curl_setopt( $ch, CURLOPT_POST, true ) ;
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ) ;
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true ) ;

$data = array(
    'MAC' => "$id",
    'submit_btn' => 'submit'
) ;

curl_setopt( $ch, CURLOPT_POSTFIELDS, $data ) ;
$output = curl_exec( $ch ) ;
curl_close( $ch ) ;

echo "$output\n" ;

?>

出于某种原因,这不起作用。我尝试了很多变化,但输出总是一样的。它给了我原始页面。有谁知道我做错了什么?我已经打了几个小时了。

谢谢!

1 个答案:

答案 0 :(得分:2)

我不是百分百肯定,但是在查看了该网址的来源之后,我觉得你应该卷曲表单的动作,而不是表单所在的页面。你正在模拟提交,因此你应该尝试将表单内容提交给表单的操作。

<form method="post" action="j_security_check" id="usage_login">
    <input type="hidden" name="j_target_url" value="secured/index.jsp" />
    <div class="form_input">
    <label for="MAC">HFC or CM MAC Address:</label>
    <input type="Text" name="MAC" id="MAC" onKeyPress="onKeyPress(this.form)" />
    <a href="#mac_num_help" title="Your HFC or CM MAC Address is located on the sticker on the bottom of your modem" class="help">MAC Address Help</a>
    <input type="hidden" name="j_username" id="j_username" />
    </div>
    <div class="form_input">
    <input type="hidden" name="j_password" id="j_password" maxlength="6" size="7" value="123456" />
    </div>
    <div class="form_input">
    <label for="submit_btn">&nbsp;</label>
    <input type="submit" name="submit_btn" id="submit_btn" value="Submit" onclick="fixAndSubmit(this.form)" />
    </div>
</form>

尝试更改:

$url = 'https://apps.nwtel.ca/cable_usage/login.jsp' ;

$url = 'https://apps.nwtel.ca/cable_usage/j_security_check' ;

也改变了

$data = array(
    'MAC' => "$id",
    'submit_btn' => 'submit'
) ;

$data = array(
    'MAC' => "$id",
    'submit_btn' => 'submit',
    'j_password' => '123456',
    'j_username' => '3D3D3D3D3D3D' //should be your MAC address all capitalize without using special characters
    'j_target_url' => 'secured/index.jsp'
) ;