【问题标题】:PHP Curl Post with PayPal's NEW Header RequirementPHP Curl Post 与 PayPal 的新标题要求
【发布时间】:2013-09-23 12:18:07
【问题描述】:

我正在使用 Paypal 为 Paypal Payments Pro 或 Direct Pay 提供的 PHP 示例代码。在最近来自 Paypal 的电子邮件中,他们声明

从 2013 年 10 月 7 日开始,我们将要求所有传入请求都具有符合 HTTP 1.1 规范的“主机”标头。 HTTP 1.0 不需要此标头。使用 HTTP 1.0 的 IPN 和 PDT 脚本可能会在 2013 年 10 月 7 日之后开始失败并出现“HTTP/1.0 400 Bad Request”错误,这将导致 IPN 消息无法成功验证,或者 PDT 脚本无法检索交易信息。

他们给了我以下代码用作标题:

$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 下,但是第一行呢?

这是我的代码。请帮助我了解如何正确添加新的标头请求。

$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);

【问题讨论】:

    标签: php curl paypal


    【解决方案1】:

    当前标题被存储在一个字符串中。要使用cURL 发送自定义标头,您可以使用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"
      )
    );
    

    【讨论】:

    • 谢谢!我不确定 POST 行,因为它不是 key:pair 组合。
    • @swg1cor14:非常好。很高兴我能帮上忙! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 2015-10-31
    • 2012-01-18
    相关资源
    最近更新 更多