Hey, Folks!
In this blog post, we’re going to learn List Management in Outlook account by importing Microsoft live contacts.
Microsoft has a centralized system. They have a dedicated service for each feature. For example, PEOPLE service for Contacts, Mail for emailing etc. It doesn’t matter which kind of microsoft account you are logged in, Hotmail, Outlook, Live . All services will get redirected to people when using contacts and redirected to mail when using mail.
You can also export contacts from outlook in a CSV file with the same procedure. Take a sneak peek at our blog Export Outlook Contacts. Let’s begin.


Watch the live demo or download code from the link given below
Insert Contacts In Outlook Using Php Demo


Take a sneak peek on our premium service MailGet for the entire solution.


Process:

Let’s take a look at the process step by step.

  1. The user will get a contact form on the index page.
  2. The user will be asked to enter his First Name, Last Name, and Email Address.
  3. After submitting the above information, the user will be asked to log into Microsoft account in which he wants to add the contact.
  4. Then, Microsoft will ask the user for permission to read/write contacts from/to his account.
  5. And at last, contact will get added in his address book.
  6. After this, he can get back to the first page with the back button, or he can log out from logout button.

So, above is the complete process to add contacts in Microsoft.

To obtain Client ID & Client Secret, you can take a reference about registering and creating application in Microsoft from our previous blog Export Outlook Contacts.


 PHP File: index.php

<html>
<head>
<title>Insert Contacts in Outlook Using PHP</title>
<link href="bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<script>
function call(){
var fn = document.getElementById("fn").value;
var ln = document.getElementById("ln").value;
var email = document.getElementById("email").value;
var url_values = '?fn='+fn+'%26ln='+ln+'%26email='+email;
var urls= 'https://login.live.com/oauth20_authorize.srf?client_id=000000004816A327&scope=wl.signin%20wl.basic%20wl.emails%20wl.contacts_emails%20wl.contacts_create&response_type=code&redirect_uri=https://www.formget.com/tutorial/insert-contacts-in-outlook-using-php/oauth-hotmail.php'+url_values;
window.location.replace(urls);
}
</script>
</head>
<body>
<div class="container-fluid">
<h1>Insert Contacts in Outlook Using PHP</h1>
<div id="login">
<div id="h2" class="h2 row">
<div class="col-md-12"><h2><span>Insert Contact in <img src="images/microsoft.png"/></span></h2></div>
</div>
<div class="row">
<div id="indexform" class="col-md-12">
<?php
$client_id = 'Insert client id';
$client_secret = 'Insert client secret';
$redirect_uri = 'https://www.formget.com/tutorial/insert-contacts-in-outlook-using-php/oauth-hotmail.php';
$urls_ = 'https://login.live.com/oauth20_authorize.srf?client_id='.$client_id.'&scope=wl.signin%20wl.basic%20wl.emails%20wl.contacts_emails%20wl.contacts_create&response_type=code&redirect_uri='.$redirect_uri."?";
?>
<input id="fn" type="text" placeholder="First Name" name="name"/>
<input id="ln" type="text" placeholder="Last Name" name="lname"/>
<input id="email" type="email" placeholder="Contact Email" name="email"/>
<button type="button" class="btn btn-primary btn-block" id="submit" onclick="call()">Add Contact</button>
</div>
</div>
</div>
</div>
</body>
</html>

PHP File: oauth_hotmail.php

//function for parsing the curl request
session_start();
$data = $_GET;
$data1 = "fn=".$_GET['fn']."&ln=".$_GET['ln']."&email=".$_GET['email'];
$client_id = 'ENTER CLIENT ID';
$client_secret = 'ENTER CLIENT SECRET';
$redirect_uri = 'https://www.formget.com/tutorial/insert-contacts-in-outlook-using-php/oauth-hotmail.php?'.$data1;
$urls_ = 'https://login.live.com/oauth20_authorize.srf?client_id='.$client_id.'&scope=wl.signin%20wl.basic%20wl.emails%20wl.contacts_emails%20wl.contacts_create&response_type=code&redirect_uri='.$redirect_uri;
$auth_code = $data["code"];
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($client_id),
'client_secret'=> urlencode($client_secret),
'scope' => 'wl.signin%20wl.basic%20wl.emails%20wl.contacts_emails%20wl.contacts_create',
'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.live.com/oauth20_token.srf');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
$result = curl_exec($curl);
curl_close($curl);

