PHP:如何使用 HTTP-Basic 身份验证发出 GET 请求

标签 php api basic-authentication

我想从这个端点获取交易状态

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

但它需要一个基本的身份验证,当我将它发布到 URL 上时,我得到的结果是:
{
    "status_code": "401",
    "status_message": "Operation is not allowed due to unauthorized payload.",
    "id": "e722750a-a400-4826-986c-ebe679e5fd94"
}

我有一个网站 ayokngaji.com 然后我想发送基本身份验证以获取我的 url 状态。例子:
ayokngaji.com/v2/[orderid]/status = (BASIC AUTH INCLUDED)

我怎么做这个?

我还尝试使用 postman ,并使用基本身份验证它可以工作,并显示正确的结果

当我在网上搜索时
它显示我喜欢 CURL、BASIC AUTH,但我不理解这些教程中的任何一个,因为我对英语的限制和对 php 的小知识

解决了:
<?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;

最佳答案

您可以通过多种方式制作 GET对 API 端点的请求。但开发人员更喜欢使用 CURL 发出请求.我提供了一个代码片段,展示了如何设置 Authorization具有基本身份验证授权的 header ,如何使用 php 的 base64_encode() 对用户名和密码进行编码函数(Basic Auth 授权支持 base64 编码),以及如何使用 php 的 CURL 库准备请求头。

哦! 别忘了替换 用户名 , 密码 端点 (api 端点) 与您的。

使用 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); 

使用流上下文
<?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);

你可以引用这个php doc关于如何使用 file_get_contents() 使用流上下文.

希望这会帮助你!

关于PHP:如何使用 HTTP-Basic 身份验证发出 GET 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60183353/

相关文章:

php - 查询返回所需的行,直到添加 LIKE

php - 使用 PHP 从文件中读取大型 js 数组

api - _resource_mismatch_handler_没有开始WSO2 esb 5.0.0

当用户名和密码包含特殊字符时,通过 Selenium 进行 Python HTTP 基本身份验证

Php - 通知 : Undefined offset 10?

php - 如何在 ZEND SOAP AutoDiscovery 中设置这些复杂类型?

rest - Django REST Framework 与accepted_renderer 错误

javascript - 连接JSON API进行解析

java - Spring Security - 基本认证

java - 使用 requestInterceptor 改造基本身份验证