如何从cURL返回的html中获取值输入?

时间:2016-02-12 13:26:47

标签: php html curl

我只需捕获curl_exec()函数返回的html输入值。这个函数返回页面的完整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;

}

1 个答案:

答案 0 :(得分: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 />";
}
?>