I have done a few Microsoft Graph API requests, but solely using cURL
. Functionally this works perfectly fine, but periodically and frustrating often I do get a cURL response code 0
back, “Connection was reset
“. I cannot figure out how to fix this, and expect it to be a connectivity issue between my server and the API, but I cannot get any help from our IT as they just state “this is working fine from our end” (oh just great, thanks a lot).
Then I considered using the Microsoft Graph AP PHP SDK, to see if this is any better. Sadly my Windows server does not have Composer
available, and all examples I can see is requiring this.
So basically I am asking kindly here, if anyone would have any help how-to implement this SDK, when I do not have “Composer” avialable?
Just to give some context, then this is my currently well-working cURL
implementation, which I now want to try to convert to the new PHP SDK solution:
<?PHP
# --------------------------------------------------------------------------------------------------
# Get a new Microsoft Graph API token
$tokenUrl = "https://login.microsoftonline.com/$azureTenantId/oauth2/v2.0/token";
$tokenParams = array(
"grant_type" => "client_credentials",
"client_id" => $azureClientId,
"client_secret" => $azureClientSecret,
"scope" => "https://graph.microsoft.com/.default",
);
# Do a cURL request with the above parameters
$ch = curl_init($tokenUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_CAINFO, $msGraphApiCertRootPem);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($tokenParams));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
# Get the cURL result and the HTTP code
$response = curl_exec($ch); // do not close the cURL instance as we need one more request
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
# Get the cURL data into an array
$tokenData = json_decode($response,true);
# Debug only - output the data
echo "<pre>";
echo "<b>Get the Azure access token</b><br><br>";
echo "tokenData=<br>";
print_r($tokenData);
echo "<br><br>";
echo "httpCode=$httpCode<br>";
echo "<br>------<br><br>";
# --------------------------------------------------------------------------------------------------
# Fetch a new binary/image
if (isset($tokenData["access_token"])) {
# Define the Graph API end-point
$url = "https://graph.microsoft.com/v1.0/users/[email protected]/photo/$value";
# Do a cURL request with the specified URL and the authorization token
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CAINFO, $msGraphApiCertRootPem);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer ".$tokenData["access_token"],
));
# Get the cURL result and the HTTP code
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
# NOW we can close the cURL connection - no more need for it :-)
curl_close($ch);
echo "<b>Get and show the user picture from Azure</b><br><br>";
echo "httpCode=$httpCode<br><br>";
# Display the user picture or else show the error
if($httpCode == 200) {
echo "Picture:<br>";
$pictureRaw = $response;
$pictureEncoded = base64_encode($pictureRaw);
echo "<img src='data:image/jpeg;base64,$pictureEncoded' /><br>";
} else {
echo "Error:<br>";
var_dump($response);
}
}
?>