2016-02-12 49 views
0

我只需要捕獲從curl_exec()函數返回的html輸入的值。這個函數返回頁面的完整html,是否有任何方法只取得輸入的值?如何獲得從cURL返回的html值輸入?

我的代碼:

public function searchUser(Request $request){ 

    $data = $request->all(); 

     $cURL = curl_init('http://www.example.com/result.php'); 

     curl_setopt($cURL, CURLOPT_HEADER, false); 
     curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true); 

     $dados = array(
     'num_cnpj' => $data['cnpj'], 
     'botao' => 'Consultar' 

    ); 
     curl_setopt($cURL, CURLOPT_POST, true); 
     curl_setopt($cURL, CURLOPT_POSTFIELDS, $dados); 

     curl_setopt($cURL, CURLOPT_REFERER, 'http://www.example.com/index.php'); 

     $result = curl_exec($cURL); 

     curl_close($cURL); 

    return $result; 

} 
+0

沒有的功能,它可以幫助你這個樣子。你需要編寫一個解析器來獲取這些值,或者你可以實現一個休息API來獲取數據。 – Afnan

回答

0

你可以嘗試使用許多PHP DOM解析器之一。

http://php.net/manual/en/domdocument.loadhtml.php

<?php 
# Create a DOM parser object 
$dom = new DOMDocument(); 

# Parse the HTML 
# The @ before the method call suppresses any warnings that 
# loadHTML might throw because of invalid HTML in the page. 
@$dom->loadHTML($result); 

# Iterate over all the <input> tags 
foreach($dom->getElementsByTagName('input') as $input) { 
     # Show the attribute value 
     echo $input->getAttribute('value'); 
     echo "<br />"; 
} 
?> 
+0

謝謝你的回答,但方法$ dom-> getElementsByTagName('input')不捕獲輸入,返回空。 – let

+0

哦,對不起,我的html沒有返回輸入,而是一個包含結果的表格,改變了td的輸入標籤,現在正在工作。謝謝! – let