How to get dynamic data returned by cURL?

0

Good night, I need to return data from a specific site for my application, for this I chose to use the cURL () PHP method, it sends a post and I return the information I want, the problem is that in that return comes the complete page of the form I accessed, I did not find any way to just get input data from this form. I need to capture the information returned from the form and move to json.

Here is the method that performs this search:

public function busca(Request $request){

    $data = $request->all();

      $cURL = curl_init('http://www.site.com.br/resultado.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.site.com.br/index.php');

      $resultado = curl_exec($cURL);

      curl_close($cURL);

      return $resultado;
}
    
asked by anonymous 11.02.2016 / 22:48

1 answer

1

You'll need to understand about regular expression to cut out parts of what you want to extract from HTML, or else you can study this native PHP link it can simulate javascript DOM to access the html node.

If the data you want to get is a little something I recommend regular expression.

But if there is a lot I recommend using the DOM.

Example using the DOM:

$dom = new DOMDocument;
$dom->loadXML($html);
$links = $dom->getElementsByTagName('link');

foreach ($links as $link) {
  print_r($link->getAttributes());
}
    
12.02.2016 / 20:01