Parse a query string to be sent as an array in a silent post (php)-ThrowExceptions

Exception or error:

I have a silent post made when completing a paypal payflow. It returns a long string like:

&AVSZIP=X&TYPE=S&BILLTOEMAIL=no%40one.com

I inserted the bold part of the following script, which I had found searching around and thought it would do the trick. Perhaps I am completely misunderstanding how this works, but I thought that it would define $proArray and then email that to me in the silent post. It is sending the silent post email, but with nothing inside.

Please tell me if more information is needed, or if I am just an idiot. I also tried the parse_str command, but I suppose I do not know how to use that correctly either.

<?php
//PLACE EMAIL BELOW:
$email="MYEMAIL@mydomain.com"; 
$req = "";
if ($_POST) 
{
// iterate through each name value pair
foreach ($_POST as $key => $value) 
{
  $value = urlencode(stripslashes($value));
  $req .= "&$key=$value";
}

**// Function to convert NTP string to an array
function NVPToArray($req)
{
    $proArray = array();
    while(strlen($req))
    {
        // name
        $keypos= strpos($req,'=');
        $keyval = substr($req,0,$keypos);
        // value
        $valuepos = strpos($req,'&') ? strpos($req,'&'): strlen($req);
        $valueval = substr($req,$keypos+1,$valuepos-$keypos-1);
        // decoding the respose
        $proArray[$keyval] = urldecode($valueval);
        $req = substr($req,$valuepos+1,strlen($req));
    }
}**

//write to file
$fh = fopen("logpost.txt", 'a');//open file and create if does not exist
fwrite($fh, "\r\n/////////////////////////////////////////\r\n");//Just for spacing in log file
fwrite($fh, $req);//write data
fclose($fh);//close file

//Email
$mail_From = "From: SilentPost@tester.com";
$mail_To = $email;
$mail_Subject = "POST EXISTS";
$mail_Body = $proArray;
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);

//

//if posted return echo response
echo $req;
}

// No post data received
if (empty($_POST)) 
{
//write to file
$fh = fopen("logpost.txt", 'a');//open file and create if does not exist
fwrite($fh, "\r\n/////////////////////////////////////////\r\n");//Just for spacing in log file
fwrite($fh, "Empty Post");//write data
fclose($fh);//close file

//Email
$mail_From = "From: SilentPost@tester.com";
$mail_To = $email;
$mail_Subject = "Empty Post";
$mail_Body = "";
mail($mail_To, $mail_Subject, $mail_Body, $mail_From);

//if posted return echo response
echo "Empty Post";
}

?>
How to solve:

There seems to be a lot wrong with this script.

First of all, you’re not calling your new NVPToArray() function, so none of that code gets executed.

Secondly, even if you were calling that function, it doesn’t return proArray, so that data would be lost.

Thirdly, you try to assign $proArray (which isn’t defined in the context you’re using it, and even if it was, you aren’t calling your NVPToArray() function to fill it) to $mail_body, but the mail() function takes a String, and $proArray would be an array. So even if everything else worked, when $proArray is cast as a string, all you would get in your mail body would be the word ‘Array’. You have to take the keys and values in $proArray and make a string out of it. The easiest way would probably be:

implode("\n", array_map(function($k, $v) { return $k . ' = ' . $v; }, array_keys($proArray), $proArray));

Which, given an array:

array('AVSZIP' => 'X', 'TYPE' => 'S', 'BILLTOEMAIL' => 'no@one.com')

would return

AVSZIP = X
TYPE = S
BILLTOEMAIL = no@one.com

To get your script to work, the changes I would suggest you make are:

1) Add to the end of the NVPToArray function().

return $proArray;

2) Then replace the line

$mail_Body = $proArray;

with

$proArray = NVPToArray($req);
$mail_body = implode("\n", array_map(function($k, $v) { return $k . ' = ' . $v; }, array_keys($proArray), $proArray));

Answer´╝Ü

It looks like all I really needed to do was change one line:

$req .= "&$key=$value";

to:

$req .= "$key = $value\n";

and keep $req as the $mail_Body:

$mail_Body = $req;

And not use the function NVPToArray at all. Maybe I was explaining it wrong or didn’t provide the context before (from my own lack of knowledge of the scripts), but this is the answer I was looking for.

Now I have a different question about it, but that will be for a different post altogether.

Leave a Reply

Your email address will not be published. Required fields are marked *