Link to home
Start Free TrialLog in
Avatar of Refael
RefaelFlag for United States of America

asked on

Manipulate var sent in get mode

Hi
I have a PHP form that sends the information to a CGI script of a third party banking payment service. This third party sends back the variables it’s stored in GET mode to a PHP page on my website. What I need to do with these variables is to store them in my databases.

One of the important variable is called $ref
This variable stores the purchase information and the client information.

Here is an example of the URL (GET mode) they send back:

http://www.mydomain.com/confirm.php? ref=CDD%2004%20MARCHRefael%20Saro00%2030%2023refael@yahoo.comMoriz.9Germany%20Order%20Code:%20CDD%20.Tickets:%2010%20Order%20Code:%20CDD%20.Tickets:%202

This page is going to be viewed also by the client. So of course I will want to present all this data and also insert them to the database without any submit button.

My question is should I use implode function? Or how can I get this information so I can use it on the page and insert to the database?

Please! Try to give an example not only the idea :-)
Thanks
Avatar of TeRReF
TeRReF
Flag of Netherlands image

This will parse the string:

<?php
$s = ($_REQUEST['ref']);
$s_array = explode(" ", $s);
print_r($s_array);
?>

It is not really clear to me how the string is concatenated.
I.e. is CCD the name of the variable and 2004 it's value? If you give me more detail about how to pasre the string I can translate it into code
Avatar of Refael

ASKER

Thanks for your reply!

Some has variable indication and some has only the value
If you put off the "%20" sign you get this from the upper URL I submitted:

CDD 2004 MARCH (this is the name of product)
Refael Saro (client name)
003023 (client phone)
refael@yahoo.com (client email)
Moriz.9 Germany (client address)

here is a variable : "OrderCode" and its value is "CDD"
here is another variable: "Tickets" and its value is "10"
here is again the variable "OrderCode" and its value is "CDD"
here is again the variable "Tiksets" and its value is "2"
That is VERY hard to parse, I can hardcode it for you, of course, but it will fail if, for instance, some of the product names only have 1 space instead of 2.
It would be MUCH better if your third party banking payment service would send you a string we can work with. In my opinion it is quite an absurd way of returning values, I mean it is VERY inconsistent, there is no space between productname and clientname or client name and phone, to name two. There should be a different character to seperate the different vars/values. If you have the change, ask for something like:
http://www.mydomain.com/confirm.php?ref=CDD%2004%20MARCH|Refael%20Saro|00%2030%2023|refael@yahoo.com|Moriz.9Germany|Order%20Code:%20CDD|Tickets:%2010|Order%20Code:%20CDD|Tickets:%202
Avatar of Refael

ASKER

Wait because i think its my problem. this is how i send it to them. would it help if i will not include spaces and also also include all the var names? if it does i will chnage thsi in my php form and then i will try to test it ands i will submit here again the URL i get from them back. just pls let me know if it will help or how else should we need it to have it.

That will help a lot!
Use var names and values. And use 2 different seperators. That would be perfect. Something like:
http://www.mydomain.com/confirm.php?ref=pn#CDD%2004%20MARCH|cn#Refael%20Saro|cf#00%2030%2023|ce#refael@yahoo.com|ca#Moriz.9Germany|Order%20Code#CDD|Tickets#10|Order%20Code#CDD|Tickets#2

Even better would be to use (since it leaves nothing to parse):
http://www.mydomain.com/confirm.php?pn=CDD%2004%20MARCH&cn=Refael%20Saro etc...

I don't know if it is a requirement to put it in 1 string, if so use option 1, otherwise use option 2

Show me the return URL you get from them and I'll write the php code that handles it...

Good luck!
Avatar of Refael

ASKER

i can get the url to have "=" sign before the value of each var but i can not get it to have "|" or "&".

here an exmaple of what i send to them:

$retour = "CDD 04 MARCH".$Name.$refer1.$refer2;

this is what i tried to do:

$retour = "PN=CDD"."CN=".$Name (it works)

$retour = "PN#=CDD"."|"."FN#".$Name (does not give back the "#" and the "|" signs)

also tried $retour = "PN=CDD"."&"."CN=".$Name (does not give back the "&" sign)

what should i do, am i lost?
No, we'll just use another seperator and go from there...
Try this:
$retour = "PN=CDD===CN=".$Name."===CP=".$Phone etc.

I other words, we use === as a seperator. Show me what they send back when you do this...
Avatar of Refael

ASKER

The problem is that they force me to send all this value in one Var.
The rest of the values like price and transaction number they gave me their own Var name. so part of this URL I am getting does have the “&” sign but for this particular Var that I need to include all information that is important for me like, client name and address they force me to include them all in one Submitted Var as shown in my example above.

Or here again:
$retour = "CDD 04 MARCH".$FirstName.$FamilyName.$refer1.$refer2;
Avatar of Refael

ASKER

ok am doing it.............moment :-)
Avatar of Refael

ASKER

Ok first I am sorry since I am trying to figure how to work it out I didn't refresh the submitted page to the third party so your first solution might have worked.

Anyway I have deleted some of the submitted variables/values so it will be shorter. Now I get this:

ref=PN=CDD%2004%20MARCH===CN=Refael===CP=00%2020300%2023====TC=AEPN4===NT=5===TC=AEPSP4===NT=3

Let me know if it will be better to check the last version you suggested.
Avatar of Refael

ASKER

I thank A LOT for your patient!!!

Now I have this coming from their URL:

ref=PN#CDD%2004%20MARCH|CNRefael%20Sero|CP00%203030%20268|TCAEPN4|NT3|TCAEPSP4|NT6|

This is what I did and I hope its ok:

$retour = "PN#CDD 04 MARCH|"."CN#".$FirstName."|"."CP#".$Phone."|" and so.....

