簡體   English   中英

使用curl發送多個URL短信

[英]Using curl to send multiple url sms

我想將結果發送給學校的學生。 我已經在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");
}

使用curl_close($curlHandle); 關閉連接。 因此...

1.)將MySQL查詢中所有信息的數組/對象傳遞給函數。

2.)打開卷曲連接。

3.)循環迭代並執行所有迭代。

4.)關閉連接。

最好還是看一下curl-multi,它不會每次都重新認證,因此應該可以提高吞吐量。 http://php.net/manual/zh/function.curl-multi-init.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM