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
相关产品推荐
相关产品推荐

