我想访问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;
}
答案 0 :(得分:0)
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;
}