Skip to content

Instantly share code, notes, and snippets.

@umbrae
Created September 6, 2012 16:57
Show Gist options
  • Select an option

  • Save umbrae/3658512 to your computer and use it in GitHub Desktop.

Select an option

Save umbrae/3658512 to your computer and use it in GitHub Desktop.
Sample image upload for Etsy Listings
<?php
define('OAUTH_CONSUMER_KEY', 'YOUR_CONSUMER_KEY_HERE');
define('OAUTH_CONSUMER_SECRET', 'YOUR_CONSUMER_SECRET_HERE');
$listing_id="ID_OF_LISTING_THAT_YOU_OWN_HERE";
$filename="FILENAME_THAT_EXISTS_ON_THE_FILESYSTEM_HERE.JPG";
$mimetype="image/jpeg";
$access_token="ACCESS_TOKEN_YOU_HAVE_AUTHENTICATED_WITH_HERE";
$access_token_secret="ACCESS_SECRET_THAT_YOU_HAVE_AUTHENTICATED_WITH_HERE";
// You must define the constants OAUTH_CONSUMER_KEY and OAUTH_CONSUMER_SECRET
// You must also assign values to the variables $access_token, $access_token_secret,
// $listing_id and $filename, and $mimetype.
// Your image file is assumed to be in the same directory as this code.
$oauth = new OAuth(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET);
$oauth->enableDebug();
$oauth->setToken($access_token, $access_token_secret);
try {
$source_file = dirname(realpath(__FILE__)) ."/$filename";
$url = "http://openapi.etsy.com/v2/listings/".$listing_id."/images";
$params = array('@image' => '@'.$source_file.';type='.$mimetype);
$oauth->fetch($url, $params, OAUTH_HTTP_METHOD_POST);
$json = $oauth->getLastResponse();
print_r(json_decode($json, true));
} catch (OAuthException $e) {
// You may want to recover gracefully here...
print $oauth->getLastResponse()."\n";
print_r($oauth->debugInfo);
die($e->getMessage());
}
@vaigtech
Copy link

Image file or image_id string must be included

@webkulabhi
Copy link

when send content of image file get following response
[body_sent] => %40image=84090%3Btype%3Dimage%2Fpng
[body_recv] => The request body is too large

@hughgrigg
Copy link

This is the example from Etsy's documentation, which doesn't actually work. You can get it working using oauth and curl: https://notestoself.dev/posts/etsy-api-php-oauth-listing-image-upload/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment