Fetch messages from API

You need your AfricasTalking username and APIKey for any request to our API. If you don't have an APIKey, please click here to get one

The PHP code snippet below shows how to fetch messages from our API.

The code uses our PHP gateway class: AfricasTalkingGateway.php [ Download]

Also, please ensure that you have the php_curl module enabled. To enable it uncomment the line: ;extension=php_curl.dll in php.ini and restart apache


<?php
// Include the helper gateway class
require_once('AfricasTalkingGateway.php');

// Specify your login credentials
$username   = "MyAfricasTalkingUsername";
$apikey     = "MyAfricasTalkingAPIKey";

// NOTE: If connecting to the sandbox, please use your sandbox login credentials

// Create a new instance of our awesome gateway class
$gateway  = new AfricaStalkingGateway($username, $apikey);

// NOTE: If connecting to the sandbox, please add the sandbox flag to the constructor:
/*************************************************************************************
             ****SANDBOX****
$gateway    = new AfricasTalkingGateway($username, $apiKey, "sandbox");
**************************************************************************************/

// Any gateway errors will be captured by our custom Exception class below, 
// so wrap the call in a try-catch block
try 
{
  // Our gateway will return 100 messages at a time back to you, starting with
  // what you currently believe is the lastReceivedId. Specify 0 for the first
  // time you access the gateway, and the ID of the last message we sent you
  // on subsequent results
  $lastReceivedId = 0;
  
  // Here is a sample of how to fetch all messages using a while loop
  do {
    
    $results = $gateway->fetchMessages($lastReceivedId);
    foreach($results as $result) {
      
      echo " From: " .$result->from;
      echo " To: " .$result->to;
      echo " Message: ".$result->text;
      echo " Date Sent: " .$result->date;
      echo " LinkId: " .$result->linkId;
      echo " id: ".$result->id;
      echo "\n";
      $lastReceivedId = $result->id;
      
    }
  } while ( count($results) > 0 );
  
  // NOTE: Be sure to save lastReceivedId here for next time
  
}
catch ( AfricasTalkingGatewayException $e )
{
  echo "Encountered an error: ".$e->getMessage();
}
// DONE!!!

Parameter Location Description Required
API key Header API key generated from your account settings Yes
Accept Header This is the format you would like your data formatted. It may be application/xml or application/json. The default is application/xml. The gateway classes provided uses appliaction/json. No
username URL This is your username Yes
lastReceivedId URL This is the id of the message that you last processed. If this is your first call, pass in 0 Yes
XML
<?xml version="1.0"?>
  <AfricastalkingResponse>
    <SMSMessageData>
      <Messages>
        <from>+254711XXXYYY</from>
        <to>5252</to>
        <text>A test message</text>
        <linkId>messageLinkId</linkId>
        <date>2012-07-15 15:40:06</date>
        <id>1</id>
      </Message>
      .
      .
      .
      .
      <Message>
        <from>+254733YYYZZZ</from>
        <to>5252</to>
        <text>Another test message</text>
        <linkId>messageLinkId</linkId>
        <date>2012-07-15 15:47:06</date>
        <id>10</id>
      </Message>
    </Messages>
  </SMSMessageData>
</AfricastalkingResponse>
JSON

{"SMSMessageData":
  {"Messages":
    [
      {
        "from":"+254711XXXYYY",
        "to":"5252",
        "text":"A test message",
        "linkId": "messageLinkId",
        "date":"2012-07-15 15:40:06",
        "id":"1"
      },
      .
      .
      .
      .
      {
        "from":"+254733YYYZZZ",
        "to":"5252",
        "text":"Another test message",
        "linkId": "messageLinkId",
        "date":"2012-07-15 15:47:06",
        "id":"10"
      }
    ]
  }
}
Example using CURL client
curl -v -H "Accept: application/xml" -H "Apikey:fb752d3417021812f0961y6c9464832dd1adb1e555c73f1e7c32bcc006488674" https://api.africastalking.com/version1/messaging?username=myUsername&lastReceivedId=0 --insecure