来自外部API的Guzzle HTTP GET与Symfony 3项目的意外$response



我正在使用Guzzle在我的Symfony 3项目中执行来自外部API的HTTP GET请求。这是我的控制器代码:

<?php
namespace AppBundleController;
use SymfonyBundleFrameworkBundleControllerController;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SymfonyComponentHttpFoundationResponse;
use SymfonyComponentHttpFoundationRequest;
use PsrHttpMessageResponseInterface;
use GuzzleHttpClient;
class ScheduleController extends Controller {
/**
 * @Route("/schedule")
 */
public function getJobs() {
    // Create a client with a base URI
    $client = new GuzzleHttpClient(['base_uri' => 'http://my.external.api/']);
    // Send a request to http://my.external.api/site/67/module/1449/item
    $response = $client->request('GET', 'site/67/module/1449/item', ['auth' => ['****', '****']]);
    var_dump($response);
    exit;
    return $this->json(array($response));
}
}

我从我的代码中得到了以下var_dump($response)

object(GuzzleHttpPsr7Response)#397 (6) { ["reasonPhrase":"GuzzleHttpPsr7Response":private]=> string(2) "OK" ["statusCode":"GuzzleHttpPsr7Response":private]=> int(200) ["headers":"GuzzleHttpPsr7Response":private]=> array(11) { ["Date"]=> array(1) { [0]=> string(29) "Tue, 24 Jan 2017 19:39:17 GMT" } ["Server"]=> array(1) { [0]=> string(6) "Apache" } ["Cache-Control"]=> array(2) { [0]=> string(35) "no-cache, no-store, must-revalidate" [1]=> string(46) "no-cache, no-store, max-age=0, must-revalidate" } ["Pragma"]=> array(2) { [0]=> string(8) "no-cache" [1]=> string(8) "no-cache" } ["Expires"]=> array(2) { [0]=> string(1) "0" [1]=> string(1) "0" } ["X-Content-Type-Options"]=> array(1) { [0]=> string(7) "nosniff" } ["X-XSS-Protection"]=> array(1) { [0]=> string(13) "1; mode=block" } ["X-Frame-Options"]=> array(1) { [0]=> string(4) "DENY" } ["Set-Cookie"]=> array(1) { [0]=> string(64) "SiteIdentifier=67; Expires=Wed, 25-Jan-2017 19:39:17 GMT; Path=/" } ["Transfer-Encoding"]=> array(1) { [0]=> string(7) "chunked" } ["Content-Type"]=> array(1) { [0]=> string(30) "application/json;charset=UTF-8" } } ["headerNames":"GuzzleHttpPsr7Response":private]=> array(11) { ["date"]=> string(4) "Date" ["server"]=> string(6) "Server" ["cache-control"]=> string(13) "Cache-Control" ["pragma"]=> string(6) "Pragma" ["expires"]=> string(7) "Expires" ["x-content-type-options"]=> string(22) "X-Content-Type-Options" ["x-xss-protection"]=> string(16) "X-XSS-Protection" ["x-frame-options"]=> string(15) "X-Frame-Options" ["set-cookie"]=> string(10) "Set-Cookie" ["transfer-encoding"]=> string(17) "Transfer-Encoding" ["content-type"]=> string(12) "Content-Type" } ["protocol":"GuzzleHttpPsr7Response":private]=> string(3) "1.1" ["stream":"GuzzleHttpPsr7Response":private]=> object(GuzzleHttpPsr7Stream)#395 (7) { ["stream":"GuzzleHttpPsr7Stream":private]=> resource(328) of type (stream) ["size":"GuzzleHttpPsr7Stream":private]=> NULL ["seekable":"GuzzleHttpPsr7Stream":private]=> bool(true) ["readable":"GuzzleHttpPsr7Stream":private]=> bool(true) ["writable":"GuzzleHttpPsr7Stream":private]=> bool(true) ["uri":"GuzzleHttpPsr7Stream":private]=> string(10) "php://temp" ["customMetadata":"GuzzleHttpPsr7Stream":private]=> array(0) { } } }

当我通过Postman运行HTTP GET时,我得到的结果如下:

"fields":[{"options":[{"id":23034,"value":"Ready for scheduling"}],"fieldDefinitionId":16444,"name":"Job Status"}

我当前获得的$response是什么,如何从外部 API 获取我正在寻找的内容的 JSON 数组响应?

你必须返回一个 Symfony 响应,从 Symfony 3.1 开始,你可以使用 JSON 控制器助手:

return $this->json(json_decode($response->getBody()));

如他们的主页所示,要获得响应body

// 'application/json; charset=utf8'
echo $response->getBody();
// {"type":"User"...'
// to return from controller
return json_decode($response->getBody());

最新更新