Gu 6. 6.x /没有得到预期的结果
问题描述:
我有一个平静的API。当我运行以下网址邮递员,我收到的回应如下:Gu 6. 6.x /没有得到预期的结果
POSTMAN RUN URL
DELETE
https://www.example.com/api/v1/Blog/blog/13
{
"status":"Failure",
"message":"The specified blog post could not be found"
}
以上是当然的预期,但是,我无法读取“状态”和“消息”。我如何得到答复?这是我目前的代码:
$entry_id = $this->uri->segment(3);
$theUrl = $this->config->item('base_url').'api/v1/Blog/blog/'.$entry_id;
// tested $theUrl and works
$client = new GuzzleHttp\Client([
'base_uri' => $theUrl,
'timeout' => 3.0,
'http_errors' => FALSE
]);
$response = $client->delete($theUrl);
$code = $response->getStatusCode();
$response = $client->delete($theUrl);
$x = $response->getBody();
echo "<pre>";
echo var_dump($x); // cannot see message or status anywhere.
echo "</pre>";
您的建议非常感谢。
+++ 现在我已经试过这个修改后的代码,但仍看不到回复的状态或消息日期:
$entry_id = $this->uri->segment(3);
$theUrl = $this->config->item('base_url').'api/v1/Blog/blog/'.$entry_id;
$client = new GuzzleHttp\Client([
'timeout' => 3.0,
'http_errors' => FALSE
]);
$response = $client->delete($theUrl, ['debug' => true]);
$code = $response->getStatusCode();
$x = $response->getBody();
echo "<pre>";
echo var_dump($x);
echo "</pre>";
die();
这里是转储和调试信息:
https://www.example.com/api/v1/Blog/blog/6
* Hostname was found in DNS cache * Trying 104.131.132.25... * Connected to
www.example.com (104.131.132.25) port 443 (#1) * successfully set
certificate verify locations: * CAfile: none CApath: /etc/ssl/certs * SSL
connection using XXXXXXXXXXXXXXXXXXXXXXXXX* Server certificate: *
subject: CN=www.example.com * start date: 2016-10-29 05:15:00 GMT * expire
date: 2017-01-27 05:15:00 GMT * subjectAltName: www.movinghaus.com matched
* issuer: C=US; O=Let's Encrypt; CN=Let's Encrypt Authority X3 * SSL
certificate verify ok. > DELETE /api/v1/Blog/blog/6 HTTP/1.1 User-Agent:
GuzzleHttp/6.2.0 curl/7.35.0 PHP/5.5.9-1ubuntu4.11 Host: www.example.com <
HTTP/1.1 200 OK < Date: Fri, 04 Nov 2016 03:55:21 GMT * Server Apache/2.4.7
(Ubuntu) is not blacklisted < Server: Apache/2.4.7 (Ubuntu) < X-Powered-By:
PHP/5.5.9-1ubuntu4.11 < Set-Cookie:
PHPSESSID=XXXXXXXXXXXXXXXXXX; expires=Fri, 04-Nov-2016 05:55:21 GMT; Max-
Age=7200; path=/; HttpOnly < Expires: Thu, 19 Nov 1981 08:52:00 GMT <
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-
check=0 < Pragma: no-cache < Content-Length: 40 < Content-Type:
application/json; charset=utf-8 < * Connection #1 to host www.example.com
left intact
object(GuzzleHttp\Psr7\Stream)#65 (7) {
["stream":"GuzzleHttp\Psr7\Stream":private]=>
resource(50) 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) {
}
}
答
我认为新的GuzzleHttp \ Client的base_uri参数是不需要的。 base_uri参数用于设置基本url。 (http://docs.guzzlephp.org/en/latest/quickstart.html?highlight=base_uri)然后,所有对客户端的调用都应该使用相对uris。由于您在删除函数调用中使用绝对URL,因此您不需要base_uri参数。
另外您还调用了两次删除函数:$ response = $ client-> delete($ theUrl)。
答
根据docs。 你应该做
$x = $response->getBody()->getContents();
或浇铸body
到string
:
$x = (string)$response->getBody()
尝试'$响应 - > JSON();' – BVengerov
感谢。尝试$ x = $ response-> json();但得到以下错误: 调用未定义的方法GuzzleHttp \ Psr7 \ Response :: json() – user3264461
当我做$ repsonse-> getBody();我得到:object(GuzzleHttp \ Psr7 \ Stream)#65(7){ [“stream”:“GuzzleHttp \ Psr7 \ Stream”:private] => 类型(流) 的资源(49) :“GuzzleHttp \ Psr7 \ Stream”:private] => NULL [“seekable”:“GuzzleHttp \ Psr7 \ Stream”:private] => bool(true) [“readable”:“GuzzleHttp \ Psr7 \ Stream “:private] => bool(true) [”可写“:”GuzzleHttp \ Psr7 \ Stream“:private] => bool(true) [”uri“:”GuzzleHttp \ Psr7 \ Stream“:private] => string(10)“php:// temp” [“customMetadata”:“GuzzleHttp \ Psr7 \ Stream”:private] => array(0){ } } no状态或消息 – user3264461