duanmei1930 2016-11-06 15:28
浏览 51
已采纳

内容仅在手动刷新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.

  • 写回答

1条回答 默认 最新

  • douxiongzhen2126 2016-11-06 15:41
    关注

    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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大