$response = json_decode($result);
if(isset($response->access_token)){
$_SESSION['access_token'] = $response->access_token;
$accesstoken = $_SESSION['access_token'];
}
if(isset($_GET['code']))
{
$accesstoken = $_SESSION['access_token'];
}
$api_url = "https://apis.live.net/v5.0/me/contacts?access_token='.$accesstoken.";
$curl = curl_init($api_url);
$curl_data = array(
'first_name' => $data['fn'],
'last_name' => $data['ln'],
'emails' => array(
'personal' => $data['email'],
'account' =>'personal'
)
);
$curl_data = json_encode($curl_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
$curl_response = curl_exec($curl);
?>

<html>
<head>
<title>Insert Contacts in Outlook Using PHP</title>
<link href="bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
<script src="js/logout.js" type="text/javascript"></script>
</head>
<body>
<div class="container-fluid">
<h1>Insert Contacts in Outlook Using PHP</h1>
<div id="login">
<div id="h2" class="h2 row">
<div class="col-md-2"><a href="http://formget.com/tutorial/insert-contacts-in-outlook-using-php/index.php"><img id="yprevious" src="images/previous.png"/></a></div>
<div class="col-md-8"><h2><span>Insert Contact in <a><img src="//d13dcw7qo4xgly.cloudfront.net/tutorial/insert-contacts-in-outlook-using-php/images/microsoft.png"/></a></span></h2></div>
<div class="col-md-2"><a href="#" onclick="caller()"><img id="ylogout" src="images/logout.png"/></a></div>
</div>
<div class="row">
<div class="col-md-12">
<p>Contact Added</p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p id="note">Note : If you want to export your Hotmail/Live/Outlook contacts, then you can use <a href="https://www.formget.com/tutorial/export-hotmail-contacts/">Export Outlook Contacts</a></p>
</div>
</div>
</div>
</body>
</html>

CSS File: style.css

@import url(http://fonts.googleapis.com/css?family=Raleway);
body{
font-family:raleway !important;
}
#h2{
background-color: #FEFFED;
text-align:center;
border-radius: 10px 10px 0 0;
border-bottom: 1px solid #ccc;
color: black;
font-weight: bolder;
font-size: 2em;
margin: 0px -15px;
padding: 8% 0;
font-family:raleway !important;
}
.wrap{
width: 33%;
margin: 5% auto;
}
.container-fluid{
width: 45%;
margin: auto auto;
}
#login{
border: 2px solid #ccc;
border-radius: 10px;
font-family:raleway!important;
}
h1{
padding: 6% 0;
font-family:raleway!important;
text-align: center;
font-size: 35px !important;
}
.h2{
margin: 0 !important;
padding: 2% 0 !important;
}
img{
padding: 0% 0%;
width: 35%;
}
input[type=text],input[type=email]{
width:99.5%;
padding: 10px;
margin-top: 14px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
font-family:raleway !important;
}
input[type=submit]{
width: 100%;
background-color:#FFBC00;
color: white;
border: 2px solid #FFCB00;
padding: 10px;
font-size:20px;
cursor:pointer;
border-radius: 5px;
margin-top: 14px;
}
p{
text-align: center;
font-family:raleway !important;
font-size:25px;
padding: 15px;
}
#yprevious,#ylogout{
width: 50px;
margin-top: 25%;
}
#logo {
margin-bottom: -2%;
}
#note{
font-size: 20px;
}
#note a{
text-decoration: none;
}
#indexform a#submit{
width: 100% !important;
background-color:#FFBC00;
color: white;
border: 2px solid #FFCB00;
padding: 10px 39%;
font-size:20px;
cursor:pointer;
border-radius: 5px;
margin-top: 14px;
margin: 0 1.5%;
}
#indexform,#email{
margin-bottom:25px;
}

