image URL of a signed-in user using Microsoft Graph

大兔子大兔子 提交于 2020-07-09 11:51:28

问题


I've been trying to hard to get the image URL of a signed-in user using Microsoft Graph

<?php
$ch = curl_init();
curl_setopt_array($ch, array(
    CURLOPT_URL => "https://graph.microsoft.com/beta/me/photo/$value",
    CURLOPT_HEADER => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_ENCODING => null,
    CURLOPT_MAXREDIRS => 10,
    CURLOPT_BINARYTRANSFER => true,
    CURLOPT_TIMEOUT => 30,
    CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
    CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11",
    CURLOPT_HTTPHEADER => array(
        'authorization: Bearer EwBgA8l6BAAURSN/FHlDW5xN74t6GzbtsBBeBUYAATuF8OsoPq1qA+OJW6vNZu1QD6xsDHby7zwXDFBo7I2An/Y4J49AuEtWvb8v1yKDL+Kf1/iyZJ72jbo0wrDqB7Soxxzj14DBjDnHWLuxkrSLLhFcRNHyITxqbiHIRPI+3j0MpxlLiuIoSCxy+Y4fQqP7vJXolCwsyPCiAKAMJ8huEJYMr9Pd+npAur+q7S/fkoUQsxxtiJaipcaWIt8yzKhajqb7rcPMnoODK9wo/K8oFUGhIYvwhhW83/rp+IwIbwzU6RcWdeD7QyIAxmOb646GKrRO6M/pR9/sPqahitIuU1MPBD9GjLEz2F+orQGNeHhrdeX8BKyQSMvfhq7I62oDZgAACOzq1UE5lZRWMAKIXRflLOb0qnrJdEzwGUpMieK3rFjEhzsWQhy0+Rtss5Ch34z5chAevB4bqlJ0DKpm9IcpbDy75KuAwoR6kodRCyWO8NZrMYz/JGVnw19LTK3/lxYVdc9fRBwj6VtuB24+82T+TBnBIaaccy/g0vlhTIYzSTT5hA3EAL4GfLJ/PwhsJhg/1KH54bvT1TTaIOT7a56SUDOCPbcG2Mgdrviu/SQpfJtKDRjdvw70awc+xhAKa3M/99TuAKysiGUme/NZbkuxw1zfS7yydEh1C4M/uN81NNJQmPA5RKZbnH9AMJ/LAWF+uAetacAbz6xidnT/iI30GWpBCcV86wbTDJoSAuzbqtHQtnYC1sLBOhqqxucU8J/THkYNaJ9EZZlccWOQKbifjkpWPUAscFj+1ZlKw4v6BNFTSLf6nE8Htc5sKqz4eC6V3+faJs6gkz406Prf1jkAsvi+5VxkiusN0SncVU0usNEKGD8YymkXvJvr1i2KKbGMZAaMKYfO3GE+9lIMZj9+LZ94brIse9/ITfRLEHvx36v50r0Lr8NeNR/iEQKRjoRygkGXVQpmtgeBcO+LFVDPUIEkI29AI/jw1VkgHoWIZgszxi8x0YTYd7CY95yEXk+67ZrraIgPstPhZOMxRMHONQe3lQ440I4y15xRaFVdxxgApMxXWVpbrofiePCGiyvB1lok+T3VAEAsdld78uVgJituXZNbHvXAsig7lzQU0PmCICTUMf+kQd1RXWgC',
        'Content-Type: image/jpg'
    ) ,
));
$response = curl_exec($ch);
curl_close($ch);
echo $response;

?>

Below is my result

{"@odata.context":"https://graph.microsoft.com/beta/$metadata#users('kayu2kshow%40hotmail.com')/photo/$entity","@odata.mediaContentType":"image/jpeg","width":96,"height":96,"id":"96X96"}

Please, how may I get the image displayed on the web page?


回答1:


It seems that you are not calling

 https://graph.microsoft.com/beta/me/photo/$value

but instead

 https://graph.microsoft.com/beta/me/photo/  

and thus receiving only the metadata of the profile image.

PHP will try to interpret $value in your string as an php-variable. As $value is not set nothing is concated to the string. To tell the interpreter that you do not want to treat $value as a variable need to escape the $ character.

Try changing your line to:

 CURLOPT_URL => "https://graph.microsoft.com/beta/me/photo/\$value"

then the correct request is send and you will receive the image data (base64 encoded)

To display an Base64 encoded image you can use the following html code:

<img src="data:image/jpeg;base64, {insertBase64EncodedData}"/>


来源:https://stackoverflow.com/questions/51491295/image-url-of-a-signed-in-user-using-microsoft-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!