PayPal IPN generating a HTTP 302 error using PHP

房东的猫 提交于 2019-12-12 13:04:39

问题


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.


回答1:


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;
}



回答2:


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



来源:https://stackoverflow.com/questions/19438962/paypal-ipn-generating-a-http-302-error-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!