Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: how to make a GET request with HTTP-Basic authentication

I want to get transaction status from this endpoint

https://api.sandbox.midtrans.com/v2/[orderid]/status

but it needs a basic auth, when i post it on the URL the result I am getting is:

{
    "status_code": "401",
    "status_message": "Operation is not allowed due to unauthorized payload.",
    "id": "e722750a-a400-4826-986c-ebe679e5fd94"
}

and i have a website ayokngaji.com then i want to send basic auth to get status with my url. Example:

ayokngaji.com/v2/[orderid]/status = (BASIC AUTH INCLUDED)

How do i make this?

i also tried using postman, and using basic auth it work, and show the right result

when i search it online it show me like CURL, BASIC AUTH, but i don't understand any of these tutorial because my limit on english and small knowledge on php

SOLVED:

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://api.sandbox.midtrans.com/v2/order-101c-1581491105/status",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "GET",
  CURLOPT_HTTPHEADER => array(
    "Accept: application/json",
    "Content-Type: application/json",
    "Authorization: Basic U0ItTWlkLXNl"
  ),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
like image 808
Firdaus The Gamer Avatar asked Feb 12 '20 07:02

Firdaus The Gamer


People also ask

How do I make a request using HTTP basic authentication with PHP curl?

Yes, Curl has built-in support for basic HTTP server authorization. To make a Curl request with basic authorization credentials, you need to use the following command line parameter: -u username: password (or --user).

How can I pass the basic HTTP authentication?

We can do HTTP basic authentication URL with @ in password. We have to pass the credentials appended with the URL. The username and password must be added with the format − https://username:password@URL.

How do I use HTTP basic authentication in REST API?

Users of the REST API can authenticate by providing their user ID and password within an HTTP header. To use this method of authentication with HTTP methods, such as POST, PATCH, and DELETE, the ibm-mq-rest-csrf-token HTTP header must also be provided, as well as a user ID and password.


1 Answers

There several ways you may make a GET request to a API endpoint. But developers prefer making requests using CURL. I am providing a code snippet that shows how to set Authorization header with Basic Auth authorization, how to encode username and password using php's base64_encode() function (Basic Auth authorization supports base64 encoding), and how to prepare headers for making a request using php's CURL library.

Oh! do not forget to replace username, password and endpoint (api endpoint) with yours ones.

Using CURL

<?php

$username = 'your-username';
$password = 'your-password'
$endpoint = 'your-api-endpoint';

$credentials = base64_encode("$username:$password");

$headers = [];
$headers[] = "Authorization: Basic {$credentials}";
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'Cache-Control: no-cache';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);

// Debug the result
var_dump($result); 

Using stream contexts

<?php

// Create a stream
$opts = array(
    'http' => array(
        'method' => "GET",
        'header' => "Authorization: Basic " . base64_encode("$username:$password")
    )
);

$context = stream_context_create($opts);

// Open the file using the HTTP headers set above
$result = file_get_contents($endpoint, false, $context);

echo '<pre>';
print_r($result);

You may refer to this php doc for how to use stream context using file_get_contents().

Hope this would help you!

like image 69
unclexo Avatar answered Sep 20 '22 12:09

unclexo