如何获得从cURL返回的html值输入?
问题描述:
我只需要捕获从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;
}
答
你可以尝试使用许多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 />";
}
?>
没有的功能,它可以帮助你这个样子。你需要编写一个解析器来获取这些值,或者你可以实现一个休息API来获取数据。 – Afnan