我正在使用 Paypal 为 Paypal Payments Pro 或 Direct Pay 提供的 PHP 示例代码。在最近一封来自 Paypal 的电子邮件中,他们声明
Starting October 7, 2013, we will require all incoming requests to have a “Host” header which complies with HTTP 1.1 Specifications. This header was not required under HTTP 1.0. IPN and PDT scripts using HTTP 1.0 may start failing with “HTTP/1.0 400 Bad Request” errors after October 7, 2013, which will result in IPN messages not being validated successfully, or PDT scripts not being able to retrieve transaction information.
他们给了我以下代码用作标题:
$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\r\n";
但是使用 CURL,我怎样才能传递所有这些呢?
我知道我可以将其中的大部分设置在一个数组中并将其放在 CURLOPT_HEADER 下,但是第一行呢?
这是我的代码。请帮助我了解如何正确添加新 header 请求。
$curl = curl_init();
curl_setopt($curl, CURLOPT_VERBOSE, 1);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_URL, $api_endpoint);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $nvp_string);
$result = curl_exec($curl);
curl_close($curl);
最佳答案
目前标题被存储在一个字符串中。要使用 cURL
发送自定义 header ,您可以使用 curl_setopt
与 CURLOPT_HTTPHEADER
如下:
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
"POST /cgi-bin/webscr HTTP/1.1\r\n",
"Content-Type: application/x-www-form-urlencoded\r\n",
"Host: www.paypal.com\r\n",
"Connection: close\r\n\r\n"
)
);
关于带有 PayPal 新 header 要求的 PHP Curl 帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18959065/