我正试图从网页中检索数据。手动更改下拉列表会在页面上生成内容,并且会存储该内容,以便在刷新页面时,更改后的数据将保留,除非手动再次进行更改。仅在手动刷新后才会更改内容CURL
因此,根据下拉的设置,我会收到不同的数据。
我已经想通了如何确定我想从页面检索数据字段和改变,像这样的URL显示什么数据:
main.php:
public function options($url = null) {
// no data fields provided
if($url == null)
$url = 'http://www.example.com/page/';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$retrievedPage = curl_exec($ch);
// retrieve options from drop down menu
$regex = '/<div class=\"form\-item form\-type\-select\">(.*?)<\/div>/s';
preg_match($regex, $retrievedPage, $options);
$doc = new DOMDocument();
@$doc->loadHTML($options[0]);
$ops = $doc->getElementsByTagName('option');
$singleOption = array();
$options = array();
foreach($ops as $op) {
$name = $op->nodeValue;
$id = $op->getAttribute('value');
$singleOption = array(
'name' => $name,
'id' => $id
);
array_push($options, $singleOption);
}
return $options;
}
public function updateOptions($id) {
$url = 'http://www.example.com/page/?id='.$id;
$this->options($url);
}
index.php:
<?php
$email = '[email protected]';
$password = 'mypassword';
// options() and updateOptions() are in the Account class
$user = new Account($email, $password);
// options array, initially called to display dropdown options
$options = $user->options();
if(isset($_POST['options'])) {
$user->updateOptions($_POST['options']);
}
?>
<form method="POST">
<select name="options" onchange="this.form.submit()">
<?php foreach($options as $key): ?>
<?php
echo '<option value="'.$key['id'].'">'.$key['name'].'</option>';
?>
<?php endforeach; ?>
</select>
</form>
下拉菜单成功地看起来像这样在H TML:
<select>
<option value="123">Option 1</option>
<option value="456">Option 2</option>
</select>
的问题是,改变的内容将不会出现,除非我手动刷新我的网站的形式提交后。
我放了回声来检查curl_exec()
之前的URL,它似乎成功地在URL的末尾添加了发布数据字段,但是除非手动刷新我的网站,否则内容不会出现。
查找到阿贾克斯。 – Mihai