Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created September 17, 2019 14:29
Show Gist options
  • Save phpfiddle/ee8468f73b2f5a9031ff18d34f9f9dc9 to your computer and use it in GitHub Desktop.
Save phpfiddle/ee8468f73b2f5a9031ff18d34f9f9dc9 to your computer and use it in GitHub Desktop.
[ Posted by Muhammad Ahmad ] application_test
<?php
$redirect_uri = 'http://localhost/myapp/';
$client_id = 'f9d70f3f-08c1-4df3-9f9a-f3ce24b572a3';
$client_secret = 'XzB@RgTO3CYUO80Si-8gdlD@g[4]IW[c';
$auth_code = $_GET['code'];
$scope="https://graph.microsoft.com/Contacts.ReadWrite";
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($client_id),
'client_secret'=> urlencode($client_secret),
// 'scope' => 'offline_access%20user.read%20mail.read%20contacts.readwrite',
'scope' => urlencode($scope),
'redirect_uri'=> urlencode($redirect_uri),
'grant_type'=> urlencode('authorization_code'),
);
$post = '';
foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://login.microsoftonline.com/common/oauth2/v2.0/token');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Content-Type: application/x-www-form-urlencoded',
// 'Host: https://login.microsoftonline.com'
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
$result = curl_exec($curl);
curl_close($curl);
$response = json_decode($result);
// echo "<pre>";
// print_r($response);
// die();
$token = $response->access_token;
//setup the request, you can also use CURLOPT_URL
$emailAddresses=array(
'address' =>"[email protected]" ,
'name' =>"Pavel Bansky",
);
$fields2=array(
'givenName'=> "Ahmadlatif",
'surname'=> "Bansky",
'emailAddresses'=> $emailAddresses,
'businessPhones' =>["+1 732 555 0102"],
);
$data_string = json_encode($fields2);
$newdata='{
"requests": [
{
"id": "1",
"method": "POST",
"url": "/me/contacts",
"headers":{
"Content-Type":"application/json"
},
"body": {
"emailAddresses": [
{
"name": "John Doe",
"address": "[email protected]"
}
],
"givenName": "John"
}
},
{
"id": "2",
"method": "POST",
"url": "/me/contacts",
"headers":{
"Content-Type":"application/json"
},
"body": {
"emailAddresses": [
{
"name": "John Doe",
"address": "[email protected]"
}
],
"givenName": "John"
}
},
{
"id": "3",
"method": "POST",
"url": "/me/contacts",
"headers":{
"Content-Type":"application/json"
},
"body": {
"emailAddresses": [
{
"name": "John Doe",
"address": "[email protected]"
}
],
"givenName": "John"
}
}
]
}';
// $curlcall="https://graph.microsoft.com/v1.0/" + $newdata;
$ch = curl_init("https://graph.microsoft.com/v1.0/");
//attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $newdata );
//Set your auth headers
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Host: graph.microsoft.com',
'Authorization: Bearer ' . $token
));
//return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute the POST request
$result = curl_exec($ch);
$response2 = json_decode($result);
echo "<pre>";
print_r($response2);
//close cURL resource
curl_close($ch);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment