带有 PayPal 新 header 要求的 PHP Curl 帖子

标签 php curl paypal

我正在使用 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_setoptCURLOPT_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/

相关文章:

php - 如何在 ZEND SOAP AutoDiscovery 中设置这些复杂类型?

php - PHP echo 中的文本不会在浏览器中显示

php - 使用 Curl PhP5.6 而不是 Curl PhP7

curl - curl无法连接到本地主机端口80

javascript - 单击一次“阅读更多”按钮将不起作用,但多次单击将不起作用

curl - Elasticsearch 错误 curl : (7) Failed to connect to localhost port 9200: Connection refused but localhost:9200 works on browser

php - 携带POST数据到另一个页面

php - 在 POST、PayPal 表单之前捕获表单数据

Paypal 自定义 Logo 和摘要消失

php - 如何创建国际国家语言特定的 url