Forum Moderators: coopster

Message Too Old, No Replies

Unpredictable code: processing HTTP response from bank

         

DaXiang

8:48 pm on Jul 25, 2010 (gmt 0)

10+ Year Member



Hope someone can help...

I'm trying to integrate my site with a payment processor and am having some trouble processing their HTTP response.

I'm checking for the word 'Processed' in the response string. Every time it's there, I need my code to echo 'WORKED'.

The problem is that my code is behaving very unpredictably. Sometimes when the bank returns a 'Processed' response, everything goes fine and I get the 'WORKED' result I'm looking for; other times, even for a 'Processed' response, my code inexplicably returns 'FAILED'...

Here is the code I'm using:



//the processed response I'm looking for from the bank
$p = "Processed";

//checking the HTTP response string for the 'Processed' response
$proc = strpos($fgets,$p);

if($proc === false){

//the result I'm sometimes getting even if the bank returned a 'Processed' response
echo "FAILED";

}
else if(!$mysqldb->increaseDeposit()){

//not usually getting this error - don't think there's a problem with the database function
echo "DATABASE ERROR";

}
else{

//the result I need every time I get a 'Processed' response from the bank
echo "WORKED";

}



All my other functions are working fine on the site, just this one which I can't fathom...

Anyone any ideas why it's behaving so unpredictably and/or suggestions as to how I could process this response reliably?

Readie

10:12 pm on Jul 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World DaXiang.

Might it be a upper/lower case issue? May be worth trying with stripos().

[uk2.php.net...]

DaXiang

2:57 am on Jul 27, 2010 (gmt 0)

10+ Year Member



Thanks for your advice.

I've tried with both strpos() and stripos(), but the whole thing is still really unreliable...

I have a feeling that the PHP code is fine, and the problem lies with the HTTP response - perhaps something to do with encoding?

Sometimes the response from the bank comes back 'chunked', other times with a set 'content-length'.

I'm going to investigate this article I found which shows how to communicate with a payment processor using PHP/cURL: [phpbuilder.com ]

I've heard that cURL has some built-in functions designed to deal with this kind of thing.

If anyone is a master of HTTP response processing with PHP, you know, just say the word.

DaXiang

2:59 am on Jul 27, 2010 (gmt 0)

10+ Year Member



(I'm surprised I can't find much about this on the web - lots of sites process payments right? Plenty people must know how to do it...)

SevenCubed

3:12 am on Jul 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For what it's worth the chunked return is typically associated with "<?php flush(); ?>". You may or may not be aware of it but it may give you a clue to work with.

I use it sometimes just after the <head></head> tag to flush the buffer to the client if I think it's going to improve page loading performance. There are varying opinions out there whether or not it's good to apply. Based on what I see in logs it is an advantage in the situations I've applied it.

Maybe the output buffer is being flushed before the full response has had time to jump onto the outgoing packet thereby only returning you partial content and causing it to fail sometimes while work others. Anyway, as I say, it might give you an idea.

Matthew1980

7:43 am on Jul 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there DaXiang,

I'm always hesitant about using "identical to" because every result may not be identical: [php.net ]

Just a thought there, and I could well be wrong, but that just sticks out to me, I only use "identical to" when I am checking for a string or data type that I set via a selection or radio, because you can be sure of that, returns from functions can be spurious at times.

change:-

if($proc === false){

to:-

if($proc == false){

Also strpos may not return a bool: [uk3.php.net ], have a read through the tech sheet for the function, it generally returns an int, so I think you may need to be more specific in what you are checking for.

Hope that makes sense :)

Cheers,
MRb

enigma1

3:10 pm on Jul 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The strpos check is fine. But where are you setting the $fgets var?

Because the part of code you posted is not the one with the problem.