dtcyv3985 2019-06-14 06:40
浏览 376

Guzzle POST请求,授权标头不起作用

I'm trying to call the sandbox payu test api as given in

http://developers.payu.com/en/restapi.html#creating_new_order_api

And doing it using guzzle post request like this,

$client = new GuzzleHttp\Client();
$result = $client->request('POST', "https://secure.snd.payu.com/api/v2_1/orders", [
    'headers' => [
        'Content-Type'  => 'application/json',
        'Authorization' => 'Bearer 3e5cac39-7e38-4139-8fd6-30adc06a61bd',
    ],
    'body' => json_encode([
        "notifyUrl"     => "https://your.eshop.com/notify",
        "customerIp"    => "127.0.0.1",
        "merchantPosId" => "145227",
        "description"   => "RTV market",
        "currencyCode"  => "PLN",
        "totalAmount"   => "15000",
        "extOrderId"    => "erm742k6ycekibt6d3qr89",
        "buyer"         => [
            "email"     => "john.doe@example.com",
            "phone"     => "654111654",
            "firstName" => "John",
            "lastName"  => "Doe",
            "language"  => "en",
        ],
        "products"      => [
            [
                "name"      => "Wireless Mouse for Laptop",
                "unitPrice" => "15000",
                "quantity"  => "1",
            ],
        ],
    ]),
]);

But I get the response status 200 and with body as:

{
    "error": "invalid_token",
    "error_description": "Invalid access token",
}

Did the same call using cURL like this,

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://secure.payu.com/api/v2_1/orders');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true)
curl_setopt($ch, CURLOPT_POSTFIELDS, "{
   \"notifyUrl\": \"https://your.eshop.com/notify\",
   \"customerIp\": \"127.0.0.1\",
   \"merchantPosId\": \"145227\",
   \"description\": \"RTV market\",
   \"currencyCode\": \"PLN\",
   \"totalAmount\": \"15000\",
   \"extOrderId\":\"erm742k6ycekibt6d3qr99\",
   \"buyer\": {
     \"email\": \"john.doe@example.com\",
     \"phone\": \"654111654\",
     \"firstName\": \"John\",
     \"lastName\": \"Doe\",
     \"language\": \"en\"
            },
   \"products\": [
       {
           \"name\": \"Wireless Mouse for Laptop\",
           \"unitPrice\": \"15000\",
           \"quantity\": \"1\"
      }
   ]
}");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: Bearer 3e5cac39-7e38-4139-8fd6-30adc06a61bd';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);

And it gives the desired response with status code 200 :

{"orderId":"F2CJ8NSJ1Z190614GUEST000P01","extOrderId":"erm742k6ycekibt6d3qr99","status":{"statusCode":"SUCCESS"},"redirectUri":"https://secure.payu.com/pay/?orderId=F2CJ8NSJ1Z190614GUEST000P01&token=eyJhbGciOiJIUzI1NiJ9.eyJvcmRlcklkIjoiRjJDSjhOU0oxWjE5MDYxNEdVRVNUMDAwUDAxIiwicG9zSWQiOiJ6RGVubjhoTiIsImF1dGhvcml0aWVzIjpbIlJPTEVfQ0xJRU5UIl0sInBheWVyRW1haWwiOiJqb2huLmRvZUBleGFtcGxlLmNvbSIsImV4cCI6MTU2MDU3OTY0NCwiaXNzIjoiUEFZVSIsImF1ZCI6ImFwaS1nYXRld2F5Iiwic3ViIjoiUGF5VSBzdWJqZWN0IiwianRpIjoiNjc2NzQxMGQtNGJhNy00MmM4LWFjZmItMjRiMDUyNmYzY2VmIn0.s1cVhDYR4U5KN2-M-KWaG5dLjJqX8jbhTwI2NDg_3fo"}

I think the Authorization in the header is not getting passed in the guzzle request. How can I solve this to work with guzzle?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记