How to use our API :: API

How to use our API (In PHP)

This page shows how to use MobileNig API (In PHP), using Airtime API as a case study

Request sample:


//Input parameters as given in the documentation
$request = "";
$param["username"] = "PAUL";
$param["api_key"] = "64125agshaii168sdhd4b6bdb5";
$param["network"] = "MTN";
$param["phoneNumber"] = "08011223344";
$param["amount"] = "200";
$param["trans_id"] = "12823327903";

//unique id, you can use time()
foreach($param as $key=>$val) //traverse through each member of the param array
{
$request .= $key . "=" . urlencode($val); //we have to urlencode the values
$request .= '&'; //append the ampersand (&) sign after each paramter/value pair
}
$len = strlen($request) - 1;
$request = substr($request, 0, $len); //remove the final ampersand sign from the request

$url = "https://mobilenig.com/API/airtime_test?"; //The URL given in the documentation without parameters
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url$request");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //return as a variable
$response = curl_exec($ch);
curl_close($ch);
The success response will be in the JSON format below
{"trans_id":"12823327903","details":{"network":"MTN","phone_number":"08011223344","amount":"200","status":"SUCCESSFUL","balance":"3000"}}

//decode response to get trans_id,network,phone_number,amount,status and balance
$array = json_decode($response, true); //decode the JSON response
$trans_id = $array["trans_id"];
$network = $array["details"]["network"];
$phone_number = $array["details"]["phone_number"];
$amount = $array["details"]["amount"];
$status = $array["details"]["status"];
$balance = $array["details"]["balance"];

if($status == "SUCCESSFUL"){
//write successful code
}else{
//write failed code
}




Query sample:


//Input parameters as given in the documentation
$request = "";
$param["username"] = "PAUL";
$param["api_key"] = "64125agshaii168sdhd4b6bdb5";
$param["trans_id"] = "12823327903";

//The trans_id used for the transaction you wish to query
foreach($param as $key=>$val) //traverse through each member of the param array
{
$request .= $key . "=" . urlencode($val); //we have to urlencode the values
$request .= '&'; //append the ampersand (&) sign after each paramter/value pair
}
$len = strlen($request) - 1;
$request = substr($request, 0, $len); //remove the final ampersand sign from the request

$url = "https://mobilenig.com/API/airtime_query?"; //The URL given in the documentation without parameters
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "$url$request");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //return as a variable
$response = curl_exec($ch);
curl_close($ch);
The success response will be in the JSON format below
{"trans_id":"12823327903","details":{"network":"MTN","phone_number":"08011223344","amount":"200","status":"SUCCESSFUL"}}

//decode response to get trans_id,network,phone_number,amount,status and balance
$array = json_decode($response, true); //decode the JSON response
$trans_id = $array["trans_id"];
$network = $array["details"]["network"];
$phone_number = $array["details"]["phone_number"];
$amount = $array["details"]["amount"];
$status = $array["details"]["status"];

if($status == "SUCCESSFUL"){
//write successful code
}else{
//write failed code
}