相关疑难解决方法(0)

Guzzlehttp - 如何从Guzzle 6获得回复?

我正在尝试围绕我公司正在开发的api编写一个包装器.它很安静,使用Postman我可以发送一个帖子请求到一个端点,比如http://subdomain.dev.myapi.com/api/v1/auth/使用用户名和密码作为POST数据,我会收到一个令牌.一切都按预期工作.现在,当我尝试从PHP执行相同操作时,我会返回一个GuzzleHttp\Psr7\Response对象,但似乎无法在其中的任何位置找到令牌,就像我对Postman请求所做的那样.

相关代码如下:

$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...
Run Code Online (Sandbox Code Playgroud)

上面代码的输出看起来像(警告,传入文本墙):

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]=> …
Run Code Online (Sandbox Code Playgroud)

php response guzzle guzzle6

140
推荐指数
3
解决办法
10万
查看次数

处理Guzzle异常并获取HTTP正文

当服务器返回4xx和5xx状态代码时,我想处理Guzzle的错误.我提出这样的请求:

$client = $this->getGuzzleClient();
$request = $client->post($url, $headers, $value);
try {
    $response = $request->send();
    return $response->getBody();
} catch (\Exception $e) {
    // How can I get the response body?
}
Run Code Online (Sandbox Code Playgroud)

$e->getMessage返回代码信息,但不返回HTTP响应的主体.我如何获得响应机构?

php guzzle

103
推荐指数
8
解决办法
11万
查看次数

标签 统计

guzzle ×2

php ×2

guzzle6 ×1

response ×1