Upload media file

Upload media file sample code

The PHP code snippet below shows how you can upload a media file to Africa's Talking servers.

The code uses our PHP SDK.

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 voice service
$voice    = $AT->voice();

// Set your Africa's Talking phone number that will be using the file
$phoneNumber = "+254711082XXX";

// Set url of file to upload
$fileUrl = "http://onlineMediaUrl.com/file.wav";

try {
    // Upload the file
    $result = $voice->uploadMediaFile([
        "phoneNumber" => $phoneNumber,
        "url"         => $fileUrl

} catch (Exception $e) {
    echo "Error: ".$e->getMessage();

You can upload a media/audio file to our servers. This media file will be played when called upon by one of our voice functionalities. ie.

Functionality Attribute Description
Play url This contains the audio file you want played during a call.
Call queueing holdMusic This contains the audio file you want played when the user has been queued waiting to be dequeued.
Dial holdMusic This contains the audio file you want played when a number has been dialed before it's picked.

Our REST resource for making calls is located at: https://voice.africastalking.com/mediaUpload. In order to upload a media file, you will need to pass in the following parameters within a POST request:

Variable Name Location Description
apikey Header Your API Key, which you can generate from your dashboard
username Body Your Africa's Talking account username
url Body The url of the file to upload. Don't forget to start with http://

You can check the HTTP Response Code to determine whether the request was successful. Any response code other than 201 (Created) indicates that the call was not initiated

Example using CURL client
curl -H "Accept: application/xml" -H "Apikey:myAPIKey" -d "username=myUsername&url=http://myOnlineMediaFile.mp3" https://voice.africastalking.com/mediaUpload --insecure