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

Play Billing API数据获取问题:无法获取lineItems->expiryTime字段

解决Play Billing API无法获取lineItems->expiryTime的问题

你遇到的问题核心是lineItems是数组类型的对象集合,不是单个对象,直接用$purchase->lineItems->expiryTime访问自然拿不到值。

正确处理方式

lineItems里可能包含多个订阅条目(比如用户购买多期订阅),需要先取出数组里的元素,再访问expiryTime属性。

修改后的代码示例:

require_once('autoload.php');
//purchases_products
$client = new Google_Client();
$client->setAuthConfig( 'credentials.json');
$client->addScope('https://www.googleapis.com/auth/androidpublisher');

$service = new Google_Service_AndroidPublisher($client);
$purchase = $service->purchases_subscriptionsv2->get(PACKAGE_NAME, $token);

// 正常输出startTime
echo $purchase->startTime;

// 处理lineItems
$lineItems = $purchase->getLineItems();
if (!empty($lineItems)) {
    // 取第一个订阅条目
    $firstItem = $lineItems[0];
    echo $firstItem->expiryTime;
    
    // 如果需要遍历所有订阅条目
    foreach ($lineItems as $item) {
        echo $item->expiryTime;
    }
}

关键说明

  • 用getLineItems()方法获取lineItems数组(符合Google PHP客户端的规范,比直接访问属性更稳妥)
  • 先判断数组非空,避免空指针报错
  • 每个lineItem是独立对象,需针对单个对象访问expiryTime属性

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 05:36:07