NotToBrag NotToBrag - 26 days ago 14
PHP Question

Content only changes after manual refresh CURL

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.

Answer

You aren't using the new POST data:

if(isset($_POST['options'])) {
   // You are updating it here, but aren't storing the response,
   // so your page will use the response from the first call (above this block)
   $user->updateOptions($_POST['options']);
}

The way you are doing it, will make two calls, one to the old and one to the new every time. A more efficient way would be to do this:

$email = 'myemail@example.com';
$password = 'mypassword';
// options() and updateOptions() are in the Account class
$user = new Account($email, $password);

if(isset($_POST['options'])) {
    // If we have a post, update and use those options.
    $options = $user->updateOptions($_POST['options']);
} else {
    // No post, let's use the default
    $options = $user->options();
}

You also need to return the value from your updateOptions($id) method:

public function updateOptions($id) {
    $url =  'http://www.example.com/page/?id='.$id;

    // Let's return the value, so we actually can use it...
    return $this->options($url);
}
Comments