Php and guzzle, how do you get to an address and pick up what you return?

Asked

Viewed 1,654 times

1

I need to send data by get and pick up what to return to know if it worked or not, however, with Curl and file_get_contents not giving (seems to be the server that receives the request), so I’m trying to use Guzzle, I made a test here and seems to have given no problems, however, I don’t quite know how to see the return of the request, I’m doing so:

require_once 'vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Message\Request;
use GuzzleHttp\Message\Response;

$client = new Client();
$response = $client->get('http://www.endereco.com.br?dados');
var_dump($response->getBody());

He prints this on my screen:

Object(Guzzlehttp Psr7 Stream)#24 (7) { ["stream":"Guzzlehttp Psr7 Stream":private]=> Resource(5) of type (stream) ["size":"Guzzlehttp Psr7 Stream":private]=> NULL ["seekable":"Guzzlehttp Psr7 Stream":private]=> bool(true) ["readable":"Guzzlehttp Psr7 Stream":private]=> bool(true) ["writable":"Guzzlehttp Psr7 Stream":private]=> bool(true) ["Uri":"Guzzlehttp Psr7 Stream":private]=> string(10) "php://temp" ["customMetadata":"Guzzlehttp Psr7 Stream":private]=> array(0) { } }

You can get HTML or json with the server response ?!

Vlw.

2 answers

2

Just go through true as a method parameter Response::getBody() to receive the HTML body as a string.

$html = $response->getBody(true);

1


Correct answer from @Rodrigorigotti.

There is also another way to convert the data, which is by using the magic method __toString.

It can be done like this:

$body = (string) $response->getBody(); 

Or so:

$body = $response->getBody()->__toString();

Personally, I prefer the first way.

If you want to debug an object that can be converted to string via var_dump, you should do so:

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

Browser other questions tagged

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