I’m trying to create a basic app which can pull image URLs for catalog items. I have the PHP SDK running and can successfully query the API for a catalog item, but the response I’m getting is (a) not json (I’d like to know how to get json data instead of all the objects returned), and (b) doesn’t seem to have imageData for items which definitely have images.
Any pointers on how to just grab image URLs for items and get the response as json?!
I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:
Additional Documentation
Catalog API
Upload and Attach Images
Search for Items and Objects
If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.
To retrieve image URLs for catalog items using the PHP SDK, you need to use the Catalog API’s RetrieveCatalogObject
endpoint. This endpoint allows you to retrieve a catalog object along with all of its associated information, including images.
However, the PHP SDK returns data as PHP objects, not JSON. If you want to convert the response to JSON, you can use PHP’s built-in json_encode
function. Here’s a basic example:
$api_response = $client->getCatalogApi()->retrieveCatalogObject($object_id, true);
$json_response = json_encode($api_response->getResult());
In this example, $object_id
is the ID of the catalog object you want to retrieve. The second parameter true
is for the include_related_objects
option, which tells the API to include all related objects in the response, including images.
The image URLs can be found in the image_data
field of the CatalogImage
objects associated with the catalog object. You can access them like this:
$catalog_object = $api_response->getResult()->getObject();
if ($catalog_object->getType() == 'IMAGE') {
$image_url = $catalog_object->getImageData()->getUrl();
}
Remember to replace $object_id
with the actual ID of the catalog object you want to retrieve. Also, ensure that the catalog object has images associated with it, otherwise the getImageData()
method will return null
.
This answer was reviewed by @Bryan-Square.