How to get the page html after loading the javascript, using GuzzleHttp

0

Good morning,

I'm creating a Crawler to go to a specific page and then get some specific data from the page, but I'm having problems.

At this point I am trying to perform a test in instagram, my code is as follows:

$client = new Client();

$request = $client->request('GET', 'https://www.instagram.com/user/');

return response()->json( $request->getBody() );

However, at the moment I'm going to get getBody's print is returning empty {} , I've also tried to add a second parameter to get the data, as follows:

return response()->json( $request->getBody()->getContents() );

When using getContents is returning me little html and the remaining javascript, due to this I believe the error may be in the way I am calling.

    
asked by anonymous 25.10.2018 / 15:00

1 answer

0

When you use Guzzle , the value returned by getBody is an object.

This object is GuzzleHttp\Stream\Stream .

By default, the json_encode class will often cause confusing results when you attempt to serialize an object that does not implement the magic interface JsonSerialize .

But the GuzzleHttp\Stream\Stream class implements the __toString method, which allows special behavior for the class as it is treated as a string.

What can be done?

Make a cast of the result to see the value returned from your request, like this:

$client = new Client();

// Pequena correção, 'request' é requisição, 'response' é resposta

$response = $client->request('GET', 'https://www.instagram.com/user/');

var_dump((string) $response->getBody());

It is also important to remember that the response()->json() function of Laravel is intended to serialize values to the JSON format. Probably your request will have an HTML response.

Be sure of what you will do with Guzzle return.

Depending on the situation, for you, you would have to do just that:

 return response((string) $response->getBody());
  

When using getContents is returning me little html and the remaining javascript, due to this I believe the error may be in the way I am calling.

I do not know what you're trying to do, but if you want Instagram to give you a JSON , you might need to access the Instagram API.

Take a look at it here

    
25.10.2018 / 18:06