PHP SDK usage - json response

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?!

:wave: 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.

I’m picking this back up again - still no joy here.

I have a method to make a request to the API like this:

public function getSquareProps( $client, $square_token ) {

    try {

      $apiResponse = $client->getCatalogApi()->retrieveCatalogObject( $square_token, true, null, true );

      if ( $apiResponse->isSuccess() ) {
        return $apiResponse->getResult();
      }
      else {
        $errors = $apiResponse->getErrors();
        log_message( 'error', json_encode( $errors ) );
        return false;
      }

    }
    catch ( ApiException $e ) {
      echo "ApiException occurred: <b/>";
      echo $e->getMessage() . "<p/>";
      return false;
    }

  }

This seems to be working, but, I’m not getting any image URLs in the responses, even when the catalogue item has images in Square.

Example response:

{
    "object": {
        "type": "ITEM_VARIATION",
        "id": "OC3RKNHZRVUT327ZQD5T27XK",
        "updated_at": "2024-11-05T10:34:31.285Z",
        "version": 1730802871285,
        "is_deleted": false,
        "present_at_all_locations": true,
        "item_variation_data": {
            "item_id": "RBLOQQDBTU3YDB525OKQKFCM",
            "name": "",
            "sku": "8260626",
            "upc": "9783740814762",
            "ordinal": 1,
            "pricing_type": "FIXED_PRICING",
            "price_money": {
                "amount": 1399,
                "currency": "GBP"
            },
            "location_overrides": [
                {
                    "location_id": "LC045GCPXTEQY",
                    "track_inventory": true
                }
            ],
            "track_inventory": true,
            "inventory_alert_type": "NONE",
            "sellable": true,
            "stockable": true
        }
    },
    "related_objects": [
        {
            "type": "ITEM",
            "id": "RBLOQQDBTU3YDB525OKQKFCM",
            "updated_at": "2024-11-05T10:34:31.285Z",
            "version": 1730802871285,
            "is_deleted": false,
            "custom_attribute_values": {
                "sq0idp-w46nJ_NCNDMSOywaCY0mwA:ecom_gifting_enabled": {
                    "name": "Ecom Gifting Enabled",
                    "custom_attribute_definition_id": "SUVNEPWVWH523WZMJKNAXCHB",
                    "type": "BOOLEAN",
                    "boolean_value": true,
                    "key": "sq0idp-w46nJ_NCNDMSOywaCY0mwA:ecom_gifting_enabled"
                }
            },
            "present_at_all_locations": true,
            "item_data": {
                "name": "111 Places in Edinburgh",
                "description": "This book takes the discerning visitor to hidden corners and secret sights in this city of contrasts, exploring fascinating locations unknown even to most residents, and revealing unexpected aspects of some familiar local landmarks. Marvel at a unique",
                "is_taxable": true,
                "available_online": false,
                "available_for_pickup": false,
                "available_electronically": false,
                "variations": [
                    {
                        "type": "ITEM_VARIATION",
                        "id": "OC3RKNHZRVUT327ZQD5T27XK",
                        "updated_at": "2024-11-05T10:34:31.285Z",
                        "version": 1730802871285,
                        "is_deleted": false,
                        "present_at_all_locations": true,
                        "item_variation_data": {
                            "item_id": "RBLOQQDBTU3YDB525OKQKFCM",
                            "name": "",
                            "sku": "8260626",
                            "upc": "9783740814762",
                            "ordinal": 1,
                            "pricing_type": "FIXED_PRICING",
                            "price_money": {
                                "amount": 1399,
                                "currency": "GBP"
                            },
                            "location_overrides": [
                                {
                                    "location_id": "LC045GCPXTEQY",
                                    "track_inventory": true
                                }
                            ],
                            "track_inventory": true,
                            "inventory_alert_type": "NONE",
                            "sellable": true,
                            "stockable": true
                        }
                    }
                ],
                "product_type": "REGULAR",
                "skip_modifier_screen": true,
                "image_ids": [
                    "77KD5QJNYUTHKSKYILYHCAWZ",
                    "5MFLTBJZEIF4PMBJSQ3DRUGT",
                    "7HJFHNSJ32PUECPGGWMFOU4T",
                    "2WYGJEXYFZSVDURFWOH5Y25C"
                ],
                "categories": [
                    {
                        "id": "M23PYY6GTR73PGF7V25WMLKN",
                        "ordinal": 2954937499648
                    },
                    {
                        "id": "2F6THZBNTAWBGV5RXB7QA5M4",
                        "ordinal": 120316
                    },
                    {
                        "id": "HLSK5ZVI5YWZOCXX6J456QDV",
                        "ordinal": 2810162
                    }
                ],
                "description_html": "<p>This book takes the discerning visitor to hidden corners and secret sights in this city of contrasts, exploring fascinating locations unknown even to most residents, and revealing unexpected aspects of some familiar local landmarks. Marvel at a unique <\/p>",
                "description_plaintext": "This book takes the discerning visitor to hidden corners and secret sights in this city of contrasts, exploring fascinating locations unknown even to most residents, and revealing unexpected aspects of some familiar local landmarks. Marvel at a unique",
                "channels": [
                    "CH_wYG80B8Mc4XMb3VHdzF4iPGPzVWtApTTwwvfYRlQuYC",
                    "CH_DbRkdm8P1PgFUo8YHzF4iPGPzVWtApTTwwvfYRlQuYC"
                ],
                "is_archived": false,
                "ecom_seo_data": {
                    "page_title": "111 Places in Edinburgh Book | Gift Shop Edinburgh",
                    "page_description": "The 111 places in Edinburgh book is a guide to some of the best places to see in Edinburgh.",
                    "permalink": "111-places-in-edinburgh-travel-guide-book"
                },
                "reporting_category": {
                    "id": "M23PYY6GTR73PGF7V25WMLKN",
                    "ordinal": 2954937499648
                }
            }
        }
    ]
}

