Hey, Folks!
In this blog post, we’re going to learn List Management  by exporting Microsoft live contacts in a CSV file.
Microsoft has created a synchronized system. All the contacts saved in any Microsoft service will get save in a single place, i.e., PEOPLE service offered by Microsoft.
It doesn’t matter which service or device you’re using, whether its outlook, Hotmail, windows mobile, desktop, tablet.
If your Microsoft account is associated with them, it will get saved on the cloud on the People’s service.
So, Microsoft has offered a single API to access these contacts.
We’re going to use the same API to export contacts from the Microsoft account.

Let’s begin.


Watch the live demo or download code from the link given below

Export Hotmail Contacts Demo


You can take a look at our premium service MailGet for a quick solution.


Registering Application:

First of all we need to register an application with a microsoft account. You can register your application here.
Let’s take a look at how to register.

After login to Microsoft, you’ll see the following window.

MS Application Page

 

After entering application name. Click on I accept. Your application will get created and following window will get appear.


 

MS Redirection Page

You can see your application’s name. My application’s name is ‘Export Contacts’. Then click on API Settings. You will get following window.


 

MS Set Redirect URL

You need to mention the redirection URL which you’re going to use in your PHP program. Now Click on APP Settings.


 

MS APP Settings
You can get your Client ID & Client Secret from here. This Client ID & Secret will get use in our PHP Program.

So this is how we’re going to register and create application.


Process:

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

  1. The user will be asked to log in to Microsoft account.
  2. The user will be redirected to the Microsoft login page.
  3. After entering the credentials, it will then redirect user to the page where contacts will get displayed.
  4. After this, the user can get download the contacts in the CSV format with the CSV image button, or he can log out from logout button.

So, above is the complete process to export contacts in microsoft.


PHP File: index.php

This is the first page in which user will be asked to sign in to microsoft account.

<?php
$client_id = 'ENTER CLIENT ID';
$client_secret = 'ENTER CLIENT SECRET';
$redirect_uri = 'http://formget.com/tutorial/export-hotmail-contacts/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&response_type=code&redirect_uri='.$redirect_uri;
?>
<html>
<head>
<title>Export Hotmail Contacts Using PHP</title>
<link href="bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css"/>
<link href="css/style.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="container-fluid">
<h1>Export Hotmail Contacts Using PHP</h1>
<div id="login">
<div class="row">
<div class="col-md-12">
<h2 id="h2">Microsoft Sign-in</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<a id="signin" href="<?php echo $urls_; ?>"><img id="signinwithms" class="img-responsive" src="http://formget.com/tutorial/export-hotmail-contacts/images/Microsoft.png"></a>
</div>
</div>
</div>
</div>
</body>
</html>

PHP File: oauth_hotmail.php

This is the main file in which contacts are getting fetched and displayed.

<?php
//function for parsing the curl request
session_start();
$client_id = 'ENTER CLIENT ID';
$client_secret = 'ENTER CLIENT SECRET';
$redirect_uri = 'http://formget.com/tutorial/export-hotmail-contacts/oauth-hotmail.php';
//$redirect_uri = 'http://locahost/export-hotmail-contacts/oauth-hotmail.php';
$auth_code = $_GET["code"];
$fields=array(
'code'=> urlencode($auth_code),
'client_id'=> urlencode($client_id),
'client_secret'=> urlencode($client_secret),
'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'];
}
$url = 'https://apis.live.net/v5.0/me/contacts?access_token='.$accesstoken;
$response = file_get_contents($url);
$response = json_decode($response, true);
$data = $response['data'];
?>

<html>
<head>
<title>Export Hotmail Contacts 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>
<meta name="robots" content="noindex, nofollow">
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-43981329-1']);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="container-fluid">
<h1>Export Hotmail Contacts Using PHP</h1>
<div id="login">
<div id="h2" class="h2 row">
<div class="csv col-md-3"><a href ="http://formget.com/tutorial/export-hotmail-contacts/download.php?<?php echo http_build_query($data); ?>" id="download"><img class="img-responsive" src="images/download-csv-icon.gif"></a></div>
<div class="col-md-6"><h2><span>Hotmail Contacts</span></h2></div>
<div class="col-md-3"><a href ="#" onclick="caller()" id="logout"><img class="img-responsive" src="images/button-power_green.png"></a></div>
</div>
<div class="row">
<div class="col-md-12">
<table cellspacing='0'>
<thead>
<td>Name</td>
<td>Email</td>
</thead>
<?php
foreach ($response['data'] as $emails) {?>
<tr>
<td><?php echo $emails['name']; ?></td>
<td><?php print_r($emails['emails']['preferred']); ?></td>
</tr>
<?php }
?>
</table>
</div>
</div>
</div>
</div>
</body>
</html>

 CSS File: style.css

This is the CSS file for index.php and oauth_hotmail.php

@import url(http://fonts.googleapis.com/css?family=Raleway);

#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;
}
.wrap{
width: 33%;
margin: 5% auto;
}
.container-fluid{
width: 44%;
margin: auto auto;
}
#login{
border: 2px solid #ccc;
border-radius: 10px;
font-family:raleway;
}
#signinwithms{
margin: auto;
width: 50%;
padding: 1% 0;
}
h1{
padding: 6% 0;
font-family:raleway;
text-align: center;
}
.csv{
margin: auto;
}
.h2{
margin: 0 !important;
padding: 2% 0 !important;
}
img{
padding: 10% 25%;
}
table {
font-family: 'Raleway', sans-serif;
font-size:12px;
text-shadow: 1px 1px 0px #fff;
border:#ccc 1px solid;
font-size: 16px;
-moz-border-radius:3px;
-webkit-border-radius:3px;
border-radius:3px;
-moz-box-shadow: 0 1px 2px #d1d1d1;
-webkit-box-shadow: 0 1px 2px #d1d1d1;
box-shadow: 0 1px 2px #d1d1d1;
width: 90%;
margin: 7% auto;
}
table td{
padding: 18px;
text-align: center;
border-bottom: 1px solid #e0e0e0;
border-right: 1px solid #e0e0e0;
}
thead{
font-weight: bold;
}
table tr:hover td {
background: #f2f2f2;
background: -webkit-gradient(linear, left top, left bottom, from(#f2f2f2), to(#f0f0f0));
background: -moz-linear-gradient(top, #f2f2f2, #f0f0f0);
}

 JavaScript File: Logout.js

This is the js file responsible for logout.

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/index.php';
}

 Explanation of the main file : oauth_hotmail.php

First step is to start the session

session_start();

Now we need to set the client credentials.

$client_id = 'ENTER CLIENT ID';
$client_secret = 'ENTER CLIENT SECRET';
$redirect_uri = 'http://formget.com/tutorial/export-hotmail-contacts/oauth-hotmail.php';

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),
'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'];
}

Following code is responsible for fetching the contacts once authorization process get completed.

$url = 'https://apis.live.net/v5.0/me/contacts?access_token='.$accesstoken;
$response = file_get_contents($url);
$response = json_decode($response, true);
$data = $response['data'];

 Conclusion:

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