0
我想將結果發送給學校的學生。我已經有了一個mysql數據庫中的結果和每個學生的電話號碼。從我的小研究中,我發現curl是發送url短信的最有效方式。爲了將結果發送給每個學生,我使用了一個while循環來選擇每個學生電話和結果,以反映數據庫表(結果)中的考試名稱。現在,問題是隻發送一條消息。我試着再次檢查代碼,但找不到任何東西。請問我該如何解決這個問題?下面是我的代碼:使用curl發送多個url短信
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");
}
我是新來的PHP。請你能給我舉一個例子,說明如何循環迭代並執行數據庫結果數組? – Flourish
您已經在循環使用while語句。只是不要關閉循環中的連接,在循環之後關閉它。 –
謝謝......它的工作 – Flourish