【发布时间】:2021-08-02 04:06:59
【问题描述】:
我正在尝试调用某个服务提供商的 API,它有 public and secret API keys。
不过,我目前正在使用 Laravel,但是关于如何发送 post 请求,只有 JavaScript、NodeJS、Python 实现。
我的问题是,如何在 Laravel/PHP 上发送发布请求以避免公开 API 密钥?
它们具有应遵循的特定格式:
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: 'Basic some_base64_encrypted_key'
},
body: JSON.stringify({
data: {
attributes: {
amount: 10000,
redirect: {success: 'https://www.test1.com/', failed: 'https://www.test2.com/'},
type: 'some_paymenth_method',
currency: 'SOME_CURRENCY'
}
}
})
};
fetch('https://api.serviceprovider.com/v1/sources', options)
.then(response => response.json())
.then(response => console.log(response))
.catch(err => console.error(err));
【问题讨论】:
-
阅读Laravel's HTTP Client。我没有处理基本授权的请求,但其中有一些通用信息可能会有所帮助