简体   繁体   English

php curl保持活动连接

[英]Php curl keep alive connection

According to : https://help.nexmo.com/hc/en-us/articles/205065817-Can-I-send-multiple-SMS-in-a-single-API-request- 根据: 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." “请确保连接保持活动状态,以便在发送请求并充分利用帐户吞吐量(5 SMS /秒)时可以重新使用HTTP套接字。最佳实践是利用HTTP 1/1并保持连接状态因此,每次发送新请求时,您都无需打开另一个HTTP连接。”

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. 我已经阅读了一些信息,以尝试使用curl保持连接状态,但是我无法使用HTTP套接字发送5条短信。

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. 我无法找到一种好的方法来维持我的连接以便按nexmo的要求发送短信。

Who knows how to proceed? 谁知道如何进行?

Sending multiple messages, and using keep-alive are two separate things. 发送多个消息以及使用keep-alive是两件分开的事情。 With Nexmo (as the FAQ mentions) you can only send a single SMS per HTTP request. 使用Nexmo(如FAQ所述),每个HTTP请求只能发送一个SMS。 To send multiple SMS, you just have to make multiple HTTP requests. 要发送多个SMS,您只需发出多个HTTP请求。

By default, Nexmo will allow your account to make 5 requests per second to the SMS API. 默认情况下,Nexmo将允许您的帐户每秒向SMS API发出5个请求。 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). 如果要最大程度地提高吞吐量,则需要确保尽可能快地发出请求(或者实际上,至少以5 /秒的速率限制速度发出请求)。

That's where the keep-alive comes into play, making sure you're sending the requests as fast as possible. 这就是keep-alive作用,确保您尽快发送请求。 The curl_setop docs reference a CURLOPT_FORBID_REUSE which allows: curl_setop文档引用了一个CURLOPT_FORBID_REUSE ,它允许:

TRUE to force the connection to explicitly close when it has finished processing, and not be pooled for reuse. TRUE强制连接在完成处理后显式关闭,并且不被池化以供重用。

So by default, curl is trying to use keep-alive , assuming you reuse the curl handle. 因此,默认情况下,curl尝试使用keep-alive ,前提是您重复使用curl手柄。 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. 如果您使用curl_setop()设置一个具有不同数字/消息的新CURLOPT_URL (重复使用curl句柄),默认情况下curl应该使用keep-alive

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. 但请记住,这并不会改变你怎么发送多条消息与Nexmo,它只是一个优化上,你发送信息的速度的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM