I am sending bulk SMS with php API through a SMS gatway like this:
http://smsserviceprovider.com/sendsms.php?apikey=xxxx&text=testsms&mobiles=11111,22222,33333,44444,55555,66666....
(upto 1800 mobile numbers which is stored in mysqldb
). But the service provider only allow me to send only 50 mobiles at once. So I have to send SMS in multiple times. How can I split the parameter
mobiles like this
http://smsserviceprovider.com/sendsms.php?apikey=xxxx&text=testsms&mobiles=11111,22222
http://smsserviceprovider.com/sendsms.php?apikey=xxxx&text=testsms&mobiles=33333,44444
http://smsserviceprovider.com/sendsms.php?apikey=xxxx&text=testsms&mobiles=55555,66666....
My current function in php is
function send_sms($message, $numbers) {
$apikey="xxxxx";
$message=urlencode($message);
$var = "apikey=".$apikey."&text=".$message."&mobiles=".$numbers";
$curl=curl_init('http://smsserviceprovider.com/sendsms.php?'.$var);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
$response=curl_exec($curl);
curl_close($curl);
return $response;
}
The trick is to build an array from your comma-separated list of phone numbers, using something like explode()
and then group them into chunks of 50 using something like array_chunk()
. However, this does mean your function now has to make multiple requests instead of one due to this constraint. So in the following example I return an array of responses instead of just a single response.
function send_sms($message, $numbers) {
$responses = [];
$params = [
'text' => $message,
'mobiles' => '',
'apikey' => 'xxxxx',
];
$numbers = explode(',', $numbers);
foreach(array_chunk($numbers, 50) as $numlist) {
$params["mobiles"] = $numlist;
$queryString = http_build_query($params);
$responses[] = file_get_contents($url . $queryString);
}
return $responses;
}
I am not sure what exactly you want. But if you need mysql solution then you should go for LIMT statement in query. for example :
$sqlstr = "SELECT * FROM Mobiles LIMIT 50 OFFSET 0";
or
$sqlstr = "SELECT * FROM Mobiles LIMIT 0, 50";//offset, limit
Similar functionality can be done in retrieved array of 1800 mobile by using:
$sliced = array_slice($mobileArray, 0, 50)//array_slice($mobileArray, OFFSET, LIMIT);
You can go on changing offset in both implementations recursively. I hope this is ok to get started and extend it in PHP.