Search code examples
phpaccess-token

I want to validate user login using api access_token in php


I am having this demo link to get acces_token for the user verifications of the user login.

http://testapp.test/app.php/v1/verifyUser?username=admin&password=password&grant_type=password&client_id=6_20jeacfmdv0gwgg4kcgw44swwkoo8ogog8g4gwggwo0wko08ow&client_secret=38311bdlv884wc0ws4g048k0ieei8oc4oc0cmswok8w33o0s9e

After sending request using postman i am getting the access token in the response

<?xml version="1.0"?>
<user_info>
    <access_token>NGI20QFmNDE1HAUjYWEyMjVlbsryNWRjNGJmOGI5ZDNiLhA1OTI5MjkzZjIwZmE4YmYxOHYwMTQ3NTUzYTdmMg</access_token>
    <expires_in>86400</expires_in>
    <token_type>bearer</token_type>
    <scope/>
    <refresh_token>AWMxYmVhYmFkNDJkOTVjYjM1YWQwNTVmOTcwYmIyKaR98TE1Y2E5KLZkZTY0ZjA3Mjc3MzQzOeGmTTUzODlmYw</refresh_token>
    <user_id>007</user_id>
    <username>admin</username>
    <email>email@admin.com</email>
    <firstname>admin</firstname>
    <lastname>admin</lastname>
    <ip_address>xxx.xxx.xx.xxx</ip_address>
    <last_login>
        <date>2019-07-12 18:21:27.000000</date>
        <timezone_type>4</timezone_type>
        <timezone>Central</timezone>
    </last_login>
</user_info>

I want to use the acess_token authentication system to login the user in the website using PHP.


Solution

  • Here is the solution to my problem

       $url = 'http://testapp.test/app.php/v1/verifyUser?username=admin&password=password&grant_type=password&client_id=6_20jeacfmdv0gwgg4kcgw44swwkoo8ogog8g4gwggwo0wko08ow&client_secret=38311bdlv884wc0ws4g048k0ieei8oc4oc0cmswok8w33o0s9e';
    
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
        curl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);
    
        $data = curl_exec($ch); 
        //echo $data;
        if(curl_errno($ch))
        {  
            print curl_error($ch);
        }
        else
        {
          curl_close($ch);
            $xml = simplexml_load_string($data);
    
            $json = json_encode($xml);
            $var= json_decode($json,true);
        }
        if(isset($var["error"]))
           {
             echo "error";
    
           }
             else
           {
    
           $_SESSION['signed_in'] = true;
           $_SESSION['sno']     = $var["user_id"];
           $_SESSION['first_name']  = $var["firstname"];
    
    
       }
    

    Hopefully, this answer will help those who have same problem.