javascript - Requesting a POST METHOD to a url

Please, i am trying to build an Airtime application, so i am ask to. (1) Send airtime by making a HTTP POST request to one of the following endpoints: Live: https://api.africastalking.com/version1/airtime/send Sandbox: https://api.sandbox.africastalking.com/version1/airtime/send
Request parameters: (a) username - String, (b)recipients String A url encoded json list of Recipients, the format of this string is: [{"phoneNumber":"+254711XXXYYY","amount":"KES X"}] Recipient is a Map with the following parameters: phoneNumber String Required: The phone number that will be topped up in international format (e.g +234811222333). amount String Required: The value of airtime to send together with the currency code. The format of this string is: (3-digit Currency Code)(space)(Decimal Value) e.g KES 100.50.
"here is their demo code"
<?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();
}
?>
Please i don't understand how to write the POST requset
Answer
Solution:
For the africastalking, the normal syntax is (if you are using curl and you have the 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'
Obviously their system requires you to submit data by POST.
Since they have already sent you the PHP sample codes, just put the php in your webserver (or any machine capable of running PHP) and then execute the PHP will be fine. (according to your sample PHP codes, the result will be printed on screen: print_r($results);). Their API should have already taken care of all the required methods, including the POST data requests.
But of course you need to install the API first, I believe they may ask you to use composer to install it. (if you do not know how to use composer, or if your system does not support it, an alternative method is to ask them to provide the necessary files so that you can use FTP to upload to your site. But using composer or similar tools will be easier)
Last but not least , you may use the above curl syntax (curl -X POST xxxxx) to create a php script to submit the post request, please see below a sample:
Additional Information (sample php code):
$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);
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: trying to access array offset on value of type bool in
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.