2017-02-15 108 views
1

我使用PHP-CURL登錄到使用ASP編寫的站點。登錄後,我需要從頁面中提取數據。數據用頁面上的元素更新。 如何通過使用Curl發佈「選項值」來進行選擇?使用php-curl選擇組合框

選擇元素:

</select> 
<span id="ctl02_lblDonem" class="NormalBold">DÖNEM</span> 
<select name="ctl02$dlDonem" onchange="javascript:setTimeout(&#39;__doPostBack(\&#39;ctl02$dlDonem\&#39;,\&#39;\&#39;)&#39;, 0)" id="ctl02_dlDonem" class="NormalBlack"> 
     <option value="-10">G&#220;Z</option> 
     <option value="-15">G&#220;Z SONU EK D&#214;NEM</option> 
     <option selected="selected" value="-20">BAHAR</option> 
     <option value="-25">BAHAR SONU EK D&#214;NEM</option> 
     <option value="-30">YAZ</option> 
     <option value="-35">YAZ SONU EK D&#214;NEM</option> 
</select> 

腳本:

<script type="text/javascript"> 
//<![CDATA[ 
var theForm = document.forms['Form1']; 
if (!theForm) { 
    theForm = document.Form1; 
} 
function __doPostBack(eventTarget, eventArgument) { 
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) { 
     theForm.__EVENTTARGET.value = eventTarget; 
     theForm.__EVENTARGUMENT.value = eventArgument; 
     theForm.submit(); 
    } 
} 
//]]> 
</script> 

我的代碼:

<?php 
$url = "https://site/Default.aspx"; 
$ckfile = tempnam("/tmp", "CURLCOOKIE"); 
$useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.2 (KHTML, like Gecko) Chrome/5.0.342.3 Safari/533.2'; 

$username = "xxxx"; 
$password = "xxxx"; 


$f = fopen('log.txt', 'w'); // file to write request header for debug purpose 

/** 
    Get __VIEWSTATE & __EVENTVALIDATION 
*/ 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 

$html = curl_exec($ch); 

curl_close($ch); 

preg_match('~<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="(.*?)" />~', $html, $viewstate); 
preg_match('~<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="(.*?)" />~', $html, $eventValidation); 
preg_match('~<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="(.*?)" />~', $html, $eventGenerator); 

$viewstate = $viewstate[1]; 
$eventValidation = $eventValidation[1]; 
$viewGenerator = $eventGenerator[1]; 



/** 
Start Login process 
*/ 
$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile); 
curl_setopt($ch, CURLOPT_HEADER, FALSE); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_REFERER, $url); 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 
curl_setopt($ch, CURLOPT_STDERR, $f); 
curl_setopt($ch, CURLOPT_USERAGENT, $useragent); 

// Collecting all POST fields 
$postfields = array(); 
$postfields['tcmDefault_HiddenField'] = ""; 
$postfields['__LASTFOCUS'] = ""; 
$postfields['__EVENTTARGET'] = ""; 
$postfields['__EVENTARGUMENT'] = ""; 
$postfields['__VIEWSTATE'] = $viewstate; 
$postfields['__VIEWSTATEGENERATOR'] = $viewGenerator; 
$postfields['__EVENTVALIDATION'] = $eventValidation; 
$postfields['ctl02$txtboxOgrenciNo'] = $username; 
$postfields['ctl02$txtBoxSifre'] = $password; 
$postfields['ctl02$btnLogin'] = "Giriş"; 

curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); 
$ret = curl_exec($ch); // Get result after login page. 
curl_setopt($ch, CURLOPT_URL, 'https://site/Default.aspx?tabInd=2&tabNo=3'); //select element inside this page. 

//execute the request 
$content = curl_exec($ch); 

echo $content; 
?> 

感謝

回答

1

簡單的方法就是打開Chrome DevTools(F12),導航到網絡選項卡,馬每年都要進行瑣碎的操作,然後將這些數據寫入CURL。

的更多信息,可以發現here

+1

我想補充:輸入是在'form'即貼,將根據輸入的'name' atrribute有他們的關鍵。 –

+0

謝謝你這麼多先生!它的工作。 – mrdeveloper