Treatment of HTML co Guzzle Laravel?

Asked

Viewed 488 times

1

I have an application that needs to read external data, or another URL, I’m doing this necessarily with Guzzle, but when I convert to json to mount the array with the data, it returns me null.

use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;


class salarioController extends Controller
{

  public function index()
  {
     $client =  new Client();
     $response = $client->get('http://www.guiatrabalhista.com.br/guia/salario_minimo.htm');
     $body = json_decode($response->getBody(), TRUE);
  }
}

The result of $body is empty.

  • That’s because the function json_decode expects a string JSON and you’re passing a document HTML: $response->getBody()

  • The result of that address is html, and json_decode needs a string in the format json => json_decode ( string $json [, bool $assoc ] ) actually the result is null ...

  • BLZ, I’ll try it another way. I’ll try later , for now thanks a lot for the ringtones.

  • Good morning guys, consider SOLVED. I used guzzle + Goutte however it needs to be installed php7.1-xml package. it worked PERFEITAMETE. From then on just treat the array(). Thank you very much for the strength .. the code below is CORRECT and WORKING!!!!

1 answer

-1

At the moment I have this code using Guzzle + Goutte, but this giving error : "Class 'Domdocument' not found"

namespace App\Http\Controllers;

use Illuminate Http Request; use Goutte Client; use Guzzlehttp Client as Guzzleclient;

class Extractcontroller extends Controller {

public function index(){

    $goutteClient = new Client();
    $guzzleClient = new GuzzleClient(array(
                        'timeout' => 6000,));

    $goutteClient->setClient($guzzleClient);

    $crawler = $goutteClient->request('GET', 'http://www.cophieu68.vn/stockonline_node.php?stcid=1');

    $news = $crawler->filterXPath('//table[@id="board_online"]')->filter('tr')->each(function ($tr, $i) { return $tr->filter('td')->each(function ($td, $i) { return trim($td->text()); }); });

    return $news;
}

}

Browser other questions tagged

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