JavaScript File: Logout.js

function call()
{
popup = window.open('https://login.live.com/logout.srf?ct=1441367969&rver=6.4.6456.0&lc=1033&id=64855&ru=https:%2F%2Fbay172.mail.live.com%2Fhandlers%2FSignout.mvc%3Fservice%3DLive.Mail%26mkt%3Den-in&mkt=en-in');
setTimeout(wait, 4000);
}
function caller()
{
call();
}
function wait()
{
popup.close();
window.location.href = 'http://formget.com/tutorial/insert-contacts-in-outlook-using-php/index.php';
}

 Explaination of the main file : oauth_hotmail.php

First step is to start the session

session_start();

Then we need to get the data travelling in the query string with the global variable $_GET.

$data = $_GET;
$data1 = "fn=".$_GET['fn']."&ln=".$_GET['ln']."&email=".$_GET['email'];

Now we need to set the client credentials, redirect URL and Login URL. Login URL must be exactly same as you have defined in the index.php file.

$client_id = 'ENTER CLIENT ID';
$client_secret = 'ENTER CLIENT SECRET';
$redirect_uri = 'https://www.formget.com/tutorial/insert-contacts-in-outlook-using-php/oauth-hotmail.php?'.$data1;
$urls_ = 'https://login.live.com/oauth20_authorize.srf?client_id='.$client_id.'&scope=wl.signin%20wl.basic%20wl.emails%20wl.contacts_emails%20wl.contacts_create&response_type=code&redirect_uri='.$redirect_uri;

Below line is to set the authentication code.

$auth_code = $_GET["code"];

Below code is to set and encode the parameters which are going to pass in URL.

$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($client_id),
'client_secret'=> urlencode($client_secret),
'scope' => 'wl.signin%20wl.basic%20wl.emails%20wl.contacts_emails%20wl.contacts_create',
'redirect_uri'=> urlencode($redirect_uri),
'grant_type'=> urlencode('authorization_code')
); 

Below code is building url which is going to append on the login URL.

foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; }
$post = rtrim($post,'&');

In below code, we’re using curl to reach the login URL.

$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://login.live.com/oauth20_token.srf');
curl_setopt($curl,CURLOPT_POST,5);
curl_setopt($curl,CURLOPT_POSTFIELDS,$post);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
$result = curl_exec($curl);
curl_close($curl);

Below line is collecting response of the curl call in the $response variable

$response = json_decode($result);

Below code is to check whether access token is set or not. If it is set then it will be stored in a SESSION variable to reuse it for refresh token.

if(isset($response->access_token)){
$_SESSION['access_token'] = $response->access_token;
$accesstoken = $_SESSION['access_token'];
}

Below code is to set refresh token. It will check if the code is set in the URL, then the access token we stored in SESSION variable will get stored in access token.

if(isset($_GET['code']))
{
$accesstoken = $_SESSION['access_token'];
}

Below is the URL responsible for writing contact into microsoft account.

$api_url = "https://apis.live.net/v5.0/me/contacts?access_token='.$accesstoken.";
$curl = curl_init($api_url);

The code below is responsible for inserting contacts into microsoft.

$curl_data = array(
'first_name' => $data['fn'],
'last_name' => $data['ln'],
'emails' => array(
'personal' => $data['email'],
'account' =>'personal'
)
);

The line below is use to encode the contact information into json.

$curl_data = json_encode($curl_data);

The code below is setting curl options and executing it.

curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);
$curl_response = curl_exec($curl);

Conclusion:

I hope that at this point you must be feeling comfortable with importing Outlook contacts in PHP. Please comment for any query. Keep visiting our website.