【发布时间】:2015-11-05 12:09:14
【问题描述】:
我想将结果发送给学校的学生。我已经在 mysql 数据库中有结果和每个学生的电话号码。从我的小研究中,我发现 curl 是发送 url 短信的最有效方式。为了将结果发送给每个学生,我使用了一个 while 循环来选择每个学生的电话和结果,并对照数据库表中的考试名称(结果)。现在,问题是只发送了一条消息。我尝试再次检查代码,但找不到任何东西。请问我该如何解决这个问题?以下是我的代码:
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");
}
【问题讨论】: