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

    Postmark Image

    Speedy emails, satisfied customers

    Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

    Sign up

    Top comments (0)

    A Workflow Copilot. Tailored to You.

    Pieces.app image

    Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

    Read the docs

    👋 Kindness is contagious

    Please leave a ❤️ or a friendly comment on this post if you found it helpful!

    Okay