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

时间:2014-05-24 10:38:10

标签: php

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

这是我到目前为止所尝试的内容:

<?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

<?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);
?>

1 个答案:

答案 0 :(得分:1)

好的,我把事情转移了一下。

注意:

  • 一遍又一遍地包括sendsms.php,将其包含在文件顶部一次
  • 但是在这里我通过将两个文件合并在一起来删除include completly
  • while循环包含$ name。它未被使用。因为ozekiSend()只接受3个参数:电话号码,消息和调试。我改变了,所以 名称用于消息。

文件: multi_sms_send.php

<?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);
}
?>