如何将https://graph.microsoft.com/v1.0/me/photo/$value响应转换为图像

时间:2018-02-28 16:50:53

标签: php curl microsoft-graph impresspages

我试图在php网站上显示Microsoft Graph图像。我从图表中收到图片,但格式不正确。

结果如����JFIF``��C等等

问题是我收到一张图片而不是一张json。但是我无法使用上面的代码显示图像。

> HTTP/1.1 200 OK
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: image/jpeg
ETag: "BB3972DD"
request-id: 1d3edddb-4278-43ac-be53-4a60861c7703
client-request-id: 1d3edddb-4278-43ac-be53-4a60861c7703
x-ms-ags-diagnostic: {"ServerInfo":{"DataCenter":"North Europe","Slice":"SliceA","Ring":"3","ScaleUnit":"002","Host":"AGSFE_IN_2","ADSiteName":"DUB"}}
Duration: 176.1934
Date: Wed, 28 Feb 2018 18:35:52 GMT
����JFIF``��C��C����"�� 
���}!1A

感谢这个社区的帮助,我能够解决我的问题。我的工作代码是:

$ch = curl_init();
    curl_setopt_array($ch, array(
        CURLOPT_URL => 'https://graph.microsoft.com/v1.0/me/photo/%24value',
        CURLOPT_HEADER => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => null,
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_HTTPHEADER => array(
            'authorization: Bearer '.ipDb()->selectValue('user','hash',array('id' => ipUser()->userId())),
            'content-type: image/jpeg; charset=utf-8'
        ) ,
    ));
    $response = curl_exec($ch);
    // Close the cURL resource, and free     system resources
    curl_close($ch);
    $returndata = 'photo <img src="data:image/jpeg;base64,' . base64_encode($response) . '"/>';
    return $returndata;

1 个答案:

答案 0 :(得分:1)

由于$ value URL返回图像二进制文件,行

$res1['@odata.context']

不应直接使用$response,而应使用base64_encode($response)

所以{{1}}应该是img src标签的最后一部分。