Send an SMS using PHP

Discussion in 'PHP' started by grantp22, Nov 7, 2009.

  1. #1
    Hi, I am using sms server software named Ozeki NG it has an http api which allows you to send sms'es from your website and works great, but I can't seem to figure out how to extract specific values from the response it sends back to my php page. The response is set to send back a url encoded response which is not sent as an array which makes things more difficult for me.

    Is there a way that I can convert the received response into an array so I can validate whether or not the message was actually received or not by the sms software or am I missing something very obvious and simpler! I want to be able to validate certain values contained in the response back from the sms server, similar to the way I have shown in red in the code below.

    It is important for me to know whether or the message was actually accepted or not, I can't just blindly submit messages the way the code is at the moment for my particular application, so I need a way to verify certain values contained in the response string!

    Here is the PHP for sending the sms and receiving a response from the sms server software:

    <?php
     
    ########################################################
    # Login information for the SMS Gateway
    ########################################################
     
    $ozeki_user = "admin";
    $ozeki_password = "admin";
    $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.= '&originator=5554443333';
          $url.= '&messagetype=SMS:TEXT';
          $url.= '&recipient='.urlencode($phone);
          $url.= '&messagedata='.urlencode($msg);
          $url.= '&responseformat=urlencoded';
     
          $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>"; }
     
          //THIS IS WHAT I WOULD LIKE TO RETRIEVE (ALL IN RED)
          [color=#FF0000]if ($response['acceptreport.statusmessage'] == 'Message+accepted+for+delivery') {
               //do something here;
          }else{
               //do something else here
          }[/color]
     
          return($response);
    }
     
     
     
    ########################################################
    # GET data from sendsms.html
    ########################################################
     
    $phonenum = $_POST['recipient'];
    $message = $_POST['message'];
    $debug = true;
     
    ozekiSend($phonenum,$message,$debug);
     
    ?>
    Code (text):

    And this is what is echoed to screen for sendsms.php:

    Request:
    http://127.0.0.1:9501/api?username=...ant&messagedata=332&responseformat=urlencoded

    Response:

    HTTP/1.1 200 OK
    Cache-Control: no-cache, must-revalidate
    Pragma: no-cache
    Content-Length: 329
    Content-Type: text/xml
    Last-Modified: Sat, 07 Nov 2009 17:10:47 GMT
    Server: OzekiNG/3.14.1 Microsoft-HTTPAPI/1.0
    Date: Sat, 07 Nov 2009 15:10:47 GMT
    Connection: close

    action=sendmessage&acceptreport.statuscode=0&acceptreport.statusmessage=Message+accepted+for+delivery&acceptreport.messageid=b20e32a6-74a7-4059-bfef-99a7d3561720&acceptreport.originator=5554443333&acceptreport.recipient=2223334444&acceptreport.messagetype=SMS%3aTEXT&acceptreport.messagedata=332&acceptreport.serviceprovider=vodaphone


    Here is the original article on their website explaining the usage of the http api: http://www.ozekisms.com/index.php?owpn=327

    The demo software can be downloaded from the same page from the menu at the top of the page... Any help will be highly appeciated!

    Thanks
    Grant
     
    grantp22, Nov 7, 2009 IP
  2. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #2
    Here is a function that will parse response text;
    
    function parseResponse($res){
    	$pos = strpos($res,"Connection: close");
    	$rmsg = substr($res,$pos,(strlen($res)-$pos));
    	$rmsg = str_replace("Connection: close","",$rmsg);
    	$rmsg = trim($rmsg);
    	parse_str($rmsg,$resp);
    	return $resp;
    }
    
    PHP:
    $res is full response text you get from server.

    Actually you get common http response from that server. First we split it into headers and response body by getting position of "Connection: close" string ( AFAIK headers and body is separated by double \r\n but I couldn't get actual response it this didn't work with my test string :) ). Than we get response body and parse it into array.

    Code I tested with:
    
    $res = "HTTP/1.1 200 OK
    Cache-Control: no-cache, must-revalidate
    Pragma: no-cache
    Content-Length: 329
    Content-Type: text/xml
    Last-Modified: Sat, 07 Nov 2009 17:10:47 GMT
    Server: OzekiNG/3.14.1 Microsoft-HTTPAPI/1.0
    Date: Sat, 07 Nov 2009 15:10:47 GMT
    Connection: close
    
    action=sendmessage&acceptreport.statuscode=0&acceptreport.statusmessage=Message+accepted+for+delivery&acceptreport.messageid=b20e32a6-74a7-4059-bfef-99a7d3561720&acceptreport.originator=5554443333&acceptreport.recipient=2223334444&acceptreport.messagetype=SMS%3aTEXT&acceptreport.messagedata=332&acceptreport.serviceprovider=vodaphone";
    
    function parseResponse($res){
    	$pos = strpos($res,"Connection: close");
    	$rmsg = substr($res,$pos,(strlen($res)-$pos));
    	$rmsg = str_replace("Connection: close","",$rmsg);
    	$rmsg = trim($rmsg);
    	parse_str($rmsg,$resp);
    	return $resp;
    }
    print_r(parseResponse($res));
    
    PHP:
     
    AsHinE, Nov 7, 2009 IP
  3. Pudge1

    Pudge1 Well-Known Member

    Messages:
    912
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    140
    Digital Goods:
    1
    #3
    It would just be easier if you wrote your own code. It is a lot simpler.
     
    Pudge1, Nov 7, 2009 IP