Search code examples
phphttpclickatell

Clickatell batch templates slow (PHP, http)


I'm trying to setup clickatell at the moment for sending out batch sms'. I've got it working but it's quite slow. About 20 seconds to send 5 test sms and 30 seconds for 10 test sms.

$nums = array(
"44-227811116" => "1",
"44-227819885" => "2",
"44-227819314" => "3",
"44-227815413" => "4",
"44-227819326" => "5"
);

//login
$url="https://api.clickatell.com/http/auth?api_id=xxxxx&user=xxxxx&password=xxxxx";
$page=Utilities::getWebPage($url);

//session
$clicksessionparts=explode(":", $page);
$clicksession=trim($clicksessionparts[1]);

//batch
$from=xxxxx;
$batchTemplate = urlencode("Test message #field1#");

$url="https://api.clickatell.com/http_batch/startbatch?session_id=$clicksession&template=$batchTemplate&from=$from&deliv_ack=1";
$page=Utilities::getWebPage($url);
$batchId=explode(":",$page);
$batchId=trim($batchId[1]);

foreach ($nums as $k => $v)
{
$start = new DateTime();
print_r($start->format("H i:s"));
$url="https://api.clickatell.com/http_batch/senditem?session_id=$clicksession&batch_id=$batchId&to=xxxxx&field1=$v";
$page=Utilities::getWebPage($url);
echo "<pre>";
print_r($page);
echo "</pre>";
$end = new DateTime();
print_r($end->format("H i:s"));
echo "<br><br>";
}

Solution

  • You should be able to submit over 100 messages per second to the HTTP API comfortably.

    Creating HTTPS connections is a very slow process (compared to HTTP). If you want better performance with HTTPS, you will have to reuse the connections.

    I am guessing that Utilities::getWebPage() is creating a new HTTPS connection each time? For PHP I would suggest you look at using cURL.

    If you want to go another step further (I doubt you need to go this far), you can consider using curl_multi... Its a bit more work though and most people dont need so much speed (some find it easier just to use another API like the SMTP API so they have many messages in 1 email).

    Also, you technically do not need to use the batch commands on the HTTP API to send your messages (unless you want to). You can send millions with just api.clickatell.com/http/sendmsg?.... in which case there is no need to do a start batch call.

    With something like an SMTP API, you can put 100 000 messages in one email (if you need unique text per message, you would use the batch facility on that API).