How to get dynamic data returned by Curl?

Asked

Viewed 704 times

0

Good evening, I need to return data from a site specific to my application, for this I chose to use the Curl() PHP method, in it I send a post and return the information I want, the problem is that in this return comes the full page of the form that Acessei, I didn’t find any way to just get input data from that form. I need to capture the information returned from the form and pass 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;
}
  • The information is returned within Inputs? If possible add the returned HTML (at least in the chunk you want to get the data).

  • Yes, it works as if it takes a copy of the full page where the data is returned from the original site, and the data I want to capture is inside the inputs.

1 answer

1


You’ll need to understand about regular expression to cut out parts of what you want to extract from HTML or if you can’t study that native PHP lib http://php.net/manual/en/book.dom.php it can simulate javascript DOM to access html Node.

If the data you want to take is little thing I recommend regular expression.

But if it’s too much 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());
}
  • Thank you for the reply, it worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.