According to : https://help.nexmo.com/hc/en-us/articles/205065817-Can-I-send-multiple-SMS-in-a-single-API-request-
"Make sure to keep your connection alive so you can reuse the HTTP socket when sending requests and taking full advantage of your account throughput (5 SMS/second). The best-practice is to leverage HTTP 1/1 and Keep-Alive the connection so each time you are sending a new request you don't need to open another HTTP connection."
I've read several infos to try to keep alive a connection using curl but I am not able to send 5 sms reusing the http socket.
What is the solution ?
I tried with:
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
No success.
I tried to remove:
curl_close($ch);
No success too...
I am not able to find a good way to keep alive my connection in order to send sms as nexmo requires.
Who knows how to proceed?
Sending multiple messages, and using keep-alive
are two separate things. With Nexmo (as the FAQ mentions) you can only send a single SMS per HTTP request. To send multiple SMS, you just have to make multiple HTTP requests.
By default, Nexmo will allow your account to make 5 requests per second to the SMS API. If you want to maximize your throughput, you need to make sure you're making the request as fast as possible (or really, just at least as fast at that 5/second rate limit).
That's where the keep-alive
comes into play, making sure you're sending the requests as fast as possible. The curl_setop
docs reference a CURLOPT_FORBID_REUSE
which allows:
TRUE to force the connection to explicitly close when it has finished processing, and not be pooled for reuse.
So by default, curl is trying to use keep-alive
, assuming you reuse the curl handle. See this question for more details on that.
Borrowing this code from the quickstarts here (disclosure, I'm the author of those):
<?php
$url = 'https://rest.nexmo.com/sms/json?' . http_build_query([
'api_key' => API_KEY,
'api_secret' => API_SECRET,
'to' => YOUR_NUMBER,
'from' => NEXMO_NUMBER,
'text' => 'Hello from Nexmo'
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
If you use curl_setop()
to set a new CURLOPT_URL
with a different number / message (which reuses the curl handle) curl should be using keep-alive
by default.
But keep in mind, this doesn't change how you send multiple messages with Nexmo, it's just a way to optimize the speed at which you send the messages.