Image Upload

Michael CareyMichael Carey Posts: 61Administrator, Lightspeed Staff moderator
edited September 2017 in Tutorials
Images are uploaded using a multipart form encoded POST. The first part contains the metadata as a typical API XML or JSON structure. The second part contains the image data.

Image Upload - cURL Example

curl -v -X POST -H "Authorization: Bearer {OAuth Token}" \
-H "Accept: application/xml" \
-H "Content-Type: multipart/form-data" \
--form-string 'data=<?xml version="1.0" encoding="UTF-8"?>test.jpgtest' \
-F "image=@/full/path/to/image/and/filename.jpg" \
https://api.merchantos.com/API/Account/{account_id}/Item/{item_id}/Image



Image Upload - PHP Example

<?php

/* Be sure to update the following values with those specific to your account */
$account_id = 'your_account_id'; // Your account ID
$item_id = 'item_id'; // Item ID of item you wish to update
$oauth_token = 'valid_oauth_token'; // OAuth access token

$url = "https://api.merchantos.com/API/Account/".$account_id."/Item/".$item_id."/Image";

$headers = array(
'Authorization: Bearer '.$oauth_token,
'Accept: application/xml',
'Content-Type: multipart/form-data');

$imagefile = '/path/to/image/file.jpg'; //Full path of image file you wish to use

$postfields = array(
'data' => '<?xml version="1.0" encoding="UTF-8"?>test.jpgtest',
'image' => new CURLFile($imagefile, 'text/plain', 'test.jpg'));

$filesize = filesize($imagefile);

$ch = curl_init();

$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
CURLOPT_POST => 1,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_INFILESIZE => $filesize,
CURLOPT_RETURNTRANSFER => true
);

curl_setopt_array($ch, $options);
$output = curl_exec($ch);

curl_close($ch);
?>

Post edited by Michael Carey on
Michael Carey

Product Manager
Lightspeed HQ

2 comments

Sign In or Register to comment.