Reputation: 58810
I have an API, and make GET to it via Postman:
Ex.http://site/api/users.count
I got
{
"status": 200,
"message": "Success",
"data": {
"count": 8
}
}
I've tried to use Guzzle:
composer require guzzlehttp/guzzle
Using version ^6.3 for guzzlehttp/guzzle
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing guzzlehttp/promises (v1.3.1)
Downloading: 100%
- Installing psr/http-message (1.0.1)
Loading from cache
- Installing guzzlehttp/psr7 (1.4.2)
Loading from cache
- Installing guzzlehttp/guzzle (6.3.0)
Downloading: 100%
Writing lock file
Generating autoload files
> php artisan clear-compiled
> php artisan optimize
Generating optimized class loader
I add these 2 lines on top of my class
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
$client = new Client();
$res = $client->request('GET','http://site/api/users.count');
dd($res->getStatusCode());
I got 200
dd($res->getBody());
Stream {#662 ▼
-stream: stream resource @272 ▼
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
dd($res);
I got
Response {#664 ▼
-reasonPhrase: "OK"
-statusCode: 200
-headers: array:4 [▼
"Connection" => array:1 [▼
0 => "Keep-Alive"
]
"Content-Length" => array:1 [▼
0 => "61"
]
"Content-Type" => array:1 [▼
0 => "application/json; charset=utf-8"
]
"Date" => array:1 [▼
0 => "Wed, 18 Oct 2017 18:01:50 GMT"
]
]
-headerNames: array:4 [▼
"connection" => "Connection"
"content-length" => "Content-Length"
"content-type" => "Content-Type"
"date" => "Date"
]
-protocol: "1.1"
-stream: Stream {#662 ▼
-stream: stream resource @272 ▼
wrapper_type: "PHP"
stream_type: "TEMP"
mode: "w+b"
unread_bytes: 0
seekable: true
uri: "php://temp"
options: []
}
-size: null
-seekable: true
-readable: true
-writable: true
-uri: "php://temp"
-customMetadata: []
}
}
I am hoping to get a similar result like this :
{
"status": 200,
"message": "Success",
"data": {
"count": 8
}
}
How can I debug this?
Upvotes: 1
Views: 1359
Reputation: 9957
You can see by the response headers that everything went OK. Status code is 200, Content-Length
is not 0, etc.
The body of the response is in the body
property of GuzzleHttp\Psr7\Response
and it's a GuzzleHttp\Psr7\Stream
. You can access it with the getBody()
method.
You can use read($n)
to read n
bytes from the response, or get the whole response by casting the Stream
to a string, either implicitly (via echo
) or explicitly (with a cast operator):
var_dump((string) $res->getBody()); // explicitly
echo $res->getBody(); // implicitly
For future reference, you can use the debug mode to see more information about the whole request:
$res = $client->request('GET','http://site/api/users.count', ["debug" => true]);
Upvotes: 2
Reputation: 592
You have to use $response->getBody();
you can also do things like
->getStatusCode();
Try and see their docs, there are a lot of options but to get the content use the getBody function
In your exact case dd($res->getBody());
Upvotes: 0