php - 如何从方形目录 API 返回对象中提取特定字段?
问题描述
我想从 Square Catalog API 使用其 PHP SDK 返回的返回对象中访问特定字段。我正在使用 listCatalog 选项,它返回显示所有项目和所有相应值的整个对象。但是,我需要做些什么来进一步分解它以提取某些值,例如 name 和 id?
我尝试了不同的方法来深入研究对象并提取以下字段:
$id = $result->getObjects()->getId()
但是,这会使程序崩溃。任何关于我做错了什么的想法都将不胜感激。
$api_instance = new SquareConnect\Api\CatalogApi();
$cursor = ""; // string | The pagination cursor returned in the previous response. Leave unset for an initial request. See [Pagination]. (/basics/api101/pagination) for more information.
$types = "Item,Item_Variation,Category"; // string | An optional case-insensitive, comma-separated list of object types to retrieve, for example `ITEM,ITEM_VARIATION,CATEGORY,IMAGE`. The legal values are taken from the [CatalogObjectType](#type-catalogobjecttype) enumeration, namely `ITEM`, `ITEM_VARIATION`, `CATEGORY`, `DISCOUNT`, `TAX`, `MODIFIER`, `MODIFIER_LIST`, or `IMAGE`.
try {
$result = $api_instance->listCatalog($cursor, $types);
$id = $result->getObjects();
print_r ($id);
} catch (Exception $e) {
echo 'Exception when calling CatalogApi->listCatalog: ', $e- >getMessage(), PHP_EOL;
}
解决方案
该listCatalog()
函数返回一个数组CatalogObject
,因此您需要遍历所有返回的对象,如下所示:
try {
$result = $api_instance->listCatalog($cursor, $types);
foreach ($result->getObjects() as $catalogObject) {
// Here are all the getter functions you can use to access the data: https://github.com/square/connect-php-sdk/blob/master/lib/Model/CatalogObject.php#L101
print_r($catalogObject->getId());
print_r($catalogObject);
}
} catch (Exception $e) {
echo 'Exception when calling CatalogApi->listCatalog: ', $e->getMessage(), PHP_EOL;
}
推荐阅读
- apache-kafka - 跳窗是什么意思?
- ios - 如何在没有 xcode 的 react-native ios 中禁用应用程序旋转?
- java - python中带有中断的线程睡眠(来自java的端口)
- dc.js - 如何在带有时间 x 轴的 dc BarChart 上使列宽(不是铅笔薄)
- .net-core - ADB2C - 浏览器关闭后如何在 .NET Core webapp 中保持登录状态
- openmdao - check_totals 在 OpenMDAO 中写入一个大向量
- python - 在 Pandas Dataframe 中比较列之间的值会生成“无法使用多维键索引”错误
- java - 如果没有关键字 infront,该方法如何工作?
- python - 为颜色数据实现萤火虫算法
- javascript - 有没有办法在不打开 Outlook 或 Apple Mail 的情况下使用 CSS(或任何与 Web 开发相关的语言)中的“mailto”立即发送电子邮件?