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

Patreon PHP SDK如何查询档位spots_count并更新剩余可售名额

Patreon档位名额查询及更新实现方案

查询档位spots_count数值

Patreon API默认不会返回档位的名额相关字段,你需要在请求时主动指定需要返回的资源和字段,替换你原有代码中fetch_campaign_details调用部分即可:

// 设置接口返回数组格式,方便解析
$api_client->api_return_format = 'array';
$campaign_id = '替换为你的实际活动ID';
// 发起请求,指定关联档位资源、以及需要返回的档位字段
$campaign_response = $api_client->get(
    "/campaigns/{$campaign_id}",
    [
        'include' => 'tiers',
        'fields[tier]' => 'title,max_pledge_campaign_spots,remaining_pledge_campaign_spots'
    ]
);

返回结果中included数组下的tier类型数据就是所有档位信息:

  • max_pledge_campaign_spots:档位总可售名额(对应你要的spots_count总数值)
  • remaining_pledge_campaign_spots:档位当前剩余可售名额

遍历判断剩余名额的示例代码:

foreach ($campaign_response['included'] as $resource) {
    if ($resource['type'] !== 'tier') {
        continue;
    }
    $tier_id = $resource['id'];
    $remaining = $resource['attributes']['remaining_pledge_campaign_spots'] ?? 0;
    $total_spots = $resource['attributes']['max_pledge_campaign_spots'] ?? 0;
    // 剩余名额小于4时增加可售名额,这里示例加10个,可自行调整
    if ($remaining < 4) {
        $new_total = $total_spots + 10;
        updateTierTotalSpots($api_client, $tier_id, $new_total);
    }
}

更新档位可售名额

官方PHP SDK没有封装档位更新的方法,你可以直接调用SDK底层的patch方法发起请求,符合Patreon API的JSON:API规范即可:

function updateTierTotalSpots($api_client, string $tier_id, int $new_total_spots) {
    $request_body = [
        'data' => [
            'id' => $tier_id,
            'type' => 'tier',
            'attributes' => [
                'max_pledge_campaign_spots' => $new_total_spots
            ]
        ]
    ];
    return $api_client->patch("/tiers/{$tier_id}", $request_body);
}

注意事项

  • 你的AccessToken必须拥有campaigns.tiers:read和campaigns.tiers:write权限,申请OAuth授权时需要主动配置这两个scope
  • 不限量的档位max_pledge_campaign_spots字段会返回null,不会触发名额更新逻辑
  • 不要在生产环境硬编码client_id、client_secret等敏感信息,建议通过环境变量读取

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 21:54:05