Image Upload Code Sample - PHP with Guzzle

Image Upload Code Sample - PHP with Guzzle

The code below is using the Guzzle library to simplify handling of the HTTP request.

<?php

require 'vendor/autoload.php';
use GuzzleHttp\Client;

$domainPrefix = getenv('VEND_DOMAIN_PREFIX');
$token = getenv('VEND_TOKEN');

// Those values wouldn't usually come from environment variables
// this is just used to avoid hardocing them in
$filePath = getenv('VEND_IMAGE_FILEPATH');
$productID = getenv('VEND_PRODUCT_ID');

$client = new \GuzzleHttp\Client();

$url = 'https://' . $domainPrefix . '.retail.lightspeed.app/api/2.0/products/' . $productID . '/actions/image_upload';

$response = $client->request('POST', $url, [
    'multipart' => [
        [
            'name'     => 'image',
            'contents' => fopen($filePath, 'r'),
        ]
    ],
    'headers'  => [
        'Authorization' => 'Bearer ' . $token
    ]
]);

echo $response->getBody();