I am attempting to retrieve data from a webpage. Manually changing a dropdown generates content on the page, and that content is stored so that if I refresh the page, the changed data will stay unless I manually change it again.
As such, depending on how the dropdown is set, I'll receive different data.
I've figured out how to determine what data I want to display by retrieving data fields from the page and changing the URL like so:
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 = 'myemail@example.com';
$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>
The dropdown menu successfully looks like this in the HTML:
<select>
<option value="123">Option 1</option>
<option value="456">Option 2</option>
</select>
The problem is, the changed content won't appear unless I manually refresh my website after the form has been submitted.
I placed an echo to check the URL before the curl_exec()
and it appears that it is successfully adding the post data fields at the end of the URL but again, the content won't appear unless I manually refresh my website.