Tôi đang cố gắng viết một trình bao bọc xung quanh một api mà công ty của tôi đang phát triển. It's restful, và sử dụng Postman tôi có thể gửi một yêu cầu gửi đến một điểm cuối như http://subdomain.dev.myapi.com/api/v1/auth/
với tên người dùng và mật khẩu làm dữ liệu POST và tôi được trả lại một mã thông báo. Tất cả các công trình như mong đợi. Bây giờ, khi tôi cố gắng và làm tương tự từ PHP tôi lấy lại một đối tượng GuzzleHttp\Psr7\Response
, nhưng dường như không thể tìm thấy mã thông báo ở bất kỳ đâu bên trong nó như tôi đã làm với yêu cầu Postman.Guzzlehttp - Làm thế nào có được cơ thể của một phản ứng từ Guzzle 6?
Mã liên quan trông giống như:
$client = new Client(['base_uri' => 'http://companysub.dev.myapi.com/']);
$response = $client->post('api/v1/auth/', [
'form_params' => [
'username' => $user,
'password' => $password
]
]);
var_dump($response); //or $resonse->getBody(), etc...
Kết quả của đoạn mã trên trông giống như sau (cảnh báo, tường đến của văn bản):
object(guzzlehttp\psr7\response)#36 (6) {
["reasonphrase":"guzzlehttp\psr7\response":private]=>
string(2) "ok"
["statuscode":"guzzlehttp\psr7\response":private]=>
int(200)
["headers":"guzzlehttp\psr7\response":private]=>
array(9) {
["connection"]=>
array(1) {
[0]=>
string(10) "keep-alive"
}
["server"]=>
array(1) {
[0]=>
string(15) "gunicorn/19.3.0"
}
["date"]=>
array(1) {
[0]=>
string(29) "sat, 30 may 2015 17:22:41 gmt"
}
["transfer-encoding"]=>
array(1) {
[0]=>
string(7) "chunked"
}
["content-type"]=>
array(1) {
[0]=>
string(16) "application/json"
}
["allow"]=>
array(1) {
[0]=>
string(13) "post, options"
}
["x-frame-options"]=>
array(1) {
[0]=>
string(10) "sameorigin"
}
["vary"]=>
array(1) {
[0]=>
string(12) "cookie, host"
}
["via"]=>
array(1) {
[0]=>
string(9) "1.1 vegur"
}
}
["headerlines":"guzzlehttp\psr7\response":private]=>
array(9) {
["connection"]=>
array(1) {
[0]=>
string(10) "keep-alive"
}
["server"]=>
array(1) {
[0]=>
string(15) "gunicorn/19.3.0"
}
["date"]=>
array(1) {
[0]=>
string(29) "sat, 30 may 2015 17:22:41 gmt"
}
["transfer-encoding"]=>
array(1) {
[0]=>
string(7) "chunked"
}
["content-type"]=>
array(1) {
[0]=>
string(16) "application/json"
}
["allow"]=>
array(1) {
[0]=>
string(13) "post, options"
}
["x-frame-options"]=>
array(1) {
[0]=>
string(10) "sameorigin"
}
["vary"]=>
array(1) {
[0]=>
string(12) "cookie, host"
}
["via"]=>
array(1) {
[0]=>
string(9) "1.1 vegur"
}
}
["protocol":"guzzlehttp\psr7\response":private]=>
string(3) "1.1"
["stream":"guzzlehttp\psr7\response":private]=>
object(guzzlehttp\psr7\stream)#27 (7) {
["stream":"guzzlehttp\psr7\stream":private]=>
resource(40) 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) {
}
}
}
Kết quả từ Postman là một cái gì đó như:
{
"data" : {
"token" "fasdfasf-asfasdfasdf-sfasfasf"
}
}
Rõ ràng là tôi đang thiếu điều gì đó về cách làm việc với đối tượng phản hồi trong Gu zzle. Câu trả lời Guzzle cho biết mã trạng thái 200 theo yêu cầu, vì vậy tôi không chắc chắn chính xác những gì tôi cần làm để truy xuất dữ liệu được trả về.
'$ response-> getBody() -> getContents()' không hoạt động? – Federkun
Bạn thiên tài xinh đẹp @Leggendario. Bạn là một huyền thoại trong thời đại của riêng bạn. Tôi không biết làm thế nào tôi đã không tìm thấy 'getContents()' trước đây. – Greg