php - 使用 Symfony 3 项目来自外部 API 的 Guzzle HTTP GET 的意外 $response

标签 php json symfony guzzle

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

<?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-缓存"} ["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; 模式=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"[ “流”:“GuzzleHttp\Psr7\Response”:私有(private)]=>对象(GuzzleHttp\Psr7\Stream)#395(7){[“流”:“GuzzleHttp\Psr7\Stream”:私有(private)]=>资源(328 ) 类型(流)["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 数组响应?

最佳答案

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

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

关于php - 使用 Symfony 3 项目来自外部 API 的 Guzzle HTTP GET 的意外 $response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41839706/

相关文章:

PHP 等待 MYSQL 完成查询

ios - Swift:检查存储在 NSMutableArray 值中的 JSON 值

php - 引用不同实体的建模用户帐户

symfony - Symfony 4:如何组织文件夹结构(即您的业务逻辑)

php - PHP PDO 语句可以接受表名或列名作为参数吗?

php - 使用 Sylius 0.16 和 Payum 实现异地支付网关时如何完成订单

php - 在 PHP 的 json_encode 截断之前转义 JSON 撇号?

java - json序列化时排除字段

php - Symfony 网络套接字 : Logged User always returns anonym within Topic

php - 如何在 "save as"对话框中强制使用正确的文件扩展名? (.jpg 而不是 .php)