Page 1 of 1

Send an SMS from PHP

Posted: Sat Nov 07, 2009 10:09 am
by grantp22
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 not 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:

Code: Select all

<?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("<",">"),$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);
 
?>

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

Request:
http://127.0.0.1:9501/api?username=admi ... 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

Re: Send an SMS from PHP

Posted: Sat Nov 07, 2009 1:26 pm
by grantp22
Ah, thanks McInfo, that is exactly what I was after!

Re: Send an SMS from PHP

Posted: Wed Dec 25, 2013 9:15 am
by McInfo
I was recently asked to recall my reply to this topic. Although I don't remember what I said four years ago, I imagine my solution was something like this:

Code: Select all

// After this line in ozekiSend()...
//$response = httpRequest($urltouse);

// Initialize an array to hold response variables
$vars = array();
// Find the line that begins with "action="
if (preg_match('/^action=.*?$/m', $response, $matches)) {
    // Fill the array with name-value pairs
    parse_str($matches[0], $vars);
}
// Return the array instead of the string
return $vars;
/*
print_r($vars);
Array
(
    [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:TEXT
    [acceptreport_messagedata] => 332
    [acceptreport_serviceprovider] => vodaphone
)
*/