使用php curl选择combobox
我使用PHP-CURL登录到一个用ASP编写的站点。登录后,我需要从页面中提取数据。数据将使用页面上的元素进行更新。 如何通过使用Curl发布“选项值”进行选择 选择元素:使用php curl选择combobox,php,curl,combobox,Php,Curl,Combobox,我使用PHP-CURL登录到一个用ASP编写的站点。登录后,我需要从页面中提取数据。数据将使用页面上的元素进行更新。 如何通过使用Curl发布“选项值”进行选择 选择元素: </select> <span id="ctl02_lblDonem" class="NormalBold">DÖNEM</span> <select name="ctl02$dlDonem" onchange="javascript:setTimeout('_
</select>
<span id="ctl02_lblDonem" class="NormalBold">DÖNEM</span>
<select name="ctl02$dlDonem" onchange="javascript:setTimeout('__doPostBack(\'ctl02$dlDonem\',\'\')', 0)" id="ctl02_dlDonem" class="NormalBlack">
<option value="-10">GÜZ</option>
<option value="-15">GÜZ SONU EK DÖNEM</option>
<option selected="selected" value="-20">BAHAR</option>
<option value="-25">BAHAR SONU EK DÖNEM</option>
<option value="-30">YAZ</option>
<option value="-35">YAZ SONU EK DÖNEM</option>
</select>
DÖNEM
GÜ;Z
GÜ;Z SONU EK DÖ;尼姆
巴哈尔
BAHAR SONU EK D&214;尼姆
雅兹
YAZ SONU EK DÖ;尼姆
脚本:
<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;
?>
最简单的方法是打开Chrome DevTools(F12),导航到“网络”选项卡,手动执行Nessery操作,然后将此数据放入CURL
您可以找到更多信息只需添加:发布的表单
上的输入将根据输入的名称
属性设置其密钥。非常感谢您,先生!!!它起作用了。