Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PayPal IPN generating a HTTP 302 error using PHP

I have an IPN script that runs, and has worked for some time now. Recently I started getting an HTTP/1.1 302 Moved Temporarily as a response and cannot determine why.

The following is the code related to posting to PayPal and getting the response:

$sd = @fsockopen('ssl://www.paypal.com', 443, $errno, $errstr, 30);
if(!$sd) {
    $error = 'Error opening socket connection to PayPal: '.$errstr;
    quit($error, $errno);
}

$req = 'cmd=_notify-validate';
foreach($_POST as $key=>$value) $req .= "&{$key}=".urlencode(stripslashes($value));

// post back to PayPal to validate
$header  = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: ".strlen($req)."\r\n";
$header .= "Host: http://www.paypal.com/\r\n";
$header .= "Connection: close\r\n\r\n";

fputs($sd, $header.$req);
$response = '';
while(!feof($sd)) $response .= fgets($sd, 4096);
fclose($sd);

Note, all the connection, transfer, and responses work, I do not get and error. But the response from PayPal is not correct in that it does not provide VERIFIED or INVALID as stated in their documentation, but rather an HTTP 302 error.

like image 816
steveo225 Avatar asked Apr 27 '26 03:04

steveo225


1 Answers

I build my request this way (and it works). Maybe it can help you

$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
    $value = urlencode(stripslashes($value));
    //Fixes some special characters Paypal sends
    $value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}', $value);
    $req .= '&' . $key . '=' . $value;
}
like image 183
Machavity Avatar answered Apr 28 '26 16:04

Machavity



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!