2011-06-07 62 views
0

我堅持嘗試處理一個隨機的名稱和值的形式HTML通過捲曲發佈數據。
在這裏我的代碼。php,curl,curl_exec()

function grapvalue($html){ 

//parse content to grap name and value random 

} 

$opt = array(
     CURLOPT_AUTOREFERER => true, 
     CURLOPT_RETURNTRANSFER => true, 
     CURLOPT_COOKIEFILE => 'cookie.txt', 
     CURLOPT_COOKIEJAR => 'cookie.txt' 
     CURLOPT_POST => true, 
     CURLOPT_POSTFIELDS => grapvalue($html) 
     ) 

$init = curl_init("site.com") 
curl_setopt_array($init,$opt) 
$html = curl_exec($init) 

我必須要執行的函數curl_exec()兩次,第一次我必須執行curl_exec函數來檢索HTML得到的名稱和隨機值,再發送數據。 我該如何解決這個問題?

+1

所以你需要提出一個請求來找出表單域是什麼,然後再提交一個實際提交它們的請求?爲什麼你不能硬編碼參數? – lonesomeday 2011-06-07 16:46:14

回答

2

以下是完整的解決方案:

//First, we make a request to grab the variables 
function grapvalue(){ 

$opt = array(
    CURLOPT_AUTOREFERER => true, 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_COOKIEFILE => 'cookie.txt', 
    CURLOPT_COOKIEJAR => 'cookie.txt' 
    ); 

$init = curl_init("site.com"); 
curl_setopt_array($init,$opt); 
$html = curl_exec($init); 

//parse content to grap name and value random 

} 

//Than, just call grapvalue() without any parameters 
$opt = array(
    CURLOPT_AUTOREFERER => true, 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_COOKIEFILE => 'cookie.txt', 
    CURLOPT_COOKIEJAR => 'cookie.txt' 
    CURLOPT_POST => true, 
    CURLOPT_POSTFIELDS => grapvalue() 
    ); 

$init = curl_init("site.com"); 
curl_setopt_array($init,$opt); 
$html = curl_exec($init); 

提示:你可能更喜歡使用CURLOPT_COOKIE而不是CURLOPT_COOKIEFILECURLOPT_COOKIEJAR

+0

@develroot,我仍然無法發送發佈數據,我不知道是什麼原因造成的? 我可以問你的電子郵件地址嗎?我會發送我創建的所有代碼。 – mac786751 2011-06-12 23:48:35

+0

終於我的腳本已經成功運行,沒有錯誤!非常感謝大家。 。 – mac786751 2011-06-16 19:32:31

+0

如果有幫助,您可以接受此答案。 – technology 2011-06-16 19:33:51

1

爲什麼不創建function cURL(),調用該函數,獲取$html並再次調用post值?

+0

這裏的示例代碼我想得到和解析後得到價值隨機與一個單一的請求。 ' ' 每次的名稱和值是隨機的。 對不起,我不會說英語好花花公子 – mac786751 2011-06-07 18:01:08

+0

@ mac786751你不能用一個請求來做。順便說一下,cURL函數可以用於獲取/發佈請求: http://pastebin.com/7Vnw1vjJ – fvox 2011-06-07 19:26:58