我正在使用Guzzle来发出一个返回JSON的aSync请求。呼叫工作正常,但响应正常,但是:
$client = new Client();
$promise = $client->requestAsync($requestType ,$this->url.$resource, // endpoint
[
'auth' => [ // credentials
$this->username,
$this->password
],
'json' => $payload, // the package
'curl' => [ // some curl options
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_RETURNTRANSFER => true,
],
'headers' => [ // custom headers
'Accept' => 'application/json',
'Content-Type' => 'application/json'
]
]
);
$response = $promise->wait();
echo $response->getStatusCode().'<br /><br />';
// Error handling
if($response->getStatusCode() != 200){
// Error Handling
}else{
echo $response->getBody(true);
}
如果我回应响应 - &gt; getBody()我看到JSON字符串,但是如果我将它分配给属性print_r,或者返回它我得到:
GuzzleHttp\Psr7\Stream Object ( [stream:GuzzleHttp\Psr7\Stream:private] => Resource id #245 [size:GuzzleHttp\Psr7\Stream:private] => [seekable:GuzzleHttp\Psr7\Stream:private] => 1 [readable:GuzzleHttp\Psr7\Stream:private] => 1 [writable:GuzzleHttp\Psr7\Stream:private] => 1 [uri:GuzzleHttp\Psr7\Stream:private] => php://temp [customMetadata:GuzzleHttp\Psr7\Stream:private] => Array ( ) )
我需要使用JSON来验证来自服务的响应。我怎样才能做到这一点?我已经阅读了文档,但我显然错过了一些东西。
基本上是将json getBody输出分配给$ json:
if($json->first_field > 0)
任何帮助表示赞赏。 此致
答案 0 :(得分:15)
经过一番关于SO的研究后,我首先进入了这篇文章
Guzzle 6: no more json() method for responses
基本上执行以下操作将返回原始输出。
return $response->getBody()->getContents();
巨大的头痛消失了。希望这有助于某人