2016-11-06 60 views
0

我正试图从网页中检索数据。手动更改下拉列表会在页面上生成内容,并且会存储该内容,以便在刷新页面时,更改后的数据将保留,除非手动再次进行更改。仅在手动刷新后才会更改内容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的末尾添加了发布数据字段,但是除非手动刷新我的网站,否则内容不会出现。

+0

查找到阿贾克斯。 – Mihai

回答

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 = '[email protected]'; 
$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); 
} 
+0

感谢您的回复!但是现在我得到了一个'(!)警告:在我的下拉菜单中为foreach()提供了无效参数,这很奇怪,因为下拉选项仍然显示为回显。内容也没有改变。 – NotToBrag

+0

该网页上的下拉列表包含更改网址的ID。我正在检索这些ID,然后在提交时运行发布请求,以便我可以从我的网站更改网址,然后加载更改后的内容。 – NotToBrag

+0

''我的main.php'var_dump($选项)'在请求包含所有我试图检索但在index.php中做同样的事情后返回null。 – NotToBrag

相关问题