22

PayPal IPN を使用すると、エラー 400 が発生し続けます。

ループ$res内で応答が何であるかを確認するために、スクリプトにメールを送信させています。while (!feof($fp)) {}私はいつもエラーが発生します:HTTP/1.0 400 Bad Request

合計で私は戻ってきます:

HTTP/1.0 400 Bad Request
​Connection: close
Server: BigIP
Content-Length: 19
​Invalid Host Header

この後の最後の行は空白です。これが私のコードです。多くのものを変更しようとしましたが、何も機能しません。

$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$value = preg_replace('/(.*[^%^0^D])(%0A)(.*)/i','${1}%0D%0A${3}', $value);// IPN fix
$req .= "&$key=$value";
}

// post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";

$fp = fsockopen('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

if (!$fp) {
// HTTP ERROR
} else {
   fputs($fp, $header . $req);
   while (!feof($fp)) {
       $res = fgets ($fp, 1024);
       if (strcmp ($res, "VERIFIED") == 0) {
           //ADD TO DB
       } else if (strcmp ($res, "INVALID") == 0) {
           // PAYMENT INVALID & INVESTIGATE MANUALY!
           // E-mail admin or alert user
       }
   }
   fclose ($fp);
}

行を追加しました。これは送信前のヘッダーです。

 Host: www.sandbox.paypal.com
 POST /cgi-bin/webscr HTTP/1.0
 Content-Type: application/x-www-form-urlencoded
 Content-Length: 1096
4

6 に答える 6

45

curl などの HTTP ライブラリを使用するのではなく、自分でソケットを開くため、適切な HTTP プロトコル バージョンを設定し、POST 行のすぐ下にHTTP Host ヘッダーを自分で追加する必要があります。

$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";
于 2012-08-04T18:53:43.103 に答える
29

私は同じ問題を抱えていましたが、これらは必要な変更です。上記の回答のいくつかは、すべての問題を解決するわけではありません。

ヘッダーの新しい形式:

$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";  // www.paypal.com for a live site
$header .= "Content-Length: " . strlen($req) . "\r\n";
$header .= "Connection: close\r\n\r\n";

最後の行にある余分な \r\n のセットのみに注意してください。また、サーバーからの応答に改行が挿入されているため、文字列比較が機能しなくなったため、次のように変更します。

if (strcmp ($res, "VERIFIED") == 0) 

これに:

if (stripos($res, "VERIFIED") !== false)  // do the same for the check for INVALID
于 2012-10-02T00:15:36.380 に答える
2

https://www.x.com/content/bulletin-ipn-and-pdt-scripts-and-http-1-1

// post back to PayPal system to validate
$header .="POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .="Content-Type: application/x-www-form-urlencoded\r\n";
$header .="Host: www.paypal.com\r\n";
$header .="Connection: close\r\n";
于 2013-01-05T09:48:52.200 に答える
0

私は同じ問題を抱えていました、そして最も良いことはペイパルのサンプルコードを使うことです...それは完璧に機能します:https ://www.x.com/developers/PayPal/documentation-tools/code-sample/216623

于 2012-08-05T11:09:13.863 に答える
0

別の解決策は、比較の前に $res をトリミングすることです..

$res = fgets ($fp, 1024);

$res = trim($res); //NEW & IMPORTANT
于 2013-08-08T09:07:38.717 に答える