slince / shopify-api-php

:rocket: Shopify API Client for PHP
MIT License
128 stars 48 forks source link

Check if it works #54

Closed zerdmer closed 4 years ago

zerdmer commented 4 years ago

There is some functions to know if works the connection with the token?

like: CheckConnection($credential);

maximzasorin commented 4 years ago

Hello, no such method provided. I think that you can check the validity of the token with the request to shop resource and response analysis, for example, like this:

use Slince\Shopify\Client;
use Slince\Shopify\Exception\ClientException;

function isTokenValid(Client $client): bool {
    try {
        $client->getShopManager()->get();
    } catch (ClientException $exception) {
        // Unauthorized request
        if ($exception->getResponse() && $exception->getResponse()->getStatusCode() == 401) {
            return false;
        }
        throw $exception;
    }
    return true;
}

You can check not only the 401 status but also some other status depending on your goals. Here is a list of codes that can be returned by the API: https://help.shopify.com/en/api/getting-started/response-status-codes

slince commented 4 years ago

I don't think it's necessary to provide a method. If you need it, you can just wrap it like above.