2013-04-10 55 views
1

我想使用HTML表單將值發佈到頁面並獲取該頁面的源代碼。POST值和獲取頁面源

我可以用$html = file_get_html('http://www.exam.com/results/');

源,但得到的烴源之前,我需要的第一篇文章的價值該網頁,然後搶來源。

可以說,其他頁面,我應該張貼值爲http://www.exam.com/results/

,我創造了價值提交表單。

<form method="post" action="http://www.exam.com/results/"> 

<input type="hidden" value="900358967" name="eid"> 
<input name="confirm" type="submit" value="Enter" > 

</form> 

所以將張貼在該網頁的價值和展示數據,但如何抓住與數據頁面的源代碼?

這可能嗎?我到處搜索,試圖抓住它,但我不知道如何抓取後的價值。

+0

隨着頁面源你是什麼意思?你能解釋更多嗎? – 2013-04-10 09:59:45

+0

我認爲你需要使用cURL來達到這個目的。使用cURL將值發佈到表單並返回結果頁面源可能工作。 http://php.net/manual/en/book.curl.php – viclim 2013-04-10 09:59:53

+0

如果我明白這個問題,你想獲得POST發送頁面的源代碼? – Brainfeeder 2013-04-10 10:00:55

回答

1

我認爲你必須嘗試用CURL

//set POST variables 
$url = 'http://www.exam.com/results/'; 
$fields = array(
'eid' => urlencode('900358967') 
); 
$fields_string = ""; 

//url-ify the data for the POST 
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; } 
rtrim($fields_string, '&'); 

//open connection 
$ch = curl_init(); 

//set the url, number of POST vars, POST data 
curl_setopt($ch,CURLOPT_URL, $url); 
curl_setopt($ch,CURLOPT_POST, count($fields)); 
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); 

//execute post 
$result = curl_exec($ch); 
+0

嗨,它給出了一個錯誤 - 注意:未定義的變量:fields_string在C:\ wamp \ www \ results \ 1 \ 1.php – naveencgr8 2013-04-10 10:16:12

+0

@ naveencgr8忘記初始化它現在檢查.. :) – 2013-04-10 10:17:50

+0

似乎它的工作,但它不顯示結果,它顯示正常的文本框輸入檢查編號..意味着值不會像我們預期的那樣。 – naveencgr8 2013-04-10 10:27:12

1

我注意到你有標記與JavaScript的問題是相同的。所以這是一個可以爲你使用jQuery的解決方案。

$.post('http://www.exam.com/results/',$('form').serialize(), function(data) { 
    alert(data); 
}); 

如果URL只返回html,則html將位於函數返回的數據var中。

+0

非常感謝,但這沒有爲我工作,我不熟悉java的那麼多,不知何故我用PHP解決了它。感謝您的時間@brainfeeder。 :) – naveencgr8 2013-04-10 12:42:11

+1

@ naveencgr8沒問題。以上是JavaScript。不一樣的Java;) – Brainfeeder 2013-04-10 13:19:49

+0

是啊我知道,對不起我的壞.. :) :) – naveencgr8 2013-04-10 14:50:31