【问题标题】:Using curl to send multiple url sms使用 curl 发送多个 url 短信
【发布时间】: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");
}

【问题讨论】:

    标签: php mysql url curl sms


    【解决方案1】:

    使用curl_close($curlHandle); 关闭连接。所以……

    1.) 将来自 MySQL 查询的所有信息的数组/对象传递给函数。

    2.) 打开 curl 连接。

    3.) 循环迭代并全部执行。

    4.) 关闭连接。

    最好还是看看 curl-multi,它不会每次都重新验证,因此应该可以让您获得更好的吞吐量。 http://php.net/manual/en/function.curl-multi-init.php

    【讨论】:

    • 我是 php 新手。请给我一个例子,说明如何循环迭代并执行数据库中的结果数组?
    • 您已经在循环使用 while 语句。只是不要在循环中关闭连接,在循环后关闭它。
    猜你喜欢
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多