views:

236

answers:

1

Hi,

I'm a relative PHP newbie implementing a PayPal IPN listener and all seems to be working fine, except I dont really know how to check for a response code.

I've tried something ugly with cURL but it doesn't work at all (I'm not understanding cURL).

I've tried this piece of code that I grabbed from somewhere on the net:

$fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
$response_headers = get_headers($fp);
$response_code = (int)substr($response_headers[0], 9, 3);

... but it's not working (returns $response_code = 0).

So right now, I'm debugging my IPN code without checking for a Response 200.

Can anyone more experienced advise me on what's the proper/simple way to check this?

Thanks

+2  A: 

It's get_headers($url), not get_headers($fp). Unless i'm reading it totally wrong (and there's some other mode i've never seen), you need to pass it the URL you're reading from, not a socket handle. Actually, it apparently does its own GET, so it'd be useless for your current task.

fsockopen(...) is a lower level (TCP/IP) function. It returns a socket handle, not a CURL handle. Which is why you can't use curl_getinfo(...) on it. You want something like this...

$fp = curl_init('https://www.sandbox.paypal.com/cgi-bin/webscr');
curl_setopt($fp, CURLOPT_POST, true);
curl_setopt($fp, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($fp, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($fp);

$response_code = curl_getinfo($fp, CURLINFO_HTTP_CODE);

except as i remember, you need to add 'cmd=_notify-validate' to the post fields.

Don't use fsockopen(...). Yeah, i know, that's what the Paypal sample code does. But it's meant to run everywhere, and can't rely on CURL being installed. You can, so use it.

cHao
@cHao: Thanks, this is really useful.
Tom
Tested at the end.
Tom