Compare the above to the same request being made using the API explorer:

{
  "object": {
    "type": "ITEM_VARIATION",
    "id": "OC3RKNHZRVUT327ZQD5T27XK",
    "updated_at": "2024-11-05T10:34:31.285Z",
    "created_at": "2022-03-23T18:30:27.298Z",
    "version": 1730802871285,
    "is_deleted": false,
    "present_at_all_locations": true,
    "item_variation_data": {
      "item_id": "RBLOQQDBTU3YDB525OKQKFCM",
      "name": "",
      "sku": "8260626",
      "upc": "9783740814762",
      "ordinal": 1,
      "pricing_type": "FIXED_PRICING",
      "price_money": {
        "amount": 1399,
        "currency": "GBP"
      },
      "location_overrides": [
        {
          "location_id": "LC045GCPXTEQY",
          "track_inventory": true
        }
      ],
      "track_inventory": true,
      "inventory_alert_type": "NONE",
      "sellable": true,
      "stockable": true,
      "default_unit_cost": {
        "amount": 839,
        "currency": "GBP"
      },
      "channels": [
        "CH_DbRkdm8P1PgFUo8YHzF4iPGPzVWtApTTwwvfYRlQuYC",
        "CH_wYG80B8Mc4XMb3VHdzF4iPGPzVWtApTTwwvfYRlQuYC"
      ],
      "item_variation_vendor_info_ids": [
        "E25LRO2ZSLFCTOIPJH26XLMU"
      ],
      "item_variation_vendor_infos": [
        {
          "type": "ITEM_VARIATION_VENDOR_INFO",
          "id": "E25LRO2ZSLFCTOIPJH26XLMU",
          "updated_at": "2024-11-05T10:34:31.285Z",
          "created_at": "2022-03-23T18:32:00.71Z",
          "version": 1730802871285,
          "is_deleted": false,
          "present_at_all_locations": true,
          "item_variation_vendor_info_data": {
            "ordinal": 0,
            "price_money": {
              "amount": 839,
              "currency": "GBP"
            },
            "item_variation_id": "OC3RKNHZRVUT327ZQD5T27XK",
            "vendor_id": "DFOH472UUZENRPZW"
          }
        }
      ]
    }
  },
  "related_objects": [
    {
      "type": "ITEM",
      "id": "RBLOQQDBTU3YDB525OKQKFCM",
      "updated_at": "2024-11-05T10:34:31.285Z",
      "created_at": "2022-03-23T18:30:27.298Z",
      "version": 1730802871285,
      "is_deleted": false,
      "custom_attribute_values": {
        "sq0idp-w46nJ_NCNDMSOywaCY0mwA:ecom_gifting_enabled": {
          "name": "Ecom Gifting Enabled",
          "custom_attribute_definition_id": "SUVNEPWVWH523WZMJKNAXCHB",
          "type": "BOOLEAN",
          "boolean_value": true,
          "key": "sq0idp-w46nJ_NCNDMSOywaCY0mwA:ecom_gifting_enabled"
        }
      },
      "present_at_all_locations": true,
      "item_data": {
        "name": "111 Places in Edinburgh",
        "description": "This book takes the discerning visitor to hidden corners and secret sights in this city of contrasts, exploring fascinating locations unknown even to most residents, and revealing unexpected aspects of some familiar local landmarks. Marvel at a unique",
        "is_taxable": true,
        "visibility": "PRIVATE",
        "available_online": false,
        "available_for_pickup": false,
        "available_electronically": false,
        "variations": [
          {
            "type": "ITEM_VARIATION",
            "id": "OC3RKNHZRVUT327ZQD5T27XK",
            "updated_at": "2024-11-05T10:34:31.285Z",
            "created_at": "2022-03-23T18:30:27.298Z",
            "version": 1730802871285,
            "is_deleted": false,
            "present_at_all_locations": true,
            "item_variation_data": {
              "item_id": "RBLOQQDBTU3YDB525OKQKFCM",
              "name": "",
              "sku": "8260626",
              "upc": "9783740814762",
              "ordinal": 1,
              "pricing_type": "FIXED_PRICING",
              "price_money": {
                "amount": 1399,
                "currency": "GBP"
              },
              "location_overrides": [
                {
                  "location_id": "LC045GCPXTEQY",
                  "track_inventory": true
                }
              ],
              "track_inventory": true,
              "inventory_alert_type": "NONE",
              "sellable": true,
              "stockable": true,
              "default_unit_cost": {
                "amount": 839,
                "currency": "GBP"
              },
              "channels": [
                "CH_DbRkdm8P1PgFUo8YHzF4iPGPzVWtApTTwwvfYRlQuYC",
                "CH_wYG80B8Mc4XMb3VHdzF4iPGPzVWtApTTwwvfYRlQuYC"
              ],
              "item_variation_vendor_info_ids": [
                "E25LRO2ZSLFCTOIPJH26XLMU"
              ],
              "item_variation_vendor_infos": [
                {
                  "type": "ITEM_VARIATION_VENDOR_INFO",
                  "id": "E25LRO2ZSLFCTOIPJH26XLMU",
                  "updated_at": "2024-11-05T10:34:31.285Z",
                  "created_at": "2022-03-23T18:32:00.71Z",
                  "version": 1730802871285,
                  "is_deleted": false,
                  "present_at_all_locations": true,
                  "item_variation_vendor_info_data": {
                    "ordinal": 0,
                    "price_money": {
                      "amount": 839,
                      "currency": "GBP"
                    },
                    "item_variation_id": "OC3RKNHZRVUT327ZQD5T27XK",
                    "vendor_id": "DFOH472UUZENRPZW"
                  }
                }
              ]
            }
          }
        ],
        "product_type": "REGULAR",
        "skip_modifier_screen": true,
        "ecom_uri": "https://www.example.com/product/111-places-in-edinburgh-travel-guide-book/21",
        "ecom_image_uris": [
          "https://www.example.com/uploads/1/4/1/2/141295149/s482407285242066437_p21_i6_w1279.jpeg",
          "https://www.example.com/uploads/1/4/1/2/141295149/s482407285242066437_p21_i7_w1594.jpeg",
          "https://www.example.com/uploads/1/4/1/2/141295149/s482407285242066437_p21_i8_w1594.jpeg",
          "https://www.example.com/uploads/1/4/1/2/141295149/s482407285242066437_p21_i9_w1594.jpeg"
        ],
        "ecom_available": true,
        "ecom_visibility": "VISIBLE",
        "image_ids": [
          "77KD5QJNYUTHKSKYILYHCAWZ",
          "5MFLTBJZEIF4PMBJSQ3DRUGT",
          "7HJFHNSJ32PUECPGGWMFOU4T",
          "2WYGJEXYFZSVDURFWOH5Y25C"
        ],
        "categories": [
          {
            "id": "M23PYY6GTR73PGF7V25WMLKN",
            "ordinal": 2954937499648
          },
          {
            "id": "2F6THZBNTAWBGV5RXB7QA5M4",
            "ordinal": 120316
          },
          {
            "id": "HLSK5ZVI5YWZOCXX6J456QDV",
            "ordinal": 2810162
          }
        ],
        "pickup_fulfillment_preferences_id": "fprefs_127cj2koz85ya0z0vzjkm2fhd",
        "delivery_fulfillment_preferences_id": "fprefs_127k2et792iachyxqqbglolo5",
        "description_html": "<p>This book takes the discerning visitor to hidden corners and secret sights in this city of contrasts, exploring fascinating locations unknown even to most residents, and revealing unexpected aspects of some familiar local landmarks. Marvel at a unique </p>",
        "description_plaintext": "This book takes the discerning visitor to hidden corners and secret sights in this city of contrasts, exploring fascinating locations unknown even to most residents, and revealing unexpected aspects of some familiar local landmarks. Marvel at a unique",
        "channels": [
          "CH_wYG80B8Mc4XMb3VHdzF4iPGPzVWtApTTwwvfYRlQuYC",
          "CH_DbRkdm8P1PgFUo8YHzF4iPGPzVWtApTTwwvfYRlQuYC"
        ],
        "shipping_fulfillment_preferences_id": "fprefs_127kfdiun899e3m2e23i3u98j",
        "dine_in_fulfillment_preferences_id": "fprefs_127tnhil7mj6bm56mbrnsowxg",
        "is_archived": false,
        "ecom_seo_data": {
          "page_title": "111 Places in Edinburgh Book | Gift Shop Edinburgh",
          "page_description": "The 111 places in Edinburgh book is a guide to some of the best places to see in Edinburgh.",
          "permalink": "111-places-in-edinburgh-travel-guide-book"
        },
        "reporting_category": {
          "id": "M23PYY6GTR73PGF7V25WMLKN",
          "ordinal": 2954937499648
        }
      }
    }
  ]
}

Why does the explorer return the ecom_image_uris but the API doesn’t?

The ecom_image_uris aren’t a documented and supported field which is why they aren’t included in the SDK. We don’t recommend coding anything to the ecom_image_uris. If you’d like the image of a catalog object you’ll use the image_id to get the images. :slightly_smiling_face: