Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.0.0+

Shopify Private Authentication for Private Apps

See more Shopify Examples

Shopify private authentication is for interacting with your own store through private applications. It uses HTTP "Basic" authentication with your Shopify private application key and secret key.

This example demonstrates how to send a private authenticated request using Chilkat Http, and then the same using Chilkat Rest.

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Http;
use Chilkat2\HttpResponse;
use Chilkat2\JsonObject;
use Chilkat2\Rest;
use Chilkat2\StringBuilder;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

// First demonstrate sending a simple request using Shopify private authentication w/ the Chilkat Http API.
$http = new Http();

// To use HTTP Basic Authentication with any HTTP request, we simply set the Login, Password, and BasicAuth properties.
// Important: All HTTP requests using Basic authentication must be over SSL/TLS.
$http->Login = 'SHOPIFY_PRIVATE_API_KEY';
$http->Password = 'SHOPIFY_PRIVATE_API_SECRET_KEY';
$http->BasicAuth = true;

// Make sure to replace "chilkat" with your store name.
$resp = new HttpResponse();
try {
    $http->HttpNoBody('GET', 'https://chilkat.myshopify.com/admin/products.json', $resp);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

// Examine the response code.
if ($resp->StatusCode != 200) {
    echo 'Received error response code: ', $resp->StatusCode, "\n";
    echo "Response body:\n";
    echo $resp->BodyStr, "\n";
    exit;
}

// Success.
echo "Success.\n";

// Examine the JSON response
$sbJson = new StringBuilder();
$resp->GetBodySb($sbJson);
$json = new JsonObject();
$json->LoadSb($sbJson);
$json->EmitCompact = false;
echo $json->Emit(), "\n";

// -------------------------------------------------
// Now let's do the same using the Chilkat Rest API.
$rest = new Rest();

// Provide the private app credentials:
$rest->SetAuthBasic('SHOPIFY_PRIVATE_API_KEY', 'SHOPIFY_PRIVATE_API_SECRET_KEY');

// Connect to the shopify server.
$bTls = true;
$port = 443;
$bAutoReconnect = true;
// Make sure to replace "chilkat" with your store name.
try {
    $rest->Connect('chilkat.myshopify.com', $port, $bTls, $bAutoReconnect);

    $sbJson->Clear();
    $rest->FullRequestNoBodySb('GET', '/admin/products.json', $sbJson);
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

if ($rest->ResponseStatusCode != 200) {
    echo 'Received error response code: ', $rest->ResponseStatusCode, "\n";
    echo "Response body:\n";
    echo $sbJson->GetAsString(), "\n";
    exit;
}

// Success...
$json->LoadSb($sbJson);
echo $json->Emit(), "\n";