Page 1 of 1
CURL and Preg_match?... help needed
Posted: Mon Aug 28, 2006 10:09 pm
by suicide
so I have a credit card processor that suggested i use CURL to see if the transactions are approved
So I got the Curl part working (I think)
When the use enters his data into my form and presses submit my page uses curl to contact the Credit Card processor and pulls the page int oa variable
the pages datat looks like this
Code: Select all
<html><head><title>Response File Redirect</title></head><body onload="document.myForm.submit()"><form action="
https://gg.com/vposwebreponse.asp" method="POST" name="myForm"><input type="hidden" name="Transactionamount" value="1.00"><input type="hidden" name="Orderstring" value="4~Resients~1.00~1~N~||"></form></body></html>
The question is... i need several of those values to figure out what my script does next...
mainly this one
Code: Select all
<input type="hidden" name="szIsApproved" value="1">
if it is a 1 i need to do something, otheriwse the creidt card or info was junk
so i woudl love to do a $szIsApproved = pregmatch(ihavenoclue);
for all the impt variable
but how do i fish that data out of there for different variables? so i can do a
switch szIsApproved (or whatever vairable im getting info for)
case 1:
do this
case 0:
do this then
MANY tHanks in advance.... jst tell me wher eto send the beer.
Posted: Mon Aug 28, 2006 10:18 pm
by feyd
Generic response: What have you tried?
Posted: Tue Aug 29, 2006 6:23 am
by suicide
Code: Select all
$ret_data = curl_exec ($ch);
preg_match('/szIsApproved=(-*[0-9]*)/i', $ret_data, $arr);
switch ($arr[1])
{
case '1':
then do everything i want;
break;
case '0':
echo $error
break;
}
The problem is that it only sometime works like this, which has been ripping my head apart
and i need to support more then just the IsApproved, so if i coudl break down each value i need into a variable first and then go from there, I figured that would be the best shot.
no matter how much i read about reg expression i cant make sense of this stuff in my head, so if someone wants to try and explain that would be great too (teach a man to fish and all)
thansk again for your time
Posted: Tue Aug 29, 2006 8:36 am
by sweatje
Perhaps something on the order of
Code: Select all
preg_match('~<input[^>]*szIsApproved[^>]*value\s*=\s*["\']?\s*(-?\d+)~ims', $ret_data, $arr);
If it works, I would be happy to explain it.
edit: just noticed an unescaped single quote in the single quoted string. Makes me wish regex were first class citizens in PHP

Posted: Tue Aug 29, 2006 8:49 am
by suicide
holy hell that thing is scary
Posted: Tue Aug 29, 2006 9:40 am
by sweatje
Did it work?
Posted: Tue Aug 29, 2006 10:12 am
by suicide
so far so good
I wont get to test it in teh live enviroment till i get home tonight (ip restricted ftp)
But I set up
Code: Select all
<?php
$ret_data = '<html><head><title></form></body></html>';
//preg_match('/szIsApproved=(-*[0-9]*)/i', $ret_data, $arr);
preg_match('~<input[^>]*szIsApproved[^>]*value\s*=\s*["\']?\s*(-?\d+)~ims', $ret_data, $arr);
echo $arr[1];
?>
and it retruned a 1...
so care to explain how the hell that monster works?
Posted: Tue Aug 29, 2006 10:22 am
by suicide
im still playing with it...
is there a way to streamline that statement into assigning variables?
so I can get the value striaght to a variable like (right now this retruns a 1, im assuming that TRUE on the array)
$INTszIsApproved = preg_match('~<input[^>]*szIsApproved[^>]*value\s*=\s*["\']?\s*(-?\d+)~ims', $ret_data, $arr);
or should I just keep essiging differnet tings to arr[1]?
Posted: Tue Aug 29, 2006 10:38 am
by sweatje
suicide wrote:
so care to explain how the hell that monster works?
Sure:
'~<input[^>]*szIsApproved[^>]*value\s*=\s*["\']?\s*(-?\d+)~ims'
Code: Select all
' start a single quoted string
~ use the ~ character for a delimiter, this means the regex will have to end with a ~ and any real ~ would need to be escaped as \~ in the regex
<input look for the start of an input tag
[^>]* [] denotes a character class, basically a list of characters to match ^ is anything but, so [^>] means anything but a > character, with the * being a shortcut repition modifier for zero or more
szIsApproved then the string you are looking for
[^>]* another of the same, as much as you can grab up to the end of the input tag
value then looking for the word value
\s* \s means whitespace, * meaning zero or more, so \s* means "optionally some whitespace"
= then an = character
\s* more optional whitespace
["\']? a character class containg either a single or a double quote, with the ? meaning zero or one, interpret this as "optionally the start of a quoted identifier" the single quote has to be escaped so PHP doe not end the string there
\s* more optional whitespace
( the start of the first match, if preg_match returns true, this will be in $arr[1]
-? optionally a - character
\d+ one or more digits
) end of the capture group
~ims end of the regex, using case insensitivity, multiline and . grabs all modifiers (the last is unnecessary in this regex, but I am in the habit of adding it when I make a regex multiline).
' end the single quoted string
HTH
Posted: Tue Aug 29, 2006 10:41 am
by sweatje
suicide wrote:im still playing with it...
is there a way to streamline that statement into assigning variables?
so I can get the value striaght to a variable like (right now this retruns a 1, im assuming that TRUE on the array)
$INTszIsApproved = preg_match('~<input[^>]*szIsApproved[^>]*value\s*=\s*["\']?\s*(-?\d+)~ims', $ret_data, $arr);
or should I just keep essiging differnet tings to arr[1]?
No, preg_match will return a boolean for if the regex matched. I would suggest a function:
Code: Select all
function getIntFromName($name, $data) {
if (preg_match('~<input[^>]*'.preg_quote($name,'~').'[^>]*value\s*=\s*["\']?\s*(-?\d+)~ims', $data, $arr)) {
return $arr[1];
}
}
Posted: Tue Aug 29, 2006 11:22 am
by suicide
alright dude, you rule... where do i send the beer?
Posted: Tue Aug 29, 2006 12:17 pm
by sweatje
suicide wrote:alright dude, you rule... where do i send the beer?
That is ok, we are here to help.
As someone else's sig around here say "No applause, just throw money".

Maybe you can buy a copy of my book instead of buying me beer and kill two birds with one stone

Posted: Wed Aug 30, 2006 10:37 am
by suicide
Im still not following what the ~ does... I'll try and figure it tho
That book looks pretty scary too

You write anything a little less painful?
Thanks again man
Posted: Wed Aug 30, 2006 11:11 am
by sweatje
suicide wrote:Im still not following what the ~ does... I'll try and figure it tho
It is just a delimiter for the re, the start and end character for it. Typically / is the delimiter, but with many PHP expressions you would have to escape the / in end tags, so I just kind of got in the habit of using ~ because I don't run across a use for that specific character in may of the re I write.
suicide wrote:
That book looks pretty scary too

You write anything a little less painful?
Thanks again man
Wrote about a 1/3 of a book on PHP graphics (specifically on using JpGraph for charting) but that one is out of print now.
I have a few power point presentations up on my blog from various PHP conference presentations I have made.
Other than that, just random blog and forum postings.
Oh, and my senior honors thesis from college: "The Application of Neural Networks to the Forecasting of Economic Time Series Data"
