Parse SMS MessageID with PHP and Insert into Database?

Parse SMS MessageID with PHP and Insert into Database? SearchSearch
Author Message
Daniel Tok
New member
Username: Dantok

Post Number: 5
Registered: 02-2009
Posted on Friday, February 27, 2009 - 10:18 pm:   

I`m creating a script in PHP using your default codes.

When using this.

$x = sendSMS();
echo $x;

It works, and it outputs the details of the sent message.

HTTP/1.0 200 OK Content-type: text/html Expires: Tue, 01 Jan 1980 1:00:00 GMT Cache-Control: no-cache
Message Submitted

Continue

MessageID=499DBC2D.req, Recipient=+65933xxx

Is it possible to extract the MessageID and the Recipient Data and insert it into a MySQL Database?

If so? How, What variables can i use when it returns the value, all help would be lovely and useful :-)
Bryce Norwood - NowSMS Support
Board Administrator
Username: Bryce

Post Number: 7753
Registered: 10-2002
Posted on Saturday, February 28, 2009 - 03:24 pm:   

Hi Daniel,

Parsing out that information is relatively simple.

Let me give you an example.

First, I'm going to write a simple helper function that we're going to use for parsing. It looks for a substring ($needle) within a string ($haystack). If it finds the substring, then it truncates the string so that the substring is not included.

Here's the function:
 
function TruncateString($haystack, $needle) {

//Find the position of $needle
$pos=strpos($haystack,$needle);

// If not found, no truncation required
if ($pos == FALSE) return $haystack;

// Truncate string at $needle
return substr($haystack,0,$pos);

}

The SendSMS function returns the HTTP response. In the example script, it is returned in a variable $x.

Here's some sample code to parse the MessageID and Recipient values from $x:
 
while ($x) {
$x = strstr ($x, "MessageID=");
if ($x) {
$msgidString = substr ($x, 0, strpos($x,"\r\n"));
if (!$msgidString) $msgidString = $x;
$recipString = strstr ($msgidString, "Recipient=");
if ($recipString) {
// Skip "MessageID="
$msgidString = substr ($msgidString, 10);
// Truncate message id at " "
$msgidString = TruncateString ($msgidString, " ");
// Truncate message id at ","
$msgidString = TruncateString ($msgidString, ",");
// If message id includes ".req", remove it
$msgidString = TruncateString ($msgidString, ".req");
// Skip "Recipient="
$recipString = substr ($recipString, 10);
// Truncate recipient at " " -- shouldn't be necessary
$recipString = TruncateString ($recipString, " ");
// Truncate recip at "," -- shouldn't be necessary
$recipString = TruncateString ($recipString, ",");
echo "Debug: MSGID=" . $msgidString . " ... RECIP=" . $recipString . "\n";
}
$x = strstr ($x, "\r\n");
}
}

The code is relatively simple, we're parsing the response line by line, looking for the "MessageID=" responses. For each one that we find, we're parsing the values out into two separate variables: $msgidString and $recipString.

In the example, we're just echoing them ... in your code, you would use them to update a database.

Hope that helps.

-bn
Daniel Tok
New member
Username: Dantok

Post Number: 6
Registered: 02-2009
Posted on Sunday, March 01, 2009 - 10:32 am:   

Hey mate,
Thanks for the code Tested it and it works perfectly. However, i need to understand this code better, the portion below, that i have pasted, what does it do? It's just a checker to see if the Message ID exists? Perhaps you could put some more comments for that part of the code, so i can understand better what it does ;)

Thanks in advance thou =)

while ($x) {
$x = strstr ($x, "MessageID=");
if ($x) {
$msgidString = substr ($x, 0, strpos($x,"\r\n"));
if (!$msgidString) $msgidString = $x;
$recipString = strstr ($msgidString, "Recipient=");
if ($recipString) {


$x = strstr ($x, "\r\n");
Bryce Norwood - NowSMS Support
Board Administrator
Username: Bryce

Post Number: 7754
Registered: 10-2002
Posted on Monday, March 02, 2009 - 04:32 pm:   

Hi Daniel,

Basically what the code is doing is looping through the response, parsing out each "MessageID=" / "Recipient=" pair.

It's a little extra tough to read, because I can't easily indent lines of code for readability here.

Each pass through the loop, the $x variable keeps progressing through the response.

It's easiest to look at that logic by focusing on each time $x gets updated.

$x = strstr ($x, "MessageID=");

$x now points to the next occurrence of "MessageID=" in the response, or it is NULL.

"if ($x)" tests if it is non-NULL and performs actions only if it is not NULL ... meaning that we found another occurrence of "MessageID=".

Before the close of the "if ($x)" condition, we do "$x = strstr ($x, "\r\n")" to move $x to the next line break, so that it is no longer pointing to the "MessageID=" string that we are processing. We can then re-enter the loop to find the next occurrence.

What else should I explain?

$msgidString = substr ($x, 0, strpos($x,"\r\n"));

This creates a new string, $msgidString which contains the contents of $x up to the next line break. So essentially, $msgidString now contains "MessageID=xxxxxxx.req, Recipient=#####", while $x contains this string plus the rest of the HTTP response.

if (!$msgidString) $msgidString = $x;

The above should never happen, as the previous line of code should have set $msgidString. I'm just in a habit of extra unnecessary error handling.

$recipString = strstr ($msgidString, "Recipient=");

The above parses out the part of the string that starts with "Recipient=", so that $recipString will now contain "Recipient=#####".

That should fill in the blanks. It's probably easiest to explain with an example.

Let's say you submit a message to 2 recipients, and you get this response:

HTTP/1.0 200 OK Content-type: text/html Expires: Tue, 01 Jan 1980 1:00:00 GMT Cache-Control: no-cache
Message Submitted

Continue

MessageID=499DBC2D.req, Recipient=+65933xxx
MessageID=499DBC23.req, Recipient=+65934xxx

After this statement is executed the first time:

$x = strstr ($x, "MessageID=");

$x now has the following value:

MessageID=499DBC2D.req, Recipient=+65933xxx
MessageID=499DBC23.req, Recipient=+65934xxx

After this statement is executed:

$msgidString = substr ($x, 0, strpos($x,"\r\n"));

$msgidString = "MessageID=499DBC2D.req, Recipient=+65933xxx"

After this statement is executed:

$recipString = strstr ($msgidString, "Recipient=");

$recipString = "Recipient=+65933xxx"

The inner logic then further parses those strings so that:

$msgidString = "499DBC2D"
$recipString = "+65933xxx"

When you get down to here:

$x = strstr ($x, "\r\n");

Now $x is modified to skip to the end of the current line, $x contains a line break followed by the next line of the HTTP response:

MessageID=499DBC23.req, Recipient=+65934xxx


Hopefully that makes more sense.

-bn