内容仅在手动刷新CURL后更改

时间:2016-11-06 15:28:43

标签: php curl

我正在尝试从网页中检索数据。手动更改下拉列表会在页面上生成内容,并存储该内容,以便在刷新页面时,更改的数据将保留,除非我再次手动更改它。

因此,根据下拉列表的设置方式,我会收到不同的数据。

我已经弄明白了如何通过从页面检索数据字段并更改网址来确定要显示的数据:

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>

下拉菜单在HTML中成功显示如下:

 <select>
     <option value="123">Option 1</option>
     <option value="456">Option 2</option>
 </select>

问题是,除非我在提交表单后手动刷新我的网站,否则更改的内容不会出现。

我在curl_exec()之前放置了一个echo来检查网址,看来它在网址的末尾成功添加了帖子数据字段,但是除非我手动,否则内容不会出现刷新我的网站。

1 个答案:

答案 0 :(得分:0)

您没有使用新的POST数据:

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']);
}

你这样做的方式,每次都会打两次电话,一次是老电话,一次是新电话。一种更有效的方法是:

$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();
}

您还需要从updateOptions($id)方法中返回值:

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);
}
相关问题