PHP没有得到预期的强烈响应
我在PHP项目中使用Guzzle进行HTTP请求/响应 我发送以下请求:PHP没有得到预期的强烈响应,php,guzzle,Php,Guzzle,我在PHP项目中使用Guzzle进行HTTP请求/响应 我发送以下请求: GET https://graph.microsoft.com/v1.0/me/events('[some_id]') 在《邮递员》中,它返回如下内容: { "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('...')/events/$entity", "@odata.etag": "W/\"...==\"",
GET https://graph.microsoft.com/v1.0/me/events('[some_id]')
在《邮递员》中,它返回如下内容:
{
"@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());
}
因此,我构建了如下请求:
{
"@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的正确方法是什么 您必须从响应中提取主体。试试这个
$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());
}
此外,您还可以使用
getContents()
获取响应
$request->getBody()->getContents()
在
try
block print out$eventInfos->getBody()
中尝试$eventInfos->getBody()
发送请求后谢谢你的回答Alex,我在下面的评论中回答了Abhishek:)谢谢你的回答!但是我已经试过了,我得到了这个:object(GuzzleHttp\Psr7\Stream)#1960(7){[“Stream”:“GuzzleHttp\Psr7\Stream”:private]=>Stream类型的资源(1609)[“size:“GuzzleHttp\Psr7\Stream”:private]=>bool(true)[“可读的”:“GuzzleHttp\Psr7\Stream”:private]=>bool(true)[“可写”:“GuzzleHttp\Psr7\Stream”:private]=>bool(true)[“uri”:“GuzzleHttp\Psr7\Stream”:private]=>string(10)”php://temp“[“customMetadata”:“GuzzleHttp\Psr7\Stream”:private]=>array(0){}@ThibautTang已更新答案,Guzzle将响应正文存储在流中,因此您需要将其类型转换为stringWorks with(string)演员阵容!非常感谢:)@ThibautTang太棒了!!只要接受答案并快乐地编码@ThibautTang你仍然可以通过这种方式来做json\u decode($res->getBody()->getContents(),true)