本文介绍了如何将 microsoft graph 365 用户个人资料照片从二进制数据转换为可读的图像格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 PHP 中使用 Microsoft Graph API 成功显示了有关管理员用户个人资料照片的信息,下面是我使用的代码

I have successfully displayed informations about admin users profiles Photos using Microsoft Graph API in PHP, below is the code that I used

<?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 结果,它确实显示了图像.

MY problem is that when I tried to display the image in an image link in php as per the json Curl result in php, it does show the image.

如何将二进制返回的图像数据转换为浏览器图像可读格式

How do I convert the binary returned Image data to browser image readable format

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"
    }
  }
}

推荐答案

$value 参数用于请求实际图片.但是,您的调用请求不是对 $ 进行 URL 编码,并且看起来 PHP/Curl 只是忽略了结果.

The $value parameter is used to request the actual image. Your call however is requesting isn't URL Encoding the $ and an it looks like PHP/Curl is just ignoring the result.

尝试改用 %24value 的 URL 编码版本.

Try using the URL Encoded version of %24value instead.

 CURLOPT_URL => "https://graph.microsoft.com/v1.0/me/photo/%24value",

这篇关于如何将 microsoft graph 365 用户个人资料照片从二进制数据转换为可读的图像格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 01:38