What i didn't tell you is that this is only part of the returned URL and basically there are other submitted variables I am getting in this URL which does have this sign "&" and that’s because they have assigned a different VAR to each one.

Here is an example of the full URL:

http://www.mydomain.compayboxconfirm.php?MONTAT=36900&REF=PN#CDD%2004%20MARCH|CNRefael%20Sero|CP00%203030%20268|TCAEPN4|NT3|TCAEPSP4|NT6|&AUTO=XXXXXX&TRANS=602604766

I them all capital letters to distinguish, these 3 VAR are: montat, ref, auto and trans.

I can not wait for you to tell me how to manipulate the data?
Avatar of Refael

ASKER

now i only notice that they put off the "#" sign! :-((((((
Wouldn't it be easier to store the info in a database before you send the client off to the payment service, and just pass the primary key (or some other unique identifier) as the ref=... value?

Then when the client returns you can just look up the value in your database and present them with a confirmation or whatever.

(or am I missing something?)
Hey, the # character only remained there at the PN var? or is that a typo from you or did they disappear from all the other vars?
This already looks very promising though, we'll manage to fix it, don't worry :)
Oh, you already answered my question... They do make life hard for you...

Oke, one more try, if this doesn't work, we'll fix it the hard way...
$retour = "PN|CDD 04 MARCH||"."CN|".$FirstName."||"."CP|".$Phone."||" and so.....

Pay attention, I've changed '#' into '|' and '|' into '||'
Avatar of Refael

ASKER

yes i see it and this is how it comes the first "#" is coming back but the rest are not.

i do not do the database first because the if was clear that if the client didnt process the payment part there is no need for inserting his data to the database. and second i think i will have the same problem as i do not have the var to check per submitted URL?

so what now back to this version?

ref=PN=CDD%2004%20MARCH===CN=Refael===CP=00%2020300%2023====TC=AEPN4===NT=5===TC=AEPSP4===NT=3

OR?
Avatar of Refael

ASKER

i think we are there???

ref=PN|CDD%2004%20MARCH||CN|refael%20sero||CP|0039%2039468||TC|AEPN4||NT|2||||TC|AEPSP4||NT|3||
Avatar of Refael

ASKER

the 3 times "|||" is my mistake, ignore it!
ASKER CERTIFIED SOLUTION
Avatar of TeRReF
TeRReF
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Any luck?
Avatar of Refael

ASKER

That’s strange

I GET THIS:

Array ( [PN] => CDD 04 MARCH [CN] => Refael Sero [CP] => 30 23639143 [TC] => CDD5 [NT] => 2 )

BUT there is the last product missing as you can see by the URL i copied:

ref=PN|CDD%2004%20MARCH||CN|Refael%20Sero||CP|30%2023639143||TC|CDD4||NT|1||TC|CDD5||NT|2

Somehow he took the last products and skipped the first products, is this because they have the same VAR name "TC" and "NT"?

If it is I will change it. Now can you please tell me how to deal with this so it will appear on the page for the client and put it to the database... just the way to do it. I am thankful!!!!
Avatar of Refael

ASKER

Ok, very funny these guys. I got an auto email from them with the subject "warning" there it stated the entire URL phrase as it should be (as we wanted it to be) and the warning say that its Impossible to join the URL for the payment.

Well I think I rest my case here! But still just so I can learn how you can manipulate the data from here.
You're right, duplicate keys are not allowed and only the last added value will remain. Renaming variables to prevent duplicates would be the best way to go...
Avatar of Refael

ASKER

Hi TeRReF,

I do not mind posting this question new. Let me know as you already answered and helped...

I really wish to know how you would get to assign keys and values from here, print them to the page or use them to insert into a database. I thank you a lot!
Avatar of Refael

ASKER

i would do something like this?

while (list($s, $s_array) = each ($vars)) {
echo $s." ".$s_array."<br>";
}

am i right?
Rafael, sorry you had to wait a while for my answer...

Now, let's see... part of this we had already:
<?php

$s = ($_REQUEST['ref']);
$s_array = explode("||", $s);
$vars = array();
foreach ($s_array as $var) {
  $tmp_array = explode("|", $var);
  if (count($tmp_array) == 2)
      $vars[$tmp_array[0]] = $tmp_array[1];
}
// $vars contains everything you need
// accessing the array directly is the fastest way
// like this:
foreach ($vars as $name=>$value) {
    echo $name.": ".$value."<br>";
}
?>
Avatar of Refael

ASKER

Genius really! I wish I would be that progressive and professional as you.
Last question I promise,
How can I use this for the database? Can I call each key and value, or? Can you show just an example?
You will be in a while :)

Ok, this should work, I just added code to generate the query, I assume you know how to open a DB connection etc.

<?php

function dbAdd($tablename, $vars) {
    $cols = '';
    $vals = '';
    foreach ($vars as $name=>$value) {
        $cols .= $name.',';
        $vals .= "'".$value."',";
    }
    // chop last comma
    $cols = substr($cols, 0, strlen($cols) - 1);
    $vals = substr($vals, 0, strlen($vals) - 1);
    // use this query to add everything to your DB
    $query = "INSERT INTO ".$tablename." (".$cols.")
              VALUES (".$vals.")";
    // depug print
    print($query);
}



$s = ($_REQUEST['ref']);
$s_array = explode("||", $s);
$vars = array();
foreach ($s_array as $var) {
  $tmp_array = explode("|", $var);
  if (count($tmp_array) == 2)
      $vars[$tmp_array[0]] = $tmp_array[1];
}
// I've added a function which is reusable for other inserts
dbAdd('tablename', $vars);

?>

Avatar of Refael

ASKER

I Thank you a lot TeRReF !
Glad to help!