Hotmail Contact list reader API in php (MSN Oauth)
Hotmail Contact list reader API or MSN Oauth API is used for importing the contact list from MSN account .Now these days it is very important to get the contact list of MSN account for several web applications. How they simply get those Hotmail contact list ? with no other security issue the answer is simple they are using MSN Oauth for reading the Hotmail contact list.
First you have sign up with the MSN account for submitting your application on the Microsoft Server.It take only few seconds for registering your application with MSN Check here
Next you have to provide the return url on the page and get both client id and secret key.
When you signup with MSN then you will get one Client id and Secret key after that you have to create a User interface for the Hotmail contact list reader application create a file with hotmailimport.php and have the following codes.
<?php
//***************************************MSN START********************************
$client_id = 'CLIENT_ID';
$client_secret = 'SECRET_KEY';
$redirect_uri = 'http://www.yourdomain.com/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;
$msn_link = '<a href="'.$urls_.'" >MSN Contacts</a>';
echo $msn_link;
//***************************************MSN ENDS********************************
?>
After creating above file you have to create a call back file like the oauth-hotmail.php and have the following codes.
<?php
//function for parsing the curl request
function curl_file_get_contents($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$client_id = 'CLIENT_ID';
$client_secret = 'SECRET_KEY';
$redirect_uri = 'http://www.yourdomain.com/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);
$accesstoken = $response->access_token;
$url = 'https://apis.live.net/v5.0/me/contacts?access_token='.$accesstoken.'&limit=100';
$xmlresponse = curl_file_get_contents($url);
$xml = json_decode($xmlresponse, true);
$msn_email = "";
foreach($xml['data'] as $emails)
{
// echo $emails['name'];
$email_ids = implode(",",array_unique($emails['emails'])); //will get more email primary,sec etc with comma separate
$msn_email .= "<div><span>".$emails['name']."</span> <span>". rtrim($email_ids,",")."</span></div>";
}
echo $msn_email;
?>
The above result contains decrypted email id from Hotmail Contacts ,By normally they don’t provide decrypted email for that we have to pass additional permission for this actions like
scope=wl.signin%20wl.basic%20wl.emails%20wl.contacts_emails
in the Hotmail Contact list reader MSN Oauth Api call https://login.live.com/oauth20_authorize.srf?client_id….
93 thoughts on “Hotmail Contact list reader API in php (MSN Oauth)”
Hi, the demo is not working “This site can’t be reached” issue. There is also an error the access token isn’t valid. please do help me.
the demo is dead due to my server migration will fix soon.
the code is works fine, if you integrate it properly.
make sure your app domain exactly match site domain even www prefix too.
getting all contacts but i also want to get all profile images of all user with there email ids.any idea how can i get profile_image path in hotmail.
Please check older comments , I already wrote link for the requirements.
following error have been occuring :
We’re unable to complete your request
Microsoft account is experiencing technical problems. Please try again later.
Hi,
I have deleted the app and again create a new one but still there is no luck with this…shall I request you to kindly login and check my app…may be I have missed anything there since its the first time am creating an app.
Hi,
I just found the following settings in my app page…
Mobile or desktop client app:
No
Restrict JWT issuing:
Yes
Are these settings creating any issues???
I am really feeling lost!!!
Please change the app redirect url with www I hanged it from APP but you have to change it from code too.
Hi, thanks for your effort you are doing for me…I have changed the redirected url in my php files also…but again that same error!!!
Hi,
I have downloaded your script and change the client id and client secret both in index.php and outh-hotmail.php files. But while clicking on Contact From MSN am getting error
We’re unable to complete your request
Microsoft account is experiencing technical problems. Please try again later.
No other errors!!!
Should I have to wait 24 hours to activate the client secret???
Ok, that error is resolved but now am getting the following error
PHP Warning: Invalid argument supplied for foreach() in C:\Inetpub\vhosts\test.multicon.in\httpdocs\hotmail\oauth-hotmail.php on line 44
Can you please let me know whats wrong!!!You can test my app here http://test.multicon.in/hotmail/index.php
either you have no contact added in MSN or it should be APP creation issue.
Hi,
Thanks for such prompt reply…I have 2 contacts in this mail id and I have tested your demo with this id and got result. So may be the issue is with the App though I have followed the procedure given in the manual. Can you please check the app once…I have shared the email id and password as the contact us form of this blog…it would be really helpful for me.
Hi,
please help me on how to check whether my app is setup correctly or not!!!
It seems wired , The same login you given load two contact in my demo.
and when I check with your link its
foreach error.
Delete the current APP. create new one, be careful the return url must be same as the url we are using like no difference in www and non-www.
then it should work.
Its due to protocol or www and non-www miss-match.
We’re unable to complete your request
Microsoft account is experiencing technical problems. Please try again later.
How did you solve this error.. Please help i have the same issue.
thanks.
I already answered this issue several time please check older comments.
hi there, it me again!
do u know how it possible to see contacts imported from others(like gmail,yahoo), because thats why i cannot see contacts, figured it out. maybe have to set another scope settings?
thanks in advance
attila
Yep, It should be related to scope , but I’m not sure which one it is!.
You have to search more on their API documentation.
hi there!
I checked your code, but it returns just this-> member_services@outlook.com (i wrote similar code like yours) having already imported like 300 contacts to Outlook.
I wrote grabbers like gmail(oauth 2.0) & yahoo(oauth 1.0 works only) but outlook not returning more than this services email, scope set to scope=wl.signin%20wl.basic%20wl.emails%20wl.contacts_emails, as suggested.
What am i missing? 🙂
can i ask why this just only parce 2 emails and not all the list of contacts?
Did you check my comments ? , for testing purpose I just set the limit to 2 you can set it to any number
This is very nice .Working For Me .
Thanks
hi… i need mobile numbers, address while importing contacts. so i think some other scope will help to get them… what are all the scope available…..???
check my previous comments.
http://msdn.microsoft.com/en-us/library/live/hh243646.aspx
Hi,dude
your code is work good but i had got an error and as per the above all issue i had also checked the curl status which is enabled on y server still got this issue, so please let me know the solution of it.
issue:
Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in D:\inetpub\vhosts\iviewlabs.com\site2\msn_import\oauth-hotmail.php on line 10
Warning: Invalid argument supplied for foreach() in D:\inetpub\vhosts\iviewlabs.com\site2\msn_import\oauth-hotmail.php on line 44
curl
cURL support enabled
cURL Information libcurl/7.21.0 OpenSSL/0.9.8q zlib/1.2.3
on my server …
Set safe_mode = Off in your php.ini file (it’s usually in /etc/ on the server). If that’s already off, then look around for the open_basedir stuff in the php.ini file, and change it accordingly.
Basically, the follow location option has been disabled as a security measure, but PHP’s built-in security features are usually more annoying than secure. In fact, safe_mode is deprecated in PHP 5.3.
dude your demo is not working .. can you guide me more how to work on it need this work 🙁
Demo is working fine,
checked again ,it may happen your account don’t have any contact or contacts may be imported from any other resources in such cases wide scope needed , I just enabled accessing default contact in my APP.
So create the app with proper permission you required.
Hope it gets resolved.
Yes! except [“email_hashes”] field, there is a [“emails”] field I can get email contacts address! thank you very much!
You have a good script can run very well without modification! thanks again!
Thanks for your article, but at last I know email contact list is not possible to get from Hotmail or outlook . Microsoft does not provide this function!
http://social.msdn.microsoft.com/Forums/onedrive/en-US/ca05e45d-d0cd-4c5c-85c8-55ee1f51fd8f/how-to-retrieve-a-userc-contacts-emails?forum=messengerconnect
Oh friend your link post is 3 years old and now Microsoft provide such feature.
did you check my demo ? is that provide your email contacts ?.
You missed the scope section last portion of the article .
I already developed many contact import plugin for Joomla,WordPress and Elgg al are based on this script and works fine.
Please re read the article in details and do not forget to set the scope .
scope=wl.signin%20wl.basic%20wl.emails%20wl.contacts_emails
Hope it make sense!
For all people who have problems with the response, particularly when getting an empty response, and are using Windows, try to do this, worked for me:
– Download cacert.pem from http://curl.haxx.se/ca/cacert.pem
– Config the php.ini, add this line replacing PATH with the absolute path where the file is placed:
curl.cainfo = “PATH\cacert.pem”
The solution is intended for Windows platform and PHP > 5.3.7
Hope this helps and many thanks the author for the scripts.
Hi jobin,
This code is working fine,but after login its displaying only two contact and in my email id i have added multiple contact, how can i get my all contact list please can u help me .
-JK
This is due to my download code have limit for accessing the number of contact set to 2,
that you can change on the following line,
$url = 'https://apis.live.net/v5.0/me/contacts?access_token='.$accesstoken.'&limit=2';
Here the “limit” parameter is setting your number of contacts needs to retrieve, set that to your required number like &limit=100 or above.
Hope its now clear for you.
Can u please provide me this code in .net .
Thanks
Sorry I’m not good in .net but you can simply do it with php and use the output in .net .
Suppose your server supports php and CURL library the set the links using .net to php files and instead of following lines
$msn_email .= $emails['name'];
you can write it to an XML or output as JSON so it will be parse using any server side or client side script.
Hope it make sense..
Hi jobin,
its displaying only 2 contact i have added multiple contact in my account , also i have imported from my gmail account. how can i get correct data
-Jitendra
Set your contact limit properly on following line,
$url = 'https://apis.live.net/v5.0/me/contacts?access_token='.$accesstoken.'&limit=1000';
The above line can be found on oauth-hotmail.php page.
hope its helps..
Hello.. just now i have downloaded ur script but it’s not showing anything ,blank page is coming.
Can you please guide me as soon as possible.
did you setup the app properly ?
download the script and set the keys on your files. don’t forget to set the redirect url on your Hotmail Oauth App then only application works well, Also keep in mind application URL and site URL should be unique like there is no conflict between www and non-www.
All these are correct app works well.
The above code shows the name of persons but I want email ids.I am unable to get email id of the contacts in my account.Please ,let me know how to get email contacts .
The above code should show the email and name ,
Make sure you are set the scope variable correctly.
AWESOME!! AWESOME!! YOU ARE AWESOME!!
It works!!
I love you. you save my day!!
How do you find this information?
I can only find incomplete Microsoft API information on web.
THANK YOU :DDDDDDDDDD
Thanks for your feedback 🙂
Im glad to hear it helps.
hi friend,
My app is showing the error:
PHP Warning: curl_setopt() [function.curl-setopt]: CURLOPT_FOLLOWLOCATION cannot be activated when safe_mode is enabled or an open_basedir is set in E:\Domains\mktollsbr.net\wwwroot\oauth-hotmail.php on line 10
PHP Warning: Invalid argument supplied for foreach() in E:\Domains\mktollsbr.net\wwwroot\oauth-hotmail.php on line 44
can you help me?
It seems to be CURL Library is not enabled on your server.
Make sure CURL is enabled and fully functional, You can check that with php_info();
what do i check with my phpinfo(); because i had the same issues…
very helpful tutorial. Thanks 🙂
sir i am using this api to get contact list and i am getting all contacts at http://www.dev.walkswithme.net/hotmail/oauth-hotmail.php
but if i put this return url to my url then got a error
my url is:
http://neighborhoodsocial.com/app/public/umar/oauth-hotmail.php
error is:
We’re unable to complete your request
Microsoft account is experiencing technical problems. Please try again later.
sir please solve my issue
This may happen for two reason,
1) WWW and Non-WWW issue means you created the app with http://www.domain.com and application runs with http://domain.com or vise versa.
2) The Redirect URI set in the application is incorrect. or Secret key different.
check these and let me know the status.
Both of your code “1.) fetch yahoo contact and 2.) fetch hotmail contact” is not working for me neither does your DEMO.
Please help
Check your error log it have the details ,
the code should work its works fine for others.
Thanks a lot! Very helpful!
Hi,
I got this below error
Invalid argument supplied for foreach()
but i will follow the demo.
please help me.
make sure the redirect uri is correct, there is no issue with www and non-www .
Another thing is the hotmail account should have contact.
Create the app and set the keys its done!
Thanks bro….
my url
http://localhost/hotmail/index.php
every time i get this error message please help hoe to get rid of this message
Your URL must be in a valid URL format, begin with http:// or https://, and be limited to 255 characters in length.
Basically the Hotmail Oauth API do not support localhost.
means it required valid domain name and your return URL must match with the application URL you applied ,
So Please check the Hotmail Oauth API with a server. It works fine
Im also experienced this issues bcoz other Oauth API like Google and Yahoo works in localhost but Hotmail Oauth does not .
I’m trying use your code but it’s work fine but i don’t get all contacts list. so how can i getting all contact list.:).
Make sure your application have proper permission and return url exactly matched with application url.
Another thing is you should have contacts in the hotmail account, then it should work.
Check the error log if any error is reported in it
let me know the status.
Hi how i get the user email from which he login
thanks
The email and other profile data required additional permission , You can check all the available permission details on the below link
http://msdn.microsoft.com/en-us/library/live/hh243646.aspx
For collecting profile use like “wl.emails”
After logined and consent, I got a blank page (pop-up window not close). I check error_log: [04-Apr-2014 00:53:19] PHP Warning: PHP Startup: Unable to load dynamic library ‘/usr/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so’ – /usr/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so: cannot open shared object file: No such file or directory in Unknown on line 0
Please guide me something. Thanks.
CURL Library is enabled ? make sure its enabled , also set up the APP with valid redirect url (Redirect url must match with the return url using). Make sure directory have proper permission too.
Warning: Invalid argument supplied for foreach() in http://www.mydomainname.com/oauth-hotmail.php on line 43
Did you setup the Hotmail APP correctly on MSN ? and you should have to set those APP ID and Secret Key from APP to the config files. Then you will get the proper contacts .
Also make sure you have contact added in your MSN account.
Thank you so much for this. Saved me a lot of time. 🙂
Everything is working f9 but i need to close the popup after login and show the emails on the main page
To close a parent window from a Child window simply write this
window.opener.close();
Worked like a charm on the first try!!!! Thank you sr, you have saved me 1 day of work. 🙂
Im glad to know that,
thanks for your response.
You are welcome, thanks again! If you can, it wouls also be great to know how to read profile pictures. 🙂
The profile picture and other profile data required additional permission , You can check all the available permission details on the below link
http://msdn.microsoft.com/en-us/library/live/hh243646.aspx
For collecting profile use like “wl.contacts_photos”
Hope its helps..
I want to see popup when clicking on the link “MSN Contacts” link . Please help me as early as possible. Thanks in advance.
Simply set the link like below,
window.open(url, windowName, “height=200,width=200”);
It will open up a new window.
Thanks for the response..
Your code is work in demo but when i am download it and use on my server it login and then blank on result window.
It not fetch any email on the result window. please help me ???
Did you setup the APP Id and Secret Key correctly ? Also make sure you have contacts added on your Hotmail account then only it will fetch the contact emails.
Also check you error_log and let me know the status.
hey i get the following error:
{“error”:”invalid_grant”,”error_description”:”The provided value for the ‘code’ parameter is not valid.”}
Can you tell me what the problem might be.
Did you set up the things properly ? the error says granting permission so it might be related to scope. Please check the scope parameters. The hotmail api works fine if you set up the things correctly. Please double check the setup and make sure your application ulr match to the return url (means www or non www) . let me know the status.
Thanks for your feedback 🙂
Wow works now …i dont have contacts ..thats the problem ..
Hai Kalaipriya,
Thanks for your feedback, 🙂
Hotmail contact list reader returns empty means you don’t have any contact!. you found that 🙂
The return was empty and when i typed the return it shows data{ “error”: { “code”: “request_token_invalid”, “message”: “The access token isn’t valid.” } } i have used you code only …is there any thing i have missed
when i tried demo it returns empty..
hello!
I did everything as explained, but keep on getting the same error message:
Warning: Invalid argument supplied for foreach()…. on line 42
any ideas why that happens?
Hai James,
Did you check the demo its works well, you did missed something otherwise it will work, for your easy and fast integration I just added a download link for Hotmail Oauth API using PHP . Just Download the script and set your keys and link then check it works or not ,
Please let me know the status I will be here to support you Guys 🙂
Is there a way to hard code the user’s credentials so you can bypass the prompt that asks user to grant permission to access?
Hello Rob,
The main usage of Oauth API is we can check the login credentials at providers sites , that brings the security, and another reason is this application is used to import user’s hotmail contact list so users should be enter the login details. If you are planning to read your own hotmail contact list then you need to try with Oauth method with token file options, like what i did on my Dropbox file upload . I am not sure that will work with hotmail API.
thanks for your feed back, let me know how can i help you . 🙂
Is it will work for importing the contacts from my outlook account
.For example test@outlook.com ?
Did you mean export your Hotmail contact list from your account ? It will works. You can check the demo if you have any Hotmail account. It will read all your contact list and print the array. 🙂
Hi.. I do not get .. something is missing? : (
Check the Demo its works fine. Did you missed anything ?