Search code examples
phparraysparsingpaypalpaypal-ipn

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


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";
}

?>

Solution

  • 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.