发送多个号码的短信请求在一秒钟内PHP [英] Send multiple numbers SMS requests in one second PHP
问题描述
我想用一个API来发送短信。这是每秒发送几乎是短信,但我想用多线程/ pthread的发送多个短信在一秒钟在PHP。这怎么可能或如何从我的结束至少一次异步发送多个短信请求API服务器。
I'm trying to send SMS using an API. It is sending almost one SMS per second but i want to send multiple SMS in one second using multithreading/pthreads in PHP. How is it possible or how can i send multiple SMS request asynchronously to API server from my end at least time.
//Threads Class
class MThread extends Thread {
public $data;
public $result;
public function __construct($data){
$this->data = $data;
}
public function run() {
foreach($this->data as $dt_res){
// Send the POST request with cURL
$ch = curl_init("http://www.example.com");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dt_res['to']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$this->result = $http_code;
/**/
}
}
}
// $_POST['data'] has multi arrays
$request = new MThread($_POST['data']);
if ($request->start()) {
$request->join();
print_r($request->result);
}
任何想法将AP preciated。
Any idea will be appreciated.
推荐答案
您不一定需要使用线程异步发送多个HTTP请求。可以使用非阻塞的I / O,multicurl适于在这种情况下。没有与multicurl一些支持HTTP客户端。
示例(使用狂饮6 ):
You don't necessarily need to use threads to send multiple HTTP requests asynchronously. You can use non-blocking I/O, multicurl is suitable in this case. There are some HTTP clients with multicurl support. Example (using Guzzle 6):
$client = new \GuzzleHttp\Client();
$requestGenerator = function() use ($client) {
$uriList = ['https://www.google.com', 'http://amazon.com', 'http://github.com', 'http://stackoverflow.com'];
foreach ($uriList as $uri) {
$request = new \GuzzleHttp\Psr7\Request('GET', $uri);
$promise = $client->sendAsync($request);
yield $promise;
}
};
$concurrency = 4;
\GuzzleHttp\Promise\each_limit($requestGenerator(), $concurrency, function(\GuzzleHttp\Psr7\Response $response) {
var_dump($response->getBody()->getContents());
}, function(\Exception $e) {
var_dump($e->getMessage());
})->wait();
这篇关于发送多个号码的短信请求在一秒钟内PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!