Search code examples
phppaypalpaypal-ipn

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.


Solution

  • Host, in the HTTP header, must be set to www.paypal.com. Notice the lack of http[s]://.