【问题标题】:PHP Guzzle Get Request FormatPHP Guzzle 获取请求格式
【发布时间】:2019-04-03 01:47:55
【问题描述】:

我正在尝试使用 Guzzle 获取 JSON 提要,这是我的第一次。

给我的身体样本是:

{
    "auth": {
        "key": "samplekeyhere",
        "secret": "samplesecrethere"
    }
}

这是我的代码:

    $res = $client->request(
                'GET',
                'https://urltofeed',
                [
                    "verify" => false, // JUST FOR TESTING NOW
                    "headers" => [
                        "Content-Type" => "application/json;charset=UTF-8"
                    ],
                    "json" => [
                        "key" => "samplekeyhere",
                        "secret" => "samplesecrethere"
                    ]
                ]
    );
    $body = $res->getBody();

$jsonResponse = json_decode($body);
var_dump($jsonResponse);

输出是:

status code is200object(stdClass)#2455 (2) { ["success"]=> bool(false) ["msg"]=> string(7) "Error 1" }

我的语法有问题还是我应该要求新的密钥和秘密?

【问题讨论】:

    标签: json guzzle


    【解决方案1】:
    $jsonResponse = json_decode($body->getContents());
    

    因为body是StreamInterface的一个实例,而不是一个简单的字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-16
      • 2015-12-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多