Main menu
WalkswithMeAPI PHPHotmail Contact list reader API in php (MSN Oauth)

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

Register with MSN Oauth

Register with MSN Oauth

Next you have to provide the return url on the page and get both client id and secret key.

Hotmail Contact list reader api with MSN Oauth

Hotmail Contact list reader api with MSN Oauth

 

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> &nbsp;&nbsp;&nbsp;<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….

demo_image                            Thanks for reading 🙂  🙂  🙂

Download2156 downloads

93 thoughts on “Hotmail Contact list reader API in php (MSN Oauth)

  1. 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.

    1. 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.

  2. 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.

  3. following error have been occuring :
    We’re unable to complete your request
    Microsoft account is experiencing technical problems. Please try again later.

  4. 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.

    1. 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!!!

  5. 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???

        1. 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.

          1. 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.

    1. 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.

  6. 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

  7. 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? 🙂

  8. 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…..???

  9. 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 …

    1. 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.

    1. 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.

  10. 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!

    1. 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!

  11. 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.

  12. 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

    1. 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.

    1. 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..

      1. 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

        1. 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..

  13. 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.

    1. 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.

  14. 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 .

  15. 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

  16. 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?

  17. Both of your code “1.) fetch yahoo contact and 2.) fetch hotmail contact” is not working for me neither does your DEMO.

    Please help

  18. Hi,

    I got this below error

    Invalid argument supplied for foreach()

    but i will follow the demo.

    please help me.

    1. 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!

    1. 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 .

  19. 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.:).

    1. 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.

  20. 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.

    1. 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.

    1. 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.

  21. Everything is working f9 but i need to close the popup after login and show the emails on the main page

  22. I want to see popup when clicking on the link “MSN Contacts” link . Please help me as early as possible. Thanks in advance.

  23. 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 ???

    1. 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.

  24. 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.

    1. 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 🙂

  25. 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

  26. 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?

    1. 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 🙂

  27. 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?

    1. 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 . 🙂

    1. 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. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

 

FacebookTwitterGoogle+RSS