使用Symfony 3项目从外部API获取Guzzle HTTP GET的意外$响应

时间:2017-01-24 21:58:46

标签: php json symfony guzzle

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

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Client;

class ScheduleController extends Controller {

/**
 * @Route("/schedule")
 */

public function getJobs() {
    // Create a client with a base URI
    $client = new \GuzzleHttp\Client(['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(GuzzleHttp\Psr7\Response)#397 (6) { ["reasonPhrase":"GuzzleHttp\Psr7\Response":private]=> string(2) "OK" ["statusCode":"GuzzleHttp\Psr7\Response":private]=> int(200) ["headers":"GuzzleHttp\Psr7\Response":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":"GuzzleHttp\Psr7\Response":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":"GuzzleHttp\Psr7\Response":private]=> string(3) "1.1" ["stream":"GuzzleHttp\Psr7\Response":private]=> object(GuzzleHttp\Psr7\Stream)#395 (7) { ["stream":"GuzzleHttp\Psr7\Stream":private]=> resource(328) 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) { } } }

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

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

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

2 个答案:

答案 0 :(得分:1)

homepage所示,要获得回复body

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

// to return from controller
return json_decode($response->getBody());

答案 1 :(得分:1)

您必须返回Symfony响应,从Symfony 3.1开始,您可以使用the json controller helper

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