Giter Club home page Giter Club logo

php-shopify-api's Introduction

About

A simple PHP wrapper around the Shopify API.

Installation

Install via Composer by running composer require luketowers/php-shopify-api in your project directory.

Usage

In order to use this wrapper library you will need to provide credentials to access Shopify's API.

You will either need an access token for the shop you are trying to access (if using a public application) or an API Key and Secret for a private application.

Examples

Make an API call

use LukeTowers\ShopifyPHP\Shopify;

// Initialize the client
$api = new Shopify('exampleshop.myshopify.com', 'mysupersecrettoken');

// Get all products
$result = $api->call('GET', 'admin/products.json');

// Get the products with ids of '632910392' and '921728736' with only the 'id', 'images', and 'title' fields
$result = $api->call('GET', 'admin/products.json', [
    'ids' => '632910392,921728736',
    'fields' => 'id,images,title',
]);

// Create a new "Burton Custom Freestyle 151" product
$result = $api->call('POST', 'admin/products.json', [
    'product' => [
        "title"        => "Burton Custom Freestyle 151",
        "body_html"    => "<strong>Good snowboard!</strong>",
        "vendor"       => "Burton",
        "product_type" => "Snowboard",
        "tags"         => 'Barnes & Noble, John's Fav, "Big Air"',
    ],
]);

Use Private Application API Credentials to authenticate API requests

use LukeTowers\ShopifyPHP\Shopify;

$api = new Shopify($data['shop'], [
    'api_key' => '...',
    'secret'  => '...',
]);

Use an access token to authenticate API requests

use LukeTowers\ShopifyPHP\Shopify;

$storedToken = ''; // Retrieve the stored token for the shop in question
$api = new Shopify('exampleshop.myshopify.com', $storedToken);

Request an access_token for a shop

use LukeTowers\ShopifyPHP\Shopify;

function make_authorization_attempt($shop, $scopes)
{
    $api = new Shopify($shop, [
        'api_key' => '...',
        'secret'  => '...',
    ]);

    $nonce = bin2hex(random_bytes(10));

    // Store a record of the shop attempting to authenticate and the nonce provided
    $storedAttempts = file_get_contents('authattempts.json');
    $storedAttempts = $storedAttempts ? json_decode($storedAttempts) : [];
    $storedAttempts[] = ['shop' => $shop, 'nonce' => $nonce, 'scopes' => $scopes];
    file_put_contents('authattempts.json', json_encode($storedAttempts));

    return $api->getAuthorizeUrl($scopes, 'https://example.com/handle/shopify/callback', $nonce);
}

header('Location: ' . make_authorization_attempt('exampleshop.myshopify.com', ['read_product']));
die();

Handle Shopify's response to the authorization request

use LukeTowers\ShopifyPHP\Shopify;

function check_authorization_attempt()
{
    $data = $_GET;

    $api = new Shopify($data['shop'], [
        'api_key' => '...',
        'secret'  => '...',
    ]);

    $storedAttempt = null;
    $attempts = json_decode(file_get_contents('authattempts.json'));
    foreach ($attempts as $attempt) {
        if ($attempt->shop === $data['shop']) {
            $storedAttempt = $attempt;
            break;
        }
    }

    return $api->authorizeApplication($storedAttempt->nonce, $data);
}

$response = check_authorization_attempt();
if ($response) {
    // Store the access token for later use
    $response->access_token;
}

php-shopify-api's People

Contributors

luketowers avatar tpenaranda avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.