【问题标题】:cURL doesn´t send content type properlycURL 无法正确发送内容类型
【发布时间】:2019-10-22 08:37:19
【问题描述】:

我正在尝试建立与 Klarna 游乐场服务器的连接。对于这种情况,我需要使用来自客户的 session_id 创建一个 HPP 会话。我已经通过 cURL 获得了 session_id,但是当我尝试发送另一个 cURL 请求时,内容类型为 NULL。

我尝试创建一个函数来正确设置 cURL 选项,它可以很好地创建一个 KP 会话以从 Klarna 接收 session_id,但不适用于 HPP 会话。

此代码用于设置 cURL 会话的选项,因为只是 URL 和数据在变化,我将它们作为参数提供。

private function executeCurl( $url, $data ) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->setHeader()); // set header for request
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); // set authorization
        curl_setopt($ch, CURLOPT_USERPWD, $this->username() . ":" . $this->password()); // set authorization
        curl_setopt($ch, CURLOPT_URL, $url); // set URL
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data); // set data for request
        curl_setopt($ch, CURLOPT_VERBOSE, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $curl = curl_exec($ch);

        $info = curl_getinfo($ch);
        var_dump($info);

        curl_close($ch);

        return $curl;
}

在此函数中正确创建了 KP session_id 并调用函数 hostpayment。

    public function start() {
        $data = $this->createJson();
        $curl = $this->executeCurl("https://api.playground.klarna.com/payments/v1/sessions", $data);

        $data = json_decode($curl,true);

        $session_id = $data["session_id"];

        $this->hostpayment($session_id);
    }

应在此函数中创建 HPP 会话。

    public function hostpayment($session_id) {
       $url = "https://api.playground.klarna.com/payments/v1/sessions/" . $session_id;

       $data = $this->createSessionJson($session_id);

       $curl = $this->executeCurl($url, $data);
       $data = json_decode($curl,true);

    }

我希望,除了 URL 和数据之外,两种情况下的 cURL 输出都是相同的。

curl_getinfo 为 KP 会话输出以下内容:


{ ["url"]=> string(54) "https://api.playground.klarna.com/payments/v1/sessions" ["content_type"]=> string(16) "application/json" ["http_code"]=> int(200) ["header_size"]=> int(290) ["request_size"]=> int(271)}

HPP 请求输出以下内容:

{ ["url"]=> string(91) "https://api.playground.klarna.com/payments/v1/sessions/53ac5196-ced3-7573-96c7-332a7b8ab0ae" ["content_type"]=> NULL ["http_code"]=> int(204) ["header_size"]=> int(212) ["request_size"]=> int(307)}

可以看到第二个请求的内容类型为null,HTTP代码为204,所以请求成功,但是header设置不正确。

【问题讨论】:

  • Guzzle 不是一个选项吗?
  • 我试过了,仍然没有设置内容类型,谢谢你的帮助
  • 你是如何尝试使用 Guzzle 的?您可以在您的问题中添加您所做的吗?

标签: php laravel curl


【解决方案1】:

当我试图找出问题时,经过数小时的调试后,我意识到 Klarna 使用非常相似的 URL,因此我不得不使用 https://api.playground.klarna.com/payments/v1/sessions/ 而不是 https://api.playground.klarna.com/hpp/v1/sessions/ 来接收正确的数据。

【讨论】:

    猜你喜欢
    • 2016-08-02
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-10
    • 2012-12-30
    相关资源
    最近更新 更多