dongxin8709 2017-02-04 16:09
浏览 66
已采纳

使用带有Laravel 5.3的Guzzle 6.2处理卷曲响应

I am trying to obtain a JSON response from an end point using Guzzle 6.2 with Laravel 5.3.

I am using the following code to make a get request:

$client = new GuzzleHttp\Client([
  'base_uri' => 'https://192.xx.xxx.xx6/',
  'timeout'  => 2.0
]);

$response = $client->request('GET',
  '/fineract-provider/api/v1/clients/388?tenantIdentifier=default&pretty=true', [
    'verify' => false,
    'auth' => ['<username>', '<password>']
  ]
);

var_dump($response);

Which outputs the following response:

Response {#282
  -reasonPhrase: "OK"
  -statusCode: 200
  -headers: array:7 [
    "Server" => array:1 [
      0 => "Apache-Coyote/1.1"
    ]
    "Access-Control-Allow-Origin" => array:1 [
      0 => "*"
    ]
    "Access-Control-Allow-Methods" => array:1 [
      0 => "GET, POST, PUT, DELETE, OPTIONS"
    ]
    "Content-Type" => array:1 [
      0 => "application/json"
    ]
    "Transfer-Encoding" => array:1 [
      0 => "chunked"
    ]
    "Vary" => array:1 [
      0 => "Accept-Encoding"
    ]
    "Date" => array:1 [
      0 => "Sat, 04 Feb 2017 15:51:10 GMT"
    ]
  ]
  -headerNames: array:7 [
    "server" => "Server"
    "access-control-allow-origin" => "Access-Control-Allow-Origin"
    "access-control-allow-methods" => "Access-Control-Allow-Methods"
    "content-type" => "Content-Type"
    "transfer-encoding" => "Transfer-Encoding"
    "vary" => "Vary"
    "date" => "Date"
  ]
  -protocol: "1.1"
  -stream: Stream {#280
    -stream: stream resource @297
      wrapper_type: "PHP"
      stream_type: "TEMP"
      mode: "w+b"
      unread_bytes: 0
      seekable: true
      uri: "php://temp"
      options: []
    }
    -size: null
    -seekable: true
    -readable: true
    -writable: true
    -uri: "php://temp"
    -customMetadata: []
  }
}

But this is not the response I expect. However, when I make the same request in my browser it gives the correct output as below:

enter image description here

What am I doing wrong here?

</div>
  • 写回答

1条回答 默认 最新

  • drg5577 2017-02-04 16:17
    关注

    The Response object contains more information than just the response. You can get the actual output like this:

    $output = (string)$response->getBody();
    

    It might be necessary (in certain cases) to cast the result to a string, because the actual result is a stream.

    Guzzle documentation: Responses

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。