Catalog API list migration to the new API

I have converted my legacy PHP list call from $apiResponse = $client->getCatalogApi()->listCatalog(types: 'item,category,image') to $apiResponse = $client->catalog->list(new ListCatalogRequest(['types' => 'item,category,image',])) however I don’t know how to convert the json_encoding part as I haven’t been able to find a guide. How should I migrate the following part?

if ($apiResponse->isSuccess()) {

    http_response_code(200);

    echo json_encode($apiResponse->getResult());

} else {

    http_response_code(getErrorCode($apiResponse->getErrors()));

    echo getErrorMessage($apiResponse->getErrors());

}

:waving_hand: 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
Manage Menus
Call the SearchCatalogItems Endpoint

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 migrate the JSON encoding part of your PHP code when using the new client structure, you can follow a similar approach to handle the API response. Here’s how you can adapt your code:

  1. Check if the response is successful using the isSuccess() method.
  2. If successful, set the HTTP response code to 200 and use json_encode to output the result.
  3. If not successful, set the HTTP response code based on the error and output the error message.

Here’s how you can implement it:

if ($apiResponse->isSuccess()) {
    http_response_code(200);
    echo json_encode($apiResponse->getResult());
} else {
    http_response_code(getErrorCode($apiResponse->getErrors()));
    echo getErrorMessage($apiResponse->getErrors());
}

This structure remains largely the same as your original code, ensuring that you handle both successful and error responses appropriately. The key is to continue using json_encode for the successful result and to handle errors by setting the appropriate HTTP response code and displaying the error message.

This answer was reviewed by @Bryan-Square.

@Square-Dev-AI @Bryan-Square But why am I getting an undefined method isSuccess error? It looks like the list function is returning a Pager object that doesn’t have the isSuccess method or am I missing something?

What is the exact error your getting? :slight_smile:

Uncaught Error: Call to undefined method Square\Core\Pagination\CursorPager::isSuccess() @Bryan-Square