You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP中使用前端console.log输出的authorization_token参数?

如何将前端Klarna授权返回的token传给PHP调用API?

场景说明

你有以下前端Klarna授权回调代码:

<script>
$(function(){
    $("button.authorize").on('click', function(){
        Klarna.Payments.authorize({
            payment_method_category: ''
             
             }, function(res) {
                console.log("Response from the authorize call:")
                console.log(res)
                            })
    })
  })
   
</script>

该代码执行后返回的结果为:

Object { show_form: true, approved: true, finalize_required: false, authorization_token: "mytoken" }

需要将返回结果中的authorization_token值,用于拼接Klarna的API URL,在PHP端完成接口调用:

https://api.playground.klarna.com/payments/v1/authorizations/{authorization_token}/order

实现步骤

1. 前端修改:获取token并发送到PHP后端

修改前端的回调函数,拿到返回结果后,通过AJAX将authorization_token发送到你的PHP接口:

<script>
$(function(){
    $("button.authorize").on('click', function(){
        Klarna.Payments.authorize({
            payment_method_category: ''
        }, function(res) {
            console.log("Response from the authorize call:")
            console.log(res)
            
            // 发送token到PHP后端
            if (res.authorization_token) {
                $.ajax({
                    url: 'your-php-handler.php', // 替换为你的PHP文件路径
                    type: 'POST',
                    data: {
                        authorization_token: res.authorization_token
                    },
                    success: function(phpResponse) {
                        console.log("PHP接口调用结果:", phpResponse);
                        // 可在此添加成功后的业务逻辑,如页面跳转
                    },
                    error: function(xhr, status, err) {
                        console.error("请求失败:", err);
                    }
                });
            }
        })
    })
})
</script>

2. PHP后端:接收token并调用Klarna API

创建对应的PHP文件(比如your-php-handler.php),接收前端传来的token,拼接API URL后发起请求:

<?php
// 接收前端传入的token
$authToken = $_POST['authorization_token'] ?? '';

if (empty($authToken)) {
    echo json_encode(['status' => 'error', 'msg' => '缺少authorization_token参数']);
    exit;
}

// 拼接完整的Klarna API地址
$apiUrl = "https://api.playground.klarna.com/payments/v1/authorizations/{$authToken}/order";

// 替换为你的Klarna API认证信息
$apiUsername = 'your-klarna-api-username';
$apiPassword = 'your-klarna-api-password';

// 初始化CURL请求
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $apiUrl);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "{$apiUsername}:{$apiPassword}");
curl_setopt($curl, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Accept: application/json'
]);

// 执行请求并获取响应
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);

// 返回处理结果给前端
if ($httpCode === 200) {
    echo json_encode([
        'status' => 'success',
        'data' => json_decode($response, true)
    ]);
} else {
    echo json_encode([
        'status' => 'error',
        'http_code' => $httpCode,
        'error_msg' => json_decode($response, true)
    ]);
}
?>

内容的提问来源于stack exchange,提问作者Ria

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 08:10:40