DEV Community

popoola Temitope
popoola Temitope

Posted on

2 2

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

    Image of Datadog

    The Essential Toolkit for Front-end Developers

    Take a user-centric approach to front-end monitoring that evolves alongside increasingly complex frameworks and single-page applications.

    Get The Kit

    Top comments (0)

    Image of Timescale

    Timescale – the developer's data platform for modern apps, built on PostgreSQL

    Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

    Try free