PHP and PayPal's returned $_POST variables - how to match variables to strings?

I am testing a system and I am at the point where I've made the (test) purchase and PayPal has sent me back to my return page with a long string of variables in the following format :

cmd=_notify-validate&payment_date=18%3A20%3A11+Mar+21%2C+2006+PST&txn_type=web_accept&last_name=Buyer&residence_country=CA&item_name=Merchant&payment_gross=&mc_currency=CAD&business=sales%40merchant.com&payment_type=instant&verify_sign=An5ns1Kso7MWUdW4ErQKJJJ4qi4-AhFhwMdtcrBWQYPu7jYen.7IBSuN&payer_status=unverified&test_ipn=1&tax=0.00&payer_email=test%40customer.com&txn_id=7WT019456541558329&quantity=1&receiver_email=someone%40merchant.com&first_name=Dion&payer_id=F34564RFQ5WQ&receiver_id=R4T13RSEJ5NA&item_number=1&payment_status=Completed&payment_fee=&mc_fee=1.79&shipping=0.00&mc_gross=42.75&custom=&charset=windows-1252&notify_version=2.1

Now I've taken this and assigned it to a variable:

$req = 'cmd=_notify-validate';

foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";


So far so good... now I tried to split the string so I could get 2 key/value pairs isolated. I want the payer_email and the payment_status separated from the rest, so I started with the following:

$postarray = explode("&", $req);


Now the problem is, how do I split it again and do a test for $postarray['payment_status']?

I can try and isolate the pairs using $postarray[14] but I'd like to specify by name in case the order of variables changes at some point in the future...

Can anyone help?

Thanks,
Dion

LVL 1
DionysusAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DionysusAuthor Commented:
Woops..I got it backwards... but my end question is the same.

I received a POST from PayPal, and using that code, created that long string.  Now I need to know how to break it down further as I said previously.

Thanks!
JakobACommented:
Not sure I understand you correctly. but here is a try:

$totalstring = "cmd=_notify-validate&payment_date=18%3A20%3A11+Mar+21%2C+2006+PST&txn_type=web_accept"
    ."&last_name=Buyer&residence_country=CA&item_name=Merchant&payment_gross=&mc_currency=CAD"
    ."&business=sales%40merchant.com&payment_type=instant"
    ."&verify_sign=An5ns1Kso7MWUdW4ErQKJJJ4qi4-AhFhwMdtcrBWQYPu7jYen.7IBSuN&payer_status=unverified"
    ."&test_ipn=1&tax=0.00&payer_email=test%40customer.com&txn_id=7WT019456541558329&quantity=1"
    ."&receiver_email=someone%40merchant.com&first_name=Dion&payer_id=F34564RFQ5WQ"
    ."&receiver_id=R4T13RSEJ5NA&item_number=1&payment_status=Completed&payment_fee=&mc_fee=1.79"
    ."&shipping=0.00&mc_gross=42.75&custom=&charset=windows-1252&notify_version=2.1"

$pair_array = explode( "&", $totalstring );
for ( $i=0; $i<count($pair_array; $i++ ) {
    $pair_array[$i] = explode( "=", $pair_array[$i] );
    ${$pair_array[$i][0]} = urldecode( str_replace( '+', ' ', $pair_array[$i][1] ) );   // turn %NN codes to characters and '+' to ' '
}
// now each field in the parameter string can be adressed by it name:
echo ">'$payment_status'<";

regards JakobA

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DionysusAuthor Commented:
Thanks man.. the only thing is that you had missed a closing bracket.. but apart from that.. you did exactly what I wanted.

Take care!
-Dion
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.