使用Stripe PHP SDK调用订阅查询API无返回结果求助
解决Stripe PHP SDK调用订阅查询无响应的问题
核心问题
你的代码仅调用了subscriptions->retrieve()方法,但没有输出或处理返回的订阅对象,所以请求返回200状态码(表示API调用成功),但没有任何内容返回给调用方。
修改后的代码示例
require_once __DIR__ . '/vendor/stripe/stripe-php/init.php'; $getVars = $_GET; if (!isset($getVars['id'])) { http_response_code(400); echo json_encode(['error' => '缺少订阅ID']); exit; } $subid = $getVars['id']; $apikey = "sk_test..."; $stripe = new \Stripe\StripeClient($apikey); try { // 获取订阅对象并赋值给变量 $subscription = $stripe->subscriptions->retrieve($subid, []); // 设置响应头为JSON格式,输出订阅数据 header('Content-Type: application/json'); echo json_encode($subscription); } catch (\Stripe\Exception\ApiErrorException $e) { // 捕获并输出Stripe API异常信息 http_response_code($e->getHttpStatus()); echo json_encode([ 'error' => $e->getMessage(), 'error_type' => $e->getError()->type ]); }
额外排查建议
- 验证订阅ID与密钥环境匹配:测试密钥(
sk_test_xxx)只能查询测试环境的订阅,生产密钥(sk_live_xxx)对应生产环境,不要跨环境查询。 - 查看Stripe事件日志:登录Stripe后台,进入「开发工具」→「事件日志」,找到对应请求的记录,可查看API返回的原始数据和请求详情。
- 检查PHP错误日志:如果依赖加载失败或有隐性报错,PHP错误日志会记录相关信息,可排查是否存在环境配置问题。
内容的提问来源于stack exchange,提问作者The Hawk
相关产品推荐
相关产品推荐

