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;
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.