【问题标题】:PHP not getting Guzzle response as expectedPHP没有按预期得到Guzzle响应
【发布时间】:2018-11-23 23:56:41
【问题描述】:

我在我的 PHP 项目中使用 Guzzle 处理 HTTP 请求/响应。

我正在发送以下请求:

GET https://graph.microsoft.com/v1.0/me/events('[some_id]')

在 Postman 中,它返回如下所示的内容:

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('...')/events/$entity",
    "@odata.etag": "W/\"...==\"",
    "id": "...",
    "createdDateTime": "2018-06-14T08:03:44.5688916Z",
    "lastModifiedDateTime": "2018-06-14T08:03:44.7407671Z",
    "changeKey": "...==",
    "categories": [],
    "originalStartTimeZone": "UTC",
    "originalEndTimeZone": "UTC",
    "iCalUId": "...",
    "reminderMinutesBeforeStart": 15,
    "isReminderOn": true,
    "hasAttachments": false,
    "subject": "Created ?",
    "bodyPreview": "",
    "importance": "normal",
    "sensitivity": "normal",
    "isAllDay": false,
    "isCancelled": false,
    "isOrganizer": true,
    "responseRequested": true,
    "seriesMasterId": null,
    "showAs": "busy",
    "type": "singleInstance",
    "webLink": "https://outlook.office365.com/owa/?itemid=...%3D&exvsurl=1&path=/calendar/item",
    "onlineMeetingUrl": null,
    "recurrence": null,
    "responseStatus": {
        "response": "organizer",
        "time": "0001-01-01T00:00:00Z"
    },
    "body": {
        "contentType": "html",
        "content": ""
    },
    "start": {
        "dateTime": "2018-06-15T10:00:00.0000000",
        "timeZone": "UTC"
    },
    "end": {
        "dateTime": "2018-06-15T13:30:00.0000000",
        "timeZone": "UTC"
    },
    "location": {
        "displayName": "",
        "locationType": "default",
        "uniqueIdType": "unknown",
        "address": {},
        "coordinates": {}
    },
    "locations": [],
    "attendees": [],
    "organizer": {
        "emailAddress": {
            "name": "...",
            "address": "..."
        }
    }
}

所以我这样构建我的请求:

$client = new Client();

$header = array(
                "Authorization" => "Bearer ".$token
);

$url = "https://graph.microsoft.com/v1.0/me/events('" .$idEvent. "')";

$request = new Request("GET", $url, $header, "");

try {
     $eventInfos = $client->send($request);
}
catch (GuzzleException $e) {
     var_dump($e->getMessage());
}

但是当我 var_dump($eventInfos) 时,我得到一个 GuzzleHttp\Psr7\Request 对象。

请问获取 JSON 的正确方法是什么?

【问题讨论】:

  • 试试$eventInfos->getBody()
  • 发送请求后在try块中打印出$eventInfos->getBody()
  • 感谢您的回答 Alex,我已经在下面的评论中回答了 Abhishek :)

标签: php guzzle


【解决方案1】:

您必须从响应中提取正文。试试这个,

$client = new Client();

$header = array(
                "Authorization" => "Bearer ".$token
);

$url = "https://graph.microsoft.com/v1.0/me/events('" .$idEvent. "')";

$request = new Request("GET", $url, $header, "");

try {
     $eventInfos = $client->send($request);
     $response = (string)$eventInfos->getBody();
}
catch (GuzzleException $e) {
     var_dump($e->getMessage());
}

【讨论】:

  • 感谢您的回答!但我已经尝试过了,我得到了这个: object(GuzzleHttp\Psr7\Stream)#1960 (7) { ["stream":"GuzzleHttp\Psr7\Stream":private]=> resource(1609) 类型 (流)["大小":"GuzzleHttp\Psr7\Stream":private]=> NULL ["seekable":"GuzzleHttp\Psr7\Stream":private]=> bool(true) ["可读":"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]=> 数组(0) { } }
  • @ThibautTang 已更新答案,Guzzle 将响应正文存储在 Stream 中,因此您需要将其类型转换为字符串
  • 与(字符串)演员一起工作!非常感谢:)
  • @ThibautTang 太棒了!!只需接受答案并快乐编码
  • 嘿@ThibautTang 你仍然可以这样做json_decode($res->getBody()->getContents(),true)
【解决方案2】:

另外,您可以使用getContents() 来获取响应。

$request->getBody()->getContents()

【讨论】:

    猜你喜欢
    • 2014-05-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    • 2020-09-28
    • 2021-06-30
    • 2016-02-27
    • 2019-11-26
    • 1970-01-01
    相关资源
    最近更新 更多