prawnsalad / Nexmo-PHP-lib

Nexmo SMS gateway example library
MIT License
121 stars 98 forks source link

Added getOverview to return status as php object #8

Closed swahome closed 12 years ago

swahome commented 12 years ago

This is an alternative to diaplayOverview which instead of returning HTML, it returns an object with more details such as the number of message parts and  status code and messages as an array in the message_status property. A sample print_r of the object on success and failure is added to the comments for clarity

swahome commented 12 years ago

I have just tested this new method so no worries works as expected.

prawnsalad commented 12 years ago

I'm not sure what the benefit of this method would be? It only adds an English status string depending on the number of messages sent which should be left down to the application using it.

swahome commented 12 years ago

All I wanted to provide is a way to separate the actual results of displayOverview from the HTML tags as this separation of code and presentation would come in handy especially if the developer required to use a given display style or store the values in a database or simply to adhere to an MVC approach.

The object would also include all values available just in case they are required.

I had the problem of extracting the message sending results from displayOverview as it only returned HTML and I would have to sift through to get the pieces I required most importantly the messageid which I would require to store so as to compare with the values from the message receipt callbacks and know when this specific message is delivered.

I thought that such a function would be immensely useful as the user would not have to modify the NexmoMessage class to extract the values.

Thanks for having a look at it perhaps an illustration will do. here I have stored the values in a db and displayed them without modifying NexmoMessage class

   /**
     * To send a text message.
     *
     */

    // Step 1: Declare new NexmoMessage.
     $nexmo_sms = new NexmoMessage('api_key','api_secret');

    // Step 2: Use sendText( $to, $from, $message ) method to send a message. 
    $info = $nexmo_sms->sendText($to='+447234567890', $from ='Test', $message = 'Hello!');

    // Step 3: Get an overview of the message
    $result=$nexmo_sms->getOverview($info);
     $code=$result->code;
     $status_text=$result->status_text;
     $message_count=$result->message_count;

     //sample mysql insertion [assuming you have a mysql table with these fields 
//and an auto_increment int primary key]
     mysql_query("insert into sms(code,status_text,message_count) 
        values('$code','$status_text','$message_count')");
     $sms_id=mysql_insert_id();

     if($code==1){ //successful
         foreach($result->message_status as $msg)
         {

            $to=$msg->to;
            $price=$msg->messageprice;
            $status=$msg->status;
            $message_id=$msg->messageid;
            $balance=$msg->remainingbalance;
         }
        //sample mysql insertion for the component messages 
//[assuming you have a mysql table with these fields and an auto_increment int primary key]
        mysql_query("insert into sms_msgs(sms_id,to,price,status,message_id,balance) 
            values('$sms_id','$to','$price','$status','$message_id','$balance')");

     }

     //sample html output

     echo "Code: ".$result->code;
     echo "<br />Status: ".$result->status_text;
     echo "<br />Message Count: ".$result->message_count;

     if($code==1){
         foreach($result->message_status as $msg)
         {
            echo "<br />To: ".$msg->to;
            echo "<br />Price: ".$msg->mesageprice;
            echo "<br />Status: ".($msg->status==0)?'Message Sent':'SMS Sending Failed';
            echo "<br />Message ID: ".$msg->messageid;
            echo "<br />Balance: ".$msg->remainingbalance;
         }
     }

As you can see you can do anything with this object and you do not have to worry about styling the output or extracting values you are fully in control.

I appreciate your efforts and concern thanks.

prawnsalad commented 12 years ago

The displayOverview() method is purely there for quick debugging and would not generally be used in production code. I should have mentioned that as a comment in the code :)

You do not need to call displaymethod() as all the information is already in $info (returned from ->sendText()). print_r($info) to see what it contains.

Note in your code that when you check for a successful message, if the message is sent in 2 or more parts your code will think it failed. Use the following to check if the message was successful:

if (count($info->messagecount) == 0) {
    // Sending failed
} else {
    // Sending was successful
}
swahome commented 12 years ago

Thanks You should have added a comment I think. Now I am wondering what I was doing all along thanks.