DEV Community

popoola Temitope
popoola Temitope

Posted on

Basic Authentication

When using endpoint API, we are most time required to authenticate the request before it can be process.


In this post I'll show you how to set basic authentication on your php script.

  • You'll have to declare variable $basic_1 and assign the value of the function base64_encode("username: your_username") to the variable on your php code. The username and it value will be given from the endpoint website.

  • 
    <?php
     $basic_1 = base64_encode("username: your_username");
    ?>
    
    
    Enter fullscreen mode Exit fullscreen mode

    You can now use basic authentication on your php code once you call the variable name like the example below

    
    <?php
     $basic_1 = base64_encode("username: your_username");
    $curl = curl_init();
    
     curl_setopt_array($curl, array(
                           CURLOPT_URL => "https://www.endpoint_url.com/api/v1/auth/login",
                          CURLOPT_RETURNTRANSFER => true,
                          CURLOPT_ENCODING => "",
                           CURLOPT_MAXREDIRS => 10,
                           CURLOPT_TIMEOUT => 0,
                           CURLOPT_FOLLOWLOCATION => true,
                          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                          CURLOPT_CUSTOMREQUEST => "POST",
                          CURLOPT_POSTFIELDS =>"{\n  \"amount\": 100.00}",
                          CURLOPT_HTTPHEADER => array(
                            "Authorization: Basic ".$basic_1,
                             "Content-Type: application/json"
                           ),
                         ));
    
    ?>
    
    
    Enter fullscreen mode Exit fullscreen mode

    Conclusion

    This should give a success response

    Top comments (0)