发送 guzzle `get` 请求后无法打印响应数据
can't print response data after sending guzzle `get` request
在使用 get 参数提交 url 后,我能够使用 POSTMAN
获得一些我想要的数据:
http://10.0.0.0/adserver/src/public?url=http://dummy.com
但是当我尝试向 guzzle 发送相同的请求时:
public function testGetAds_test()
{
$client = new Client(['base_uri' => $this->config['base_url']]);
$response = $client->get('getAds', ['query' => ['url' => 'http://dummy.com']]);
$data = json_decode($response->getBody());
var_dump($response->getBody());
}
我得到 200,但转储打印了我使用 POSTMAN
获得的数据的这个 insead:
.object(GuzzleHttp\Psr7\Stream)#41 (7) {
["stream":"GuzzleHttp\Psr7\Stream":private]=>
resource(226) 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) {
}
}
知道我在这里错过了什么吗??谢谢
对于 var_dump() 你需要使用那个
$response->getBody()->getContents()
如果您处理 Json 响应,您可以直接调用响应对象上的 json()
方法,如 here in the doc:
所述
所以你可以使用:
$data = $response->json();
Guzzle internally uses PHP’s json_decode()
function to parse
responses. If Guzzle is unable to parse the JSON response body, then a
GuzzleHttp\Exception\ParseException is thrown.
希望对您有所帮助
在使用 get 参数提交 url 后,我能够使用 POSTMAN
获得一些我想要的数据:
http://10.0.0.0/adserver/src/public?url=http://dummy.com
但是当我尝试向 guzzle 发送相同的请求时:
public function testGetAds_test()
{
$client = new Client(['base_uri' => $this->config['base_url']]);
$response = $client->get('getAds', ['query' => ['url' => 'http://dummy.com']]);
$data = json_decode($response->getBody());
var_dump($response->getBody());
}
我得到 200,但转储打印了我使用 POSTMAN
获得的数据的这个 insead:
.object(GuzzleHttp\Psr7\Stream)#41 (7) {
["stream":"GuzzleHttp\Psr7\Stream":private]=>
resource(226) 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) {
}
}
知道我在这里错过了什么吗??谢谢
对于 var_dump() 你需要使用那个
$response->getBody()->getContents()
如果您处理 Json 响应,您可以直接调用响应对象上的 json()
方法,如 here in the doc:
所以你可以使用:
$data = $response->json();
Guzzle internally uses PHP’s
json_decode()
function to parse responses. If Guzzle is unable to parse the JSON response body, then a GuzzleHttp\Exception\ParseException is thrown.
希望对您有所帮助