Skip to main content

Login with Microsoft Live Account PHP

How to Login with Microsoft Live Account PHP



This post explains you how to implement login Microsoft Live account using PHP.

For login ,  we need  Client Secret Key and Client ID . Click Here To create Microsoft application that will provide client id and password.

Open the above url and fill "app name" and "redirect url" .

Application id is your client ID and application secret is client secret key.

Our api is created successfully.

Now we need to create 2 php pages one for login and second one for receive user data as response from microsoft app.

First create a link on your login page , that will redirect to user on our microsoft login page(mslogin).
<a href="mslogin.php">Login with Microsoft</a>


Then create a page mslogin.php and paste below code

mslogin.php
<?php
$client_id = "YOUR-CLIENT-ID";
$redirect_uri = "https://your-site.com/response.php"; //you can also use localhost url for testing but first add url into microsoft app redirect url then you can use localhost url
 $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;
  header("Location: " .$urls);
?>

Now when user click on that link then he will redirect to microsoft login page and after login through microsoft he will redirect to our response url . we already saved redirect url into our app.

Now create response.php
<?php
   if(isset($_GET['code']) && $_GET['code']!="") {
    $auth_code = $_GET["code"];
    $client_id = "YOUR-CLIENT-ID";
    $client_secret = "Your-API-PASSWORD";
    $redirect_uri = "https://your-site.com/response.php"; 

    $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;
    $get_profile_url='https://apis.live.net/v5.0/me?access_token='.$accesstoken;
    $xmlprofile_res=curl_file_get_contents($get_profile_url);
    $profile_res = json_decode($xmlprofile_res, true);

    if($profile_res) {
        echo "<pre>"; print_r($profile_res); echo "</pre>"; //this will print output of user info
        $userid = $profile_res['id'];
        $login_name = $profile_res['name'];
        $fname = $profile_res['first_name'];
        $lname = $profile_res['last_name'];
        $email = $profile_res['emails']['account'];
    }    
  } 
?> 

Now run your mslogin.php and try to login with microsoft live ID. oh it works great.  :)

Comments

  1. Did you hear there is a 12 word phrase you can communicate to your crush... that will induce deep feelings of love and instinctual attraction to you buried within his chest?

    That's because hidden in these 12 words is a "secret signal" that triggers a man's instinct to love, adore and guard you with his entire heart...

    12 Words Will Fuel A Man's Desire Instinct

    This instinct is so hardwired into a man's mind that it will drive him to try harder than ever before to build your relationship stronger.

    Matter-of-fact, fueling this powerful instinct is absolutely binding to having the best possible relationship with your man that the instance you send your man a "Secret Signal"...

    ...You'll immediately find him expose his mind and soul to you in a way he never expressed before and he'll recognize you as the one and only woman in the galaxy who has ever truly understood him.

    ReplyDelete
  2. Your Affiliate Money Printing Machine is waiting -

    And getting it set up is as simple as 1..2..3!

    Here is how it works...

    STEP 1. Input into the system which affiliate products you want to promote
    STEP 2. Add PUSH BUTTON TRAFFIC (this LITERALLY takes 2 minutes)
    STEP 3. Watch the system grow your list and sell your affiliate products all for you!

    So, do you want to start making profits???

    Check it out here

    ReplyDelete
  3. This is how my friend Wesley Virgin's autobiography begins with this SHOCKING AND CONTROVERSIAL video.

    Wesley was in the army-and soon after leaving-he found hidden, "self mind control" secrets that the CIA and others used to get everything they want.

    As it turns out, these are the EXACT same methods many celebrities (notably those who "come out of nowhere") and top business people used to become wealthy and famous.

    You probably know how you utilize only 10% of your brain.

    That's mostly because the majority of your brain's power is UNCONSCIOUS.

    Perhaps this conversation has even taken place INSIDE OF YOUR very own brain... as it did in my good friend Wesley Virgin's brain 7 years back, while driving an unlicensed, beat-up garbage bucket of a vehicle with a suspended license and $3 in his bank account.

    "I'm absolutely fed up with living paycheck to paycheck! When will I finally make it?"

    You took part in those types of conversations, ain't it so?

    Your own success story is going to be written. You just have to take a leap of faith in YOURSELF.

    Take Action Now!

    ReplyDelete
  4. function curl_file_get_contents($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Convert website to android and ios application using react native expo webview

Convert website to android and ios application using react native expo webview If you want to check you can check on github by using below link also dont forget to give star ;) Source Code: https://github.com/shubham715/react-native-expo-webview-convert-website-to-app React native is the best choice to create multi platform mobile application , but sometimes we dont want to write a complete application because we already have a web application or a website and its complicated to manage both . So we have a solution for this problem. React native supports webView that makes easy to run any website url like an app natively. What is webview in react native? In React native, WebView helps to show web content in a native view. For this tutorial we will use EXPO. What is EXPO ? EXPO a set of tools to help you quickly start an app. Expo have many inbuilt components that helps to simplify the development and testing of React Native app. So please follow the below steps to c...

Solution-windows 'expo' is not recognized as an internal or external command

Solution for expo is not recognized as an internal or external command,operable program or batch file in Windows 10 Sometimes expo will not work globally mostly in windows 10, If you are facing same issue then follow the below Steps 1) Click on windows button and search for  " Environment variables"  and click on "Edit the system environment variables" 2) Now you will see a popup like below screen. Then you need to click on Environment Variables. (Please see highlight part in below image)     3)Then click on new button that i have highlighted in below image 4. Then a popup will open and you need to fill details like below mentioned Variable Name :Path Variable Value: %USERPROFILE%\AppData\Roaming\npm Here we are creating a new path variable and passing location of npm.   Now Click on OK and close all the terminal windows and open new CMD or terminal and type Expo . Great now you can access expo from any...

Read files from folder using php

Read files from folder using PHP Today we learn how to read all files from a folder . we will learn to list all files and read all files . So please follow below steps:-   METHOD 1 1) List all files from folder If you want the list of all files in a folder then you can do by using below code //Get a list of file paths using the glob function. $allFilesList= glob('myfolder/*'); //Loop through the array that glob returned. foreach($allFilesList as $filename){ //Simply print them out onto the screen. echo $filename; echo '<br>'; } The above code will print list of all files like file1.jpg file2.png file3.gif file4.pdf   If you want to read only specific extension file like you just want to a list of all png files then you can do it by using below code. //Get a list of all files ending in .txt $fileList = glob('myfolder/*.png);   METHOD 2 Here is the second method. here we are using scandir() function to s...