skip to Main Content

I am trying to connect to Shopify Rest Admin API using PHP and I am using Shopify API’s PHP library and I initialize successfully like this:

ShopifyContext::initialize(
    'key',
    'secret',
    'scope_1, scope_2',
    'https://example.com',
    new FileSessionStorage('/tmp/php_sessions'),
    '2022-10',
);

But when I try to make a request like this:

$this->test_session = Utils::loadCurrentSession(
    $requestHeaders,
    $requestCookies,
    $isOnline
);
Webhookv2::webhooks(
    $this->test_session, // Session
    [], // Url Ids
    [], // Params
);

I get the following error:

Fatal error: Uncaught TypeError: Argument 1 passed to ShopifyUtils::loadCurrentSession() must be of the type array, null given, called in /www/doc/www.example.com/www/script/index.php on line 45 and defined in /www/doc/www.example.com/www/script/vendor/shopify/shopify-api/src/Utils.php:158 Stack trace: #0 /www/doc/www.example.com/www/script/index.php(45): ShopifyUtils::loadCurrentSession(NULL, NULL, NULL) #1 {main} thrown in /www/doc/www.example.com/www/script/vendor/shopify/shopify-api/src/Utils.php on line 158

What am I missing?

P.S.
The code in the Utils.php file (part of Shopify PHP library) that corresponds to the three session variables above:

/**
 * Loads the current user's session based on the given headers and cookies.
 *
 * @param array $rawHeaders The headers from the HTTP request
 * @param array $cookies    The cookies from the HTTP request
 * @param bool  $isOnline   Whether to load online or offline sessions
 *
 * @return Session|null The session or null if the session can't be found
 * @throws ShopifyExceptionCookieNotFoundException
 * @throws ShopifyExceptionMissingArgumentException
 */
public static function loadCurrentSession(array $rawHeaders, array $cookies, bool $isOnline): ?Session
{
    $sessionId = OAuth::getCurrentSessionId($rawHeaders, $cookies, $isOnline);

    return Context::$SESSION_STORAGE->loadSession($sessionId);
}

2

Answers


  1. TheĀ loadCurrentSession method requires $requestHeaders to be an array (docs).
    Make sure the $requestHeaders variable you pass as argument is indeed an array. Something like:

    $requestHeaders = array( 
                         'api_version' => '2023-01', 
                         'X-Shopify-Access-Token' => 'ACCESS_TOKEN'
                      );
    $requestCookies = array();
    $isOnline = true;
    
    $this->test_session = Utils::loadCurrentSession(
        $requestHeaders,
        $requestCookies,
        $isOnline
    );
    
    Login or Signup to reply.
  2. I am also stuck in above question. Please some one help!

    Please tell how to set the values to the sub,exp,nbf,iat elements in below code.

        $payload = [
            'iss' => 'https://XXXXXXXXX.myshopify.com/admin',
            'dest' => 'https://XXXXXXXX.myshopify.com',
            'aud' => YOUR APP API KEY,
            'sub' => HOW SET THIS VALUE IN PHP,//<user ID
            'exp' => HOW SET THIS VALUE IN PHP,//time in seconds
            'nbf' => HOW SET THIS VALUE IN PHP,//time in seconds
            'iat' => HOW SET THIS VALUE IN PHP,//time in seconds
            'jti' => Uuid::uuid1(),
            'sid' => HOW SET THIS VALUE IN PHP
        ];
        $jwt = JWT::encode($payload, 'YOUR APP SECRET KEY', 'HS256');
    
        $headers['authorization'] = "Bearer " . $jwt;
        $headers['api_version'] = config('shopify.apiVersion');
        $currentSession = Utils::loadCurrentSession($headers, $_COOKIE, true);
    
        echo $currentSession->getAccessToken();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search