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

Posted on 2006-03-21
Last Modified: 2008-03-17
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 :


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?


Question by:Dionysus
    LVL 1

    Author Comment

    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.

    LVL 15

    Accepted Solution

    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"

    $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
    LVL 1

    Author Comment

    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!

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now