从Microsoft Graph获取个人资料图片

时间:2017-12-13 14:10:59

标签: laravel microsoft-graph guzzle

我正在尝试使用Microsoft Graph获取当前用户的个人资料图片。我正在使用msgraph-sdk-php

下面的代码获取照片,但返回jpeg文件的二进制数据。

if (session_status() == PHP_SESSION_NONE)
        session_start();

    $graph = new Graph();
    $graph->setAccessToken($_SESSION['access_token']);

    $photo = $graph->createRequest("GET", "/me/photo/\$value")
                           ->execute();

    return $photo->getRawBody();

It seems我需要在以更常规的方式使用图像之前将响应类型设置为blob,但是如何使用Guzzle进行此操作?

我也用cUrl尝试过,但同样的问题,我得到的只是二进制数据:

$curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => "https://graph.microsoft.com/v1.0/me/photos/48x48/\$value",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => array(
            "authorization: Bearer $at",
            "cache-control: no-cache",
            "Content-type: image/jpeg",
            "Accept: blob",
            "postman-token: caccedb3-8253-e6aa-7e30-25052bc28f2f"
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        return $response;
    }

2 个答案:

答案 0 :(得分:0)

假设您正在使用Laravel(您标记​​的)

Content-Type标头添加到您的回复中,以便浏览器了解它的数据类型:

return response($response)
        ->header('Content-Type', 'image/jpeg');

答案 1 :(得分:0)

好吧终于找到了:

curl_setopt_array($curl, array(
        CURLOPT_URL => "https://graph.microsoft.com/v1.0/me/photos/48x48/\$value",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => array(
            "authorization: Bearer $at",
            "cache-control: no-cache",
            "postman-token: 2d4b85a3-5490-3f58-ff74-52e0a98286ec"
        ),
    ));

    $response = curl_exec($curl);
    $err = curl_error($curl);

    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        return base64_encode($response);
    }

在模板中:

<img src="data:image/jpeg;base64,{{\O365\Profile::photo()}}"/>