【问题标题】:PayPal Orders v2 Create API in PHP - UNSUPPORTED_MEDIA_TYPEPayPal Orders v2 在 PHP 中创建 API - UNSUPPORTED_MEDIA_TYPE
【发布时间】:2020-12-13 23:39:28
【问题描述】:

我正在尝试使用此 PayPal API:https://developer.paypal.com/docs/api/orders/v2/#orders_create

这是我的 PHP 代码:

$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $uri, [
        'headers' => [
            'Accept' => 'application/json',
            'Accept-Language' => 'en_US',
            'Content-Type' => 'application/x-www-form-urlencoded',
            'Authorization' => "Bearer " . $this->token
        ],
        'form_params' => [
            "intent" => "CAPTURE",
            "purchase_units" => [
                "amount" => [
                    "currency_code" => "USD",
                    "value" => "100.00"
                ]
            ]
        ]
    ]
);

收到错误:

客户端错误:POST https://api.sandbox.paypal.com/v2/checkout/orders 导致415 Unsupported Media Type 响应: {"name":"UNSUPPORTED_MEDIA_TYPE","message":"请求载荷不是 支持","debug_id":"6bd372e5171ee","details":[(截断...)

【问题讨论】:

  • 您是否尝试将数据作为 json 发送,as they do in their docs? 在他们的文档中的“Content-Type”下,它说:“媒体类型。对于带有请求正文的操作是必需的.值为application/其中格式为json。"

标签: php paypal


【解决方案1】:

'Content-Type' => 'application/x-www-form-urlencoded',

不接受这种媒体类型,只接受application/json

关于如何发送 json,请参阅文档:http://docs.guzzlephp.org/en/stable/request-options.html#json

或者,使用 Checkout-PHP-SDK,这里有一些指南文档:https://developer.paypal.com/docs/checkout/reference/server-integration/


为了获得最佳用户体验,请在您的服务器上创建两条路由,一条用于“设置事务”,一条用于“捕获事务”,用于以下前端代码:https://developer.paypal.com/demo/checkout/#/pattern/server

【讨论】:

    猜你喜欢
    • 2021-05-11
    • 2021-04-05
    • 2020-03-10
    • 2021-10-20
    • 2020-12-05
    • 2021-01-26
    • 2020-10-30
    • 1970-01-01
    • 2021-09-19
    相关资源
    最近更新 更多