Php and guzzle, how to do a get to an address and get what to return?

1

I need to send data by get and get what to return to know if it worked or not, however, with cURL and file_get_contents is not giving (seems to be something of the server that receives the request), hence I'm trying to use Guzzle, I did a test here and it seems not to have caused problems, however, I'm not sure how to see the return of the request, I'm doing this:

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());

It 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) {     }   }

How to get the HTML or json with the server response?!

Vlw.

    
asked by anonymous 01.09.2015 / 16:10

2 answers

1

Correct @RodrigoRigotti's answer.

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

This can be done like this:

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

Or so:

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

Particularly, I prefer the first form.

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

var_dump((string) $response->getBody());
    
01.09.2015 / 17:28
2

Just pass true as a parameter of the Response::getBody() method to get the body of the HTML as a string.

$html = $response->getBody(true);
    
01.09.2015 / 16:15