2016-11-06 58 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

相關問題