示例#1
0
function sendAirtime($recipients)
{
    require_once "AfricasTalkingGateway.php";
    //Specify your credentials
    $username = "******";
    $apiKey = "apikey";
    $recipientStringFormat = json_encode($recipients);
    //Create an instance of our awesome gateway class and pass your credentials
    $gateway = new AfricasTalkingGateway($username, $apiKey);
    try {
        $results = $gateway->sendAirtime($recipientStringFormat);
        foreach ($results as $result) {
            echo $result->status;
            echo $result->amount;
            echo $result->phoneNumber;
            echo $result->discount;
            echo $result->requestId;
            //Error message is important when the status is not Success
            echo $result->errorMessage;
        }
    } catch (AfricasTalkingGatewayException $e) {
        echo $e->getMessage();
    }
}
         echo "Calls have been initiated. Time for song and dance!\n";
         // Our API will now contact your callback URL once the recipient answers the call!
     } catch (AfricasTalkingGatewayException $e) {
         echo "Encountered an error while making the call: " . $e->getMessage();
     }
 } elseif ($userResponse == "3") {
     $response = "END Please wait while we load your account.\n";
     // Search DB and the Send Airtime
     $recipients = array(array("phoneNumber" => "" . $phoneNumber . "", "amount" => "KES 10"));
     //JSON encode
     $recipientStringFormat = json_encode($recipients);
     //Create an instance of our awesome gateway class and pass your credentials
     $gateway = new AfricasTalkingGateway($username, $apiKey);
     // Thats it, hit send and we'll take care of the rest. Any errors will be captured in the Exception class as shown below
     try {
         $results = $gateway->sendAirtime($recipientStringFormat);
         //Store the service details
         foreach ($results as $result) {
             $status = $result->status;
             $amount = $result->amount;
             $airtimeNo = $result->phoneNumber;
             $discount = $result->discount;
             $requestId = $result->requestId;
             $error = $result->errorMessage;
             // You can then store this information in the database for your records
             $dialQuery = "insert into `services`(`callSession_id`, `phoneNumber`,`service`, 'amount', 'currencyCode', 'recording')\n    values('" . $requestId . "','" . $airtimeNo . "', 'Airtime top up','" . $amount . "', NULL, NULL)";
             $db->query($dialQuery);
         }
     } catch (AfricasTalkingGatewayException $e) {
         echo $e->getMessage();
     }