2012-08-06 63 views
0

我正在嘗試編寫一個腳本,該腳本可以獲取本月的數據使用情況,並瀏覽我使用cURL的url。不幸的是,我並不熟悉它。這是到目前爲止我的代碼:爲什麼我無法提交表單? PHP 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" ; 

?> 

出於某種原因,雖然,這是行不通的。我嘗試了很多變化,但輸出總是一樣的。它給了我原來的頁面。有誰知道我做錯了什麼?幾個小時以來,我一直在嘲笑我。

謝謝!

回答

2

我不是100%確定的,但在查看url的源代碼之後,我覺得你應該縮小表單的行爲,而不是表單所在的頁面。因此,您正在模擬提交,因此您應該嘗試將表單內容提交給表單的操作。

<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' 
) ; 
+0

嘿,幾乎工作! 現在它說我有錯誤的MAC地址,即使我已經檢查了幾次,它在實際網站上工作。任何想法是什麼造成這個? 對不起,我絕對贊成這一點,但我的代表現在太低了。 編輯:其實,它說我沒有輸入正確的地址,即使我沒有發佈任何內容。 – KriptSkitty 2012-08-06 20:35:50

+0

是的,在提交表單時會運行一些JavaScript。看看我的修改答案, – 2012-08-06 20:43:40

+0

*錯誤。 FixAndSubmit函數?這會改變什麼嗎? – KriptSkitty 2012-08-06 20:47:17