如何使用PHP中的sms api从mysql数据库发送批量短信到手机号码

时间:2013-03-02 06:32:37

标签: php mysql api sms

嗨我在尝试使用批量短信api在php发送批量短信我是一个新手所以我不知道这个bt我通过谷歌发现了一些脚本但是我尝试发送短信它显示了错误“NO DATA RECIEVED”任何人都可以帮助我 这是m脚本

<?php

    if (isset($_POST["submit"])) {


         require("includes/config.php");
         require("includes/dbconnect.php");
         require("includes/functions.php");

        $tourdb = DBSUFFIX . $_SESSION["cono"] . "_" . $_SESSION["tourname"];
        mysql_select_db($tourdb);

        $message = substr($_POST["sms"], 0, 160);

        $cond = "";
        if (!empty($_POST["district"])) {
            $district = trim($_POST["district"]);
            $cond .= "AND `district` = '$district'";
        }

        if (!empty($_POST["state"])) {
            $state = trim($_POST["state"]);
            $cond .= "AND `state` = '$state'";
        }

        if (!empty($_POST["for"])) {
            $for = trim($_POST["for"]);
            $cond .= "AND `visitingfor` = '$for'";
        }

        $ccond = "";
        if (!empty($_POST["Airline"])) {
            $air = trim($_POST["Airline"]);
            $ccond .= "AND `airline_code` = '$air'";
        }

        if (!empty($_POST["bjdate"])) {
            $date1 = trim($_POST["bjdate"]);
            $ccond .= "AND `dept_date` = '$date1'";
        }

        if (!empty($_POST["jbdate"])) {
            $date2 = trim($_POST["jbdate"]);
            $ccond .= "AND `dept_date` = '$date2'";
        }

        if (!empty($_POST["siscono"])) {
            $siscono = trim($_POST["siscono"]);
            $cond .= "AND `company` = '$siscono'";
        }

        if (!empty($_POST["batch"])) {
            $batch = $_POST["batch"];
            $ccond .= " AND `batchno` = '$batch' ";
        }

        $hajicond = "";
        if (!empty($_POST["fromhaji"]) && !empty($_POST["tohaji"])) {
            $fromhaji = $_POST["fromhaji"];
            $tohaji = $_POST["tohaji"];
            $hajicond = " AND `hajino` BETWEEN '$fromhaji' AND '$tohaji'";
        }

        $paxcond = "";
        if (!empty($_POST["frompax"]) && !empty($_POST["topax"])) {
            $frompax = $_POST["frompax"];
            $topax = $_POST["topax"];
            $paxcond = " AND `paxid` BETWEEN '$frompax' AND '$topax'";
        }

        $msg = $_POST["sms"];



        $ctr = 0;
        $query = mysql_query("SELECT RIGHT(`mobile`,10) as `mobile`  FROM `pax` WHERE LENGTH(`mobile`) >= 10 AND `paxid` IN (SELECT `paxno` FROM `airline_detail` WHERE paxno IS NOT NULL $ccond) $cond $hajicond $paxcond ") or die(mysql_error());
            while ($pax = mysql_fetch_array($query)){
                $ctr++;
                $to.= "$pax[mobile],";
             }  

    $to.= "8652372200";

    $user = "****";
    $password = "t*****"; 
    $mobilenumbers = "$to";
    $message = "$msg";
    $senderid = "****";
    $url = "http://tran.mobilogi.com/api/httpapi.php";
    $message = urlencode($message);
    $ch = curl_init();
    echo "$user";
    if (!$ch) {
        die("Couldn't initialize a cURL handle");
    }
    $ret = curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "username=$user&password=$password&to=$mobilenumbers&sender=$senderid&message=$message");

    $ret = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);



    $curlresponse = curl_exec($ch);
    if (curl_errno($ch))
        echo 'curl error : ' . curl_error($ch);

    if (empty($ret)) {

        die(curl_error($ch));
        curl_close($ch); 
    } else {
        $info = curl_getinfo($ch);
        curl_close($ch);

        echo $curlresponse;
        }



    }
    ?>

2 个答案:

答案 0 :(得分:1)

PHP或javascript中没有可以直接发送短信的功能。 SMS基本上基于Short Message Peer-to-Peer (SMPP) protocol,因此您无法以编程方式直接发送短信。是的你可以使用smsgateway或现有的api,你可以从php发出http请求。有许多短信服务提供商,如增值服务提供商,有许多免费和开源的smsgateway,如kannel,你可以用来发送/接收短信,或者你也可以配置your own gateway

您需要第三方发送您的邮件,您还需要付费才能发送邮件。我自己没有尝试过,但是这篇关于通过HTTP发送短信的教程似乎是一个很好的方法。它会让你

Use PHP and the HTTP protocol to send text-messages from your website through an SMS gateway.

答案 1 :(得分:0)

您可以使用our API。由于您已经通过curl library使用了http请求,因此使用我们为批量短信请求提供的两个选项之一是没有问题的(例如,请参阅this answer):

完整表格

当您描述要发送的每条消息时,所有遗漏的内容都将从模板中获取:

pom.xml

POST /sms/v1/{subAccountId}/many

紧凑形式

如果您只列举电话号码,而其他所有内容都来自模板:

{ "clientBatchId": "Demo#1001", "messages": [ { "destination": "6598760001" }, { "destination": "659876002", "source": "SenderId2", "clientMessageId": "id_100001" }, { "destination": "509750003", "country": "FR", "source": "SenderId3", "text": "Custom message: สวัสดี", "encoding": "UCS2", "clientMessageId": "id_100002" } ], "template": { "source": "DefaultSenderId", "text": "Default message for all phone numbers" } }

POST /sms/v1/{subAccountId}/many/compact

请查看此tutorial,以便更顺利地了解API。