简体   繁体   中英

PHP not getting Guzzle response as expected

I'm using Guzzle for HTTP Requests/Responses in my PHP project.

I'm sending the following request :

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

which, in Postman, returns something that looks like this :

{
    "@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": "..."
        }
    }
}

So I build my request like this :

$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());
}

But when I var_dump($eventInfos), I get a GuzzleHttp\\Psr7\\Request object.

What is the correct way to get the JSON I was expecting please ?

You have to extract the body of from the response. Try this,

$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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM