Skip to content

Instantly share code, notes, and snippets.

@sagikazarmark

sagikazarmark/guzzle#2018

Last active Feb 27, 2018
Embed
What would you like to do?
* Trying ::1...
* TCP_NODELAY set
* Connection failed
* connect to ::1 port 8080 failed: Connection refused
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /server.php HTTP/1.1
Host: localhost:8080
User-Agent: GuzzleHttp/6.3.0 curl/7.54.0 PHP/5.6.30
< HTTP/1.1 200 OK
< Host: localhost:8080
< Connection: close
< X-Powered-By: PHP/5.6.30
< Content-type: text/html; charset=UTF-8
<
* Closing connection 0
Status code: 200
Body size: 43
Stream object type: GuzzleHttp\Psr7\Stream
{"id": "1234", "name": "bar \"for\" baz " }
array(2) {
["id"]=>
string(4) "1234"
["name"]=>
string(14) "bar "for" baz "
}
<?php
echo '{"id": "1234", "name": "bar \"for\" baz " }';
php -S 0.0.0.0:8080
<?php
require_once __DIR__.'/vendor/autoload.php';
$client = new GuzzleHttp\Client([
'handler' => new \GuzzleHttp\Handler\CurlHandler(),
//'handler' => new \GuzzleHttp\Handler\StreamHandler(),
]);
$response = $client->get(
'http://localhost:8080/server.php',
[
'debug' => true,
]
);
echo 'Status code: '.$response->getStatusCode()."\n";
echo 'Body size: '.$response->getBody()->getSize()."\n";
echo 'Stream object type: '.get_class($response->getBody())."\n";
$body = $response->getBody();
while (!$body->eof()) {
echo $body->read(1024);
flush();
}
echo "\n\n";
var_dump(json_decode((string) $body, true));
php test.php
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.