javascript - 向 url 请求 POST METHOD
问题描述
拜托,我正在尝试构建一个通话时间应用程序,所以我被要求这样做。(1) 通过向以下端点之一发出 HTTP POST 请求来发送通话时间: 实时:https ://api.africastalking.com/version1/airtime/send 沙盒:https ://api.sandbox.africastalking.com/version1 /通话时间/发送
请求参数:(a) username - String, (b)recipients String 一个 url 编码的 json Recipients 列表,该字符串的格式为:[{"phoneNumber":"+254711XXXYYY","amount":"KES X"} ] Recipient 是一个带有以下参数的 Map: phoneNumber String 必需:将以国际格式充值的电话号码(例如 +234811222333)。amount String 必需:与货币代码一起发送的通话时间值。此字符串的格式为:(3 位货币代码)(空格)(十进制值),例如 KES 100.50。
“这是他们的演示代码”
<?php
require 'vendor/autoload.php';
use AfricasTalking\SDK\AfricasTalking;
// Set your app credentials
$username = "MyAppUsername";
$apikey = "MyAppAPIKey";
// Initialize the SDK
$AT = new AfricasTalking($username, $apiKey);
// Get the airtime service
$airtime = $AT->airtime();
// Set the phone number, currency code and amount in the format below
$recipients = [[
"phoneNumber" => "MyPhoneNumber",
"currencyCode" => "KES",
"amount" => 100
]];
try {
// That's it, hit send and we'll take care of the rest
$results = $airtime->send([
"recipients" => $recipients
]);
print_r($results);
} catch(Exception $e) {
echo "Error: ".$e->getMessage();
}
?>
解决方案
对于 africastalking,正常的语法是(如果你使用 curl 并且你有 apikey)
curl -X POST \
https://api.sandbox.africastalking.com/version1/airtime/send \
-H 'Accept: application/json' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'apiKey: MyAppAPIKey' \
-d 'username=myAppUserName&recipients=%5B%7B%22phoneNumber%22%3A%20%22MyPhoneNumber%22%2C%22currencyCode%22%3A%20%22KES%22%2C%20%22amount%22%3A%20%22100%22%20%7D%5D'
显然,他们的系统要求您通过 POST 提交数据。
由于他们已经向您发送了 PHP 示例代码,只需将 php 放入您的网络服务器(或任何能够运行 PHP 的机器)中,然后执行 PHP 就可以了。(根据您的示例 PHP 代码,结果将打印在屏幕上:print_r($results);)。他们的 API 应该已经处理了所有必需的方法,包括 POST 数据请求。
但是当然你需要先安装API,我相信他们可能会要求你使用composer来安装它。(如果您不知道如何使用composer,或者您的系统不支持,另一种方法是要求他们提供必要的文件,以便您可以使用FTP上传到您的站点。但使用composer或类似工具会更容易)
最后但同样重要的是,您可以使用上面的 curl 语法(curl -X POST xxxxx)创建一个 php 脚本来提交 post 请求,请参见下面的示例:
附加信息(示例 php 代码):
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.africastalking.com/version1/airtime/send');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "username=myAppUserName&recipients=testrecipient&phoneNumber=12121212&MyPhoneNumber=99999999¤cyCode=KES&amount=100");
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'Apikey: MyAppAPIKey';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
echo $result;
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
推荐阅读
- aspnetboilerplate - 如何创建像 aspnetboilerplate 这样的文档站点?
- sql - 如何使用 UWP 连接到数据库?
- django - 为什么我在嵌套 if 的模型中的方法中得到一个 none 值?
- r - 将文本框或表格添加到 R highchart
- javascript - Javascript 查找对象是否具有至少一个未定义的属性
- android - 应用程序关闭后如何保持套接字连接打开?
- android - 如何在 Android API 21 上使用 NMEA 协议?
- android - 服务内的 Android 版 MapBox
- oracle - 我在哪里可以找到 Oracle Cloud 中的 Rest API Endpoint?
- asp.net - ADFS 本机应用程序访问令牌缺少声明