首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用ozeki服务器发送自动短信?

如何使用ozeki服务器发送自动短信?
EN

Stack Overflow用户
提问于 2014-05-24 10:38:10
回答 1查看 1.2K关注 0票数 1

我的问题是,我使用的是gets循环,但是只发送了一条消息。所有记录都存储在数据库中。我想将消息发送到存储在数据库中的所有数字。我怎么能这么做?

以下是我到目前为止尝试过的:

代码语言:javascript
复制
<?php

$sql   = "select * from subscribe where type ='$cat' and city ='$city'";
$query = mysql_query($sql);

if ($query != null) {
    while ($row = mysql_fetch_array($query)) {
        $name      = $row['name'];
        $phoneNum  = $row['fone'];
        $condition = 'true';
        $message   = "Hi Now you can buy your product.";
        include('sendsms.php');
        $debug     = true;

        ozekiSend($phoneNum, $message, $debug);
    }
}
?>

档案:sendsms.php

代码语言:javascript
复制
<?php
########################################################
# Login information for the SMS Gateway
########################################################

$ozeki_user     = "admin";
$ozeki_password = "abc123";
$ozeki_url      = "http://127.0.0.1:9501/api?";

########################################################
# Functions used to send the SMS message
########################################################

function httpRequest($url)
{
    $pattern = "/http...([0-9a-zA-Z-.]*).([0-9]*).(.*)/";
    preg_match($pattern, $url, $args);
    $in      = "";
    $fp      = fsockopen("$args[1]", $args[2], $errno, $errstr, 30);
    if (!$fp) {
        return("$errstr ($errno)");
    } else {
        $out = "GET /$args[3] HTTP/1.1\r\n";
        $out .= "Host: $args[1]:$args[2]\r\n";
        $out .= "User-agent: Ozeki PHP client\r\n";
        $out .= "Accept: */*\r\n";
        $out .= "Connection: Close\r\n\r\n";

        fwrite($fp, $out);
        while (!feof($fp)) {
            $in.=fgets($fp, 128);
        }
    }
    fclose($fp);
    return($in);
}

function ozekiSend($phone, $msg, $debug = false)
{
    global $ozeki_user, $ozeki_password, $ozeki_url;

    $url = 'username=' . $ozeki_user;
    $url.= '&password=' . $ozeki_password;
    $url.= '&action=sendmessage';
    $url.= '&messagetype=SMS:TEXT';
    $url.= '&recipient=' . urlencode($phone);
    $url.= '&messagedata=' . urlencode($msg);

    $urltouse = $ozeki_url . $url;
    if ($debug) {
        echo "Request: <br>$urltouse<br><br>";
    }

    //Open the URL to send the message
    $response = httpRequest($urltouse);
    if ($debug) {
        echo "Response: <br><pre>" .
        str_replace(array("<", ">"), array("&lt;", "&gt;"), $response) .
        "</pre><br>";
    }

    return($response);
}

########################################################
# GET data from sendsms.html
########################################################

$phonenum = $_POST['recipient']; //here how i receive data from while loop???//
$message  = $_POST['message'];
$debug    = true;

ozekiSend($phonenum, $message, $debug);
?>
EN

回答 1

Stack Overflow用户

发布于 2014-05-24 11:02:59

好吧,我把东西移了一下。

备注:

  • sendsms.php被一次又一次地包含在文件的顶部。
  • 但是在这里,通过将两个文件合并在一起,我完全删除了包含的内容。
  • while循环包含$name。还没用呢。因为ozekiSend()只接受3个参数:电话号码、消息和调试。我更改了它,所以在消息中使用了名称。

文件:multi_sms_send.php

代码语言:javascript
复制
<?php   
$sql   = "select * from subscribe where type ='$cat' and city ='$city'";
$query = mysql_query($sql);

if ($query != null) {
    while ($row = mysql_fetch_array($query)) {
        $name      = $row['name'];

        $phoneNum  = $row['fone'];
        $message   = "Hi ".$name.", now you can buy your product.";

        ozekiSend($phoneNum, $message);

        // for debugging, try the following line
        //echo ozekiSend($phoneNum, $message, true);
    }
}

########################################################
# Login information for the SMS Gateway
########################################################

$ozeki_user     = "admin";
$ozeki_password = "abc123";
$ozeki_url      = "http://127.0.0.1:9501/api?";

########################################################
# Functions used to send the SMS message
########################################################

function httpRequest($url)
{
    $pattern = "/http...([0-9a-zA-Z-.]*).([0-9]*).(.*)/";
    preg_match($pattern, $url, $args);
    $in      = "";
    $fp      = fsockopen("$args[1]", $args[2], $errno, $errstr, 30);
    if (!$fp) {
        return("$errstr ($errno)");
    } else {
        $out = "GET /$args[3] HTTP/1.1\r\n";
        $out .= "Host: $args[1]:$args[2]\r\n";
        $out .= "User-agent: Ozeki PHP client\r\n";
        $out .= "Accept: */*\r\n";
        $out .= "Connection: Close\r\n\r\n";

        fwrite($fp, $out);
        while (!feof($fp)) {
            $in.=fgets($fp, 128);
        }
    }
    fclose($fp);
    return($in);
}

function ozekiSend($phone, $msg, $debug = false)
{
    global $ozeki_user, $ozeki_password, $ozeki_url;

    $url = 'username=' . $ozeki_user;
    $url.= '&password=' . $ozeki_password;
    $url.= '&action=sendmessage';
    $url.= '&messagetype=SMS:TEXT';
    $url.= '&recipient=' . urlencode($phone);
    $url.= '&messagedata=' . urlencode($msg);

    $urltouse = $ozeki_url . $url;
    if ($debug === true) {
        echo "Request: <br>$urltouse<br><br>";
    }

    //Open the URL to send the message
    $response = httpRequest($urltouse);
    if ($debug === true) {
        echo "Response: <br><pre>" .
        str_replace(array("<", ">"), array("&lt;", "&gt;"), $response) .
        "</pre><br>";
    }

    return($response);
}
?>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23843985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档