Jump to content

[SOLVED] Send an SMS from PHP


grantp22

Recommended Posts

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("<",">"),$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=admin&password=admin&action=sendmessage&originator=5554443333&messagetype=SMS:TEXT&recipient=grant&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

Link to comment
Share on other sites

Lots of ways you could do it. If all you want to do is check that particular value though, why not just use...

 

if(strstr('acceptreport.statusmessage=Message+accepted+for+delivery')) {
   // message accepted
} else {
   // message not accepted
}

Link to comment
Share on other sites

Cags, that would work, but the response string returns other values too, this is an example of the returned string in red::

 

action=sendmessage&acceptreport.statuscode=0&acceptreport.statusmessage=Message+accepted+for+delivery&acceptreport.messageid=e613ba8e-c3b9-4089-ad00-f2dbee850a9b&acceptreport.originator=5554443333&acceptreport.recipient=grant&acceptreport.messagetype=SMS%3aTEXT&acceptreport.messagedata=5556&acceptreport.serviceprovider=LOOPBACK1

 

I would need to have the string only return 'acceptreport.statusmessage=Message+accepted+for+delivery' in order for your solution to work, but the string returns other info with that. How would I select a specific part of the string to compare against?

 

 

Link to comment
Share on other sites

Cags, that would work, but the response string returns other values too, this is an example of the returned string in red::

 

action=sendmessage&acceptreport.statuscode=0&acceptreport.statusmessage=Message+accepted+for+delivery&acceptreport.messageid=e613ba8e-c3b9-4089-ad00-f2dbee850a9b&acceptreport.originator=5554443333&acceptreport.recipient=grant&acceptreport.messagetype=SMS%3aTEXT&acceptreport.messagedata=5556&acceptreport.serviceprovider=LOOPBACK1

 

I would need to have the string only return 'acceptreport.statusmessage=Message+accepted+for+delivery' in order for your solution to work, but the string returns other info with that. How would I select a specific part of the string to compare against?

 

 

 

Erm... no it wouldn't require that at all. The function strstr looks if string a contains string b. Perhaps that would have been more obvious if I hadn't been rushing so much and passed both arguments in.

 

if(strstr($response, 'acceptreport.statusmessage=Message+accepted+for+delivery')) {
   // message accepted
} else {
   // message not accepted
}

Link to comment
Share on other sites

Add this into your code and manually look if the string from $needle is contained in $haystack exactly. I'd personally copy the needle value and use 'Find' in your browser make sure it picks up both strings.

 

$haystack = $response;
$needle = 'acceptreport.statusmessage=Message+accepted+for+delivery';

echo '<pre>';
echo $haystack;
echo '<br/>';
echo '<br/>';
echo $needle;
echo '<pre>';

Link to comment
Share on other sites

Sorry cags, kept missing you there, having some problems with my wireless 3G! it works just fine, I had typo in the string, it works great thanks the help...

 

if(strstr($response,'acceptreport.statusmessage=Message+accepted+for+delivery')) {
   // message accepted
   echo 'message accepted';
} else {
   // message not accepted
   echo 'message not accepted';
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.