我已成功显示有关管理员用户个人资料的信息使用PHP中的Microsoft Graph API的照片,下面是我使用的代码
<?php
session_start();
echo $acc = $_SESSION['access_token'];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://graph.microsoft.com/v1.0/me/photo/$value",
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_HTTPHEADER => array(
"authorization: Bearer $acc",
"accept: application/json",
"content-type: application/json; charset=utf-8"
) ,
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$res = json_decode($response);
$res1 = json_decode($response, true);
$res1['@odata.context'];
echo 'photo <img src="data:image/jpeg;base64,' . base64_encode($res1['@odata.context']) . '"/>';
echo '<p>photo Name : ' . $res1['@odata.context'] . '</p>
<img src="' . $res1['@odata.context'] . '"><br />';
echo '<pre>' . print_r($response, true) . '</pre>';
if ($err)
{
echo "cURL Error #:" . $err;
}
else
{
}
?>
通过成功通话返回标题:
HTTP/1.1 200 OK
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false;charset=utf-8
Content-Encoding: gzip
Vary: Accept-Encoding
request-id: dc3a8a0c-0ff1-455f-afe8-5cdeb6b70a1b
client-request-id: dc3a8a0c-0ff1-455f-afe8-5cdeb6b70a1b
x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"North Central US","Slice":"SliceA","Ring":"3","ScaleUnit":"002","Host":"AGSFE_IN_14","ADSiteName":"CHI"}}
OData-Version: 4.0
Duration: 121.1991
Date: Sun, 03 Dec 2017 10:21:55 GMT
JSON返回:
{
"@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('2b7f8bf2-3d23-43e6-b068-39aa5009fed7')/photo/$entity",
"@odata.mediaContentType": "image/jpeg",
"@odata.mediaEtag": "\"10F9EA34\"",
"id": "504X504",
"height": 504,
"width": 504
}
我的问题是当我试图在php中的图像链接中显示图像时 根据php中的json Curl结果,它确实显示了图像。
如何将二进制返回的图像数据转换为浏览器图像可读格式
Below is the new error message after implementing Marc Solutions
CURLOPT_HTTPHEADER => array(
"authorization: Bearer $acc",
"accept: image/jpeg",
"content-type: application/json; charset=utf-8"
)
HTTP/1.1 400 Bad Request
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: application/json
request-id: 93186bcd-05ad-41b5-bafe-8f5c4d7a3952
client-request-id: 93186bcd-05ad-41b5-bafe-8f5c4d7a3952
x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"North Central US","Slice":"SliceA","Ring":"3","ScaleUnit":"001","Host":"AGSFE_IN_14","ADSiteName":"CHI"}}
OData-Version: 4.0
Duration: 104.1582
Date: Sun, 03 Dec 2017 16:09:14 GMT
{
"error": {
"code": "BadRequest",
"message": "A supported MIME type could not be found that matches the acceptable MIME types for the request. The supported type(s) 'application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=false, application/json;odata.metadata=minimal;odata.streaming=true;IEEE754Compatible=true, application/json;odata.metadata=minimal;odata.streaming=true, application/json;odata.metadata=minimal;odata.streaming=false;IEEE754Compatible=false, application/json;odata.metadata=minimal;odata.streaming=false;IEEE754Compatible=true, application/json;odata.metadata=minimal;odata.streaming=false, application/json;odata.metadata=minimal;IEEE754Compatible=false, application/json;odata.metadata=minimal;IEEE754Compatible=true, application/json;odata.metadata=minimal, application/json;odata.metadata=full;odata.streaming=true;IEEE754Compatible=false, application/json;odata.metadata=full;odata.streaming=true;IEEE754Compatible=true, application/json;odata.metadata=full;odata.streaming=true, application/json;odata.metadata=full;odata.streaming=false;IEEE754Compatible=false, application/json;odata.metadata=full;odata.streaming=false;IEEE754Compatib...' do not match any of the acceptable MIME types 'image/jpeg'.",
"innerError": {
"request-id": "93186bcd-05ad-41b5-bafe-8f5c4d7a3952",
"date": "2017-12-03T16:09:14"
}
}
}
答案 0 :(得分:1)
$value
参数用于请求实际图像。然而,您的电话请求的不是URL编码$
,而且看起来PHP / Curl只是忽略了结果。
请尝试使用%24value
的URL编码版本。
CURLOPT_URL => "https://graph.microsoft.com/v1.0/me/photo/%24value",
答案 1 :(得分:0)
为了获取图像,您需要使用$ value:
{{1}}