PHP:如何使用HTTP基本身份验证发出GET请求 [英] PHP: how to make a GET request with HTTP-Basic authentication

查看:179
本文介绍了PHP:如何使用HTTP基本身份验证发出GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从该端点获取交易状态

I want to get transaction status from this endpoint

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

但是它需要基本的身份验证,当我将其发布在URL上时,我得到的结果是:

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"
}

,我有一个网站ayokngaji.com,然后我想发送基本身份验证以获取我的网址的状态.示例:

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)

我该怎么做?

我还尝试使用邮递员,并使用基本身份验证功能,并显示正确的结果

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

当我在线搜索时 它向我显示了CURL,BASIC AUTH,但我对这些教程都不了解,因为我对英语的了解有限,并且对php的了解很少

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

已解决:

<?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;

推荐答案

有几种方法可以向API端点发出GET请求.但是开发人员更喜欢使用CURL进行请求.我提供了一个代码片段,该代码片段显示了如何使用Basic Auth授权设置Authorization标头,如何使用php的base64_encode()函数(基本Auth授权支持base64编码)对用户名和密码进行编码以及如何准备用于使用php的CURL库的请求.

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.

哦! 别忘了,将用户名密码端点(API端点)替换为您的用户名.

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

使用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()使用流上下文.

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

希望这对您有帮助!

这篇关于PHP:如何使用HTTP基本身份验证发出GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