使用 curl 发送多条 url 短信
Using curl to send multiple url sms
我想将结果发送给学校的学生。我已经在 mysql 数据库中获得了结果和每个学生的 phone 编号。从我的小研究中,我发现 curl 是发送 url 短信最有效的方式。为了将结果发送给每个学生,我使用了一个 while 循环 select 每个学生 phone 并将结果与数据库中的考试名称 table (结果)进行对比。现在,问题是只发送了一条消息。我试过再次检查代码,但找不到任何东西。请问我该如何解决这个问题?下面是我的代码:
class SendSMS
{
private $url = 'http://www.domain.com/http/index.aspx?account=acct&password=pwd';
function __construct()
{
}
// public function to commit the send
public function send($message,$recipient)
{
$url_array= array(
'message'=>$message,
'sendto'=>$recipient
);
$url_string = $data = http_build_query($url_array, '', '&');
// we're using the curl library to make the request
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $this->url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $url_string);
curl_setopt($curlHandle, CURLOPT_POST, 1);
$responseBody = curl_exec($curlHandle);
$responseInfo = curl_getinfo($curlHandle);
curl_close($curlHandle);
return $this->handleResponse($responseBody,$responseInfo);
}
private function handleResponse($body,$info)
{
if (substr($body, 0, 2) == "OK"){ // successful submission
$_SESSION['success'] = "[$body]: Your SMS(s) have been sent";
return true;
}
else{
$_SESSION['error'] = "An error has occurred: [$body].";
// error handling
return false;
}
}
}
下面是发送多条消息的 while 循环:
$sql = mysqli_query($conn, "SELECT * FROM result_table WHERE exam='".$_POST['examName']."'") or die(mysqli_error($conn));
$std = mysqli_fetch_assoc($sql);
while ($std = mysqli_fetch_array($std_query)) {
$recipient = $std['parent_phone'];
$sms = new SendSMS();
$sms->send($msssg,$recipient,"header");
}
使用 curl_close($curlHandle);
关闭连接。因此...
1.) 将来自 MySQL 查询的所有信息的 array/object 传递给函数。
2.) 打开 curl 连接。
3.) 循环迭代并全部执行。
4.) 关闭连接。
最好看看 curl-multi,它不会每次都重新验证,所以应该会让您获得更好的吞吐量。
http://php.net/manual/en/function.curl-multi-init.php
我想将结果发送给学校的学生。我已经在 mysql 数据库中获得了结果和每个学生的 phone 编号。从我的小研究中,我发现 curl 是发送 url 短信最有效的方式。为了将结果发送给每个学生,我使用了一个 while 循环 select 每个学生 phone 并将结果与数据库中的考试名称 table (结果)进行对比。现在,问题是只发送了一条消息。我试过再次检查代码,但找不到任何东西。请问我该如何解决这个问题?下面是我的代码:
class SendSMS
{
private $url = 'http://www.domain.com/http/index.aspx?account=acct&password=pwd';
function __construct()
{
}
// public function to commit the send
public function send($message,$recipient)
{
$url_array= array(
'message'=>$message,
'sendto'=>$recipient
);
$url_string = $data = http_build_query($url_array, '', '&');
// we're using the curl library to make the request
$curlHandle = curl_init();
curl_setopt($curlHandle, CURLOPT_URL, $this->url);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, CURLOPT_POSTFIELDS, $url_string);
curl_setopt($curlHandle, CURLOPT_POST, 1);
$responseBody = curl_exec($curlHandle);
$responseInfo = curl_getinfo($curlHandle);
curl_close($curlHandle);
return $this->handleResponse($responseBody,$responseInfo);
}
private function handleResponse($body,$info)
{
if (substr($body, 0, 2) == "OK"){ // successful submission
$_SESSION['success'] = "[$body]: Your SMS(s) have been sent";
return true;
}
else{
$_SESSION['error'] = "An error has occurred: [$body].";
// error handling
return false;
}
}
}
下面是发送多条消息的 while 循环:
$sql = mysqli_query($conn, "SELECT * FROM result_table WHERE exam='".$_POST['examName']."'") or die(mysqli_error($conn));
$std = mysqli_fetch_assoc($sql);
while ($std = mysqli_fetch_array($std_query)) {
$recipient = $std['parent_phone'];
$sms = new SendSMS();
$sms->send($msssg,$recipient,"header");
}
使用 curl_close($curlHandle);
关闭连接。因此...
1.) 将来自 MySQL 查询的所有信息的 array/object 传递给函数。
2.) 打开 curl 连接。
3.) 循环迭代并全部执行。
4.) 关闭连接。
最好看看 curl-multi,它不会每次都重新验证,所以应该会让您获得更好的吞吐量。 http://php.net/manual/en/function.curl-multi-init.php