Could not upload image using etsy PHP api. Some one please help me !!

1,122 views
Skip to first unread message

Jyotiprakash Biswal

unread,
Aug 8, 2015, 12:32:37 PM8/8/15
to Etsy API
Hello,
I am using the following code to upload an image to an existing listing in PHP. The image is present in the same directory where the code is.

<?php
ini_set('display_errors',true);
define('OAUTH_CONSUMER_KEY', '<MY-KEY>');
define('OAUTH_CONSUMER_SECRET', '<MY-SECRET>');
$listing_id="242953861";
$listing_image_id = "";
$filename="abc.jpg";
$access_token="<ACCESS-TOKEN-HERE>";
$access_token_secret="<TOKEN-SECRET-HERE>";
$url = "https://openapi.etsy.com/v2/listings/".$listing_id."/images";

$oauth = new OAuth(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET);
$oauth->setVersion("1.1");
$oauth->enableDebug();
$oauth->setToken($access_token, $access_token_secret);
try {
$source_file = dirname(realpath(__FILE__)) ."/$filename";
$mimetype=mime_content_type($source_file);
$filesize=filesize($source_file);
$params = array('@image' => '@'.$source_file.';type='.$mimetype); 
$header["Content-Type"] = 'multipart/form-data';
$header["Content-Length"] = $filesize;
$data=$oauth->fetch($url, $params, OAUTH_HTTP_METHOD_POST,$header);
$json = $oauth->getLastResponse();
print_r(json_decode($json, true));

} catch (OAuthException $e) {

echo "<pre>";
print $oauth->getLastResponse()."\n";
print_r($oauth->debugInfo);
die($e->getMessage());
}


The Code is same as mentioned in Etsy Developer Documentation and the URL is here :


But I could not able to upload the image, its showing errors as follows. I have spent so many hours searching in the web but could not find any suitable article.
Please some one help me finding the issue.


OUTPUT

Image file or image_id string must be included
Array
(
    [sbs] => POST&https%3A%2F%2Fopenapi.etsy.com%2Fv2%2Flistings%2F242953861%2Fimages........
    [headers_sent] => Content-Type: multipart/form-data
    [headers_recv] => HTTP/1.1 400 Bad Request
Server: Apache
Set-Cookie: uaid=uaid%3DTBxjj5JIeT8AiD5HbLU-oSYKGyUq%26_now%3D1439043021%26_slt%3DABLJqdpZ%26_kid%3D1%26_ver%3D1%26_mac%3Dp9Piw9YybzhknMXmDFh683M0E93IiFcm23-FIUUEXj4.; expires=Wed, 07-Sep-2016 06:28:41 GMT; Max-Age=34186700; path=/; domain=.etsy.com; httponly
X-Etsy-Request-Uuid: _sDEiCigwVAujsOWBCgt09p53rvH
X-RateLimit-Limit: 10000
X-RateLimit-Remaining: 9873
X-Error-Detail: Image file or image_id string must be included
Cache-Control: private
X-Cnection: close
Content-Type: text/plain;charset=UTF-8
Content-Length: 46
Accept-Ranges: bytes
Date: Sat, 08 Aug 2015 14:10:21 GMT
Via: 1.1 varnish
Connection: close
X-Served-By: cache-fra1239-FRA
X-Cache: MISS
X-Cache-Hits: 0
X-Timer: S1439043021.363271,VS0,VE138
    [body_recv] => Image file or image_id string must be included
)
Invalid auth/bad request (got a 400, expected HTTP/1.1 20X or a redirect)

Jyotiprakash Biswal

unread,
Aug 9, 2015, 9:08:18 AM8/9/15
to Etsy API
Here are some more clarifications to my issues:
1) Here I am trying to upload a image to an existing listing which does not have any images so no listing image id present.
2) If i remove @ from '@image' in the below line , i get another error " The request body is too large - Invalid auth/bad request (got a 400, expected HTTP/1.1 20X or a redirect) "

    $params = array('@image' => '@'.$source_file.';type='.$mimetype);

Jyotiprakash Biswal

unread,
Aug 10, 2015, 9:26:15 AM8/10/15
to Etsy API
Hello, I have figured out the issue and solved it.
Please do not bother about this Post now.

Thanks
Jyotiprakash

Perry Rylance

unread,
Aug 20, 2015, 8:35:17 AM8/20/15
to Etsy API
I'm having the same problem. How did you solve it?

Rob

unread,
Feb 3, 2016, 4:32:52 PM2/3/16
to Etsy API
How did you solve the "The request body is too large" error?

Kibet Hosea

unread,
Feb 16, 2016, 4:09:54 PM2/16/16
to Etsy API
How did you solve the error?

Jessy Loran

unread,
Apr 13, 2017, 1:50:06 PM4/13/17
to Etsy API
I'm having the same problem. How did you solve it?

On Saturday, August 8, 2015 at 10:02:37 PM UTC+5:30, Jyotiprakash Biswal wrote:

ljubomir.pet...@gmail.com

unread,
Aug 13, 2017, 3:29:06 PM8/13/17
to Etsy API
Can you tell us how to fix that problem, thanks?

Ben Scarola

unread,
Feb 26, 2019, 5:37:35 PM2/26/19
to Etsy API
Hopefully this will help someone who stumbles on this thread in the future... I just went through hours of pulling my hair out trying to get a simple image upload to work using the example here: https://www.etsy.com/developers/documentation/getting_started/images. I tried many variations based on other threads and received a seemingly random mix of error messages, related to content-length header, image_id missing, etc.
In the end, all of these errors and the work-arounds mentioned in other threads seemed to be red herrings. The real issue is that you must have curl/libcurl configured correctly and available as an OAuth "request engine". If you don't see it listed in the OAuth section of your PHPInfo page, then it's not going to work. So here's what I did to fix it:

1. Symlink your system's curl headers folder to the default location that pecl/oauth is looking for them ( /usr/local/include/curl )
2. Uninstall and re-install Oauth

Watch the PECL install log.. there's a line where it says something like 'Looking for curl in default location'. If you don't see an affirmative 'found' message there, then it's not going to add the request engine.

NOTE: You may also get a complaint about missing ca-bundle. For my cloud VM I had to symlink the system's ca-bundle.crt to the location that curl was looking for it.

I've repeated these basic steps on an aws-linux box, and my osx development machine, on PHP 5.6 and 7.2. I did not need to add any additional code to the example provided by Etsy ( e.g. setting headers or request engine explicitly ). Everything just works as long as curl is configured correctly.

Raj Seecharran

unread,
Jul 23, 2019, 12:15:57 PM7/23/19
to Etsy API
Hi
I am having the same problem, can you let me know how did you do this?


On Saturday, August 8, 2015 at 10:02:37 PM UTC+5:30, Jyotiprakash Biswal wrote:

Kai Ritchie

unread,
Jul 23, 2019, 12:16:06 PM7/23/19
to Etsy API
Hey Ben, are you using Etsy's code as provided in the docs? If yours is a bit different, do you think you could share it?

I have curl set up with oauth just fine, but I can't get the image post to work either...

Other posts work fine though... Thanks in advance!

Ben Scarola

unread,
Jul 23, 2019, 3:35:49 PM7/23/19
to Etsy API
Sure... here is a simplified snippet from my working code that uploads an image to a listing, overwriting the existing image at that position ( rank ). Substitute your credential vars for: EtsyKey, EtsySecret, EtsyOAuthToken, and EtsyOAuthTokenSecret.

  $imageParams = array('overwrite' => 1, 'rank' => $index + 1,'@image' => '@'. $tmpPath .';type=image/jpeg' );

  $this->jsonRequest( self::ETSY_BASE_URI . self::ETSY_LISTINGS . "/" . $parentListingId . "/images", $imageParams );

   function jsonRequest( $url, $postData ) {
        $oauth = new OAuth( {EtsyKey}, {EtsySecret}, OAUTH_SIG_METHOD_HMACSHA1, OAUTH_AUTH_TYPE_URI );
        $oauth->setToken( {EtsyOAuthToken}, {EtsyOAuthTokenSecret} );
        $oauth->enableDebug();

        $method = OAUTH_HTTP_METHOD_POST;

        try {
            $headers=[];
            $data = $oauth->fetch( $url, $postData, $method, $headers );

            $json = $oauth->getLastResponse();

            $response = json_decode($json );
            echo $oauth->getLastResponseInfo();

            echo 'RESPONSE' . "\n\n" . print_r( $response, true );
            return $response;

        } catch (OAuthException $e) {
            echo $e->getMessage();
            echo print_r($oauth->getLastResponse(), true);
            echo print_r($oauth->getLastResponseInfo(), true);
            exit;
        }
    }

Raj Seecharran

unread,
Jul 24, 2019, 10:55:47 AM7/24/19
to Etsy API
 Hi Ben 
I am also getting the problem in push images in already pushed products.
below is my code.

I am getting this error  ==>  The request body is too large
Invalid auth/bad request (got a 400, expected HTTP/1.1 20X or a redirect) .

Can you please let me know where I am doing wrong. Because my push products and pull products is working fine.

Thanks In Advance. 
 
$access_token = 'xxxxxxxxxxxx';
$access_token_secret = 'yyyy';
$api_key = 'wwwww';
$secret = 'eeeee';
$oauth = new Oauth($api_key, $secret);
$oauth->disableSSLChecks();
$oauth->setToken($access_token, $access_token_secret);
$oauth = new Oauth($api_key, $secret);
$oauth->setToken($access_token, $access_token_secret);
/*define(OAUTH_REQENGINE_STREAMS);
define(OAUTH_REQENGINE_CURL);
*/
$oauth->setRequestEngine(1);
echo "I am here";
#echo $listing_id = $result['results'][0]['listing_id'];
$listing_id = 706699568;
$filename = "earrings.jpeg";
 $mimetype = "jpeg";
echo "<br>";
echo $url = "https://openapi.etsy.com/v2/listings/".$listing_id."/images";

    try {
    $source_file = dirname(realpath(__FILE__)) ."/$filename";
    echo $mimetype=mime_content_type($source_file);
    $filesize=filesize($source_file);
    
    $params = array('image'=>"@{$source_file};type=".$mimetype);

    //$params = array('@image' => '@'.$source_file.';type='.$mimetype); 

    $header["Content-Type"] = 'multipart/form-data';    
    $header["Content-Length"] = $filesize;          

    $data=$oauth->fetch($url, $params, OAUTH_HTTP_METHOD_POST,$header);
    $json = $oauth->getLastResponse();
    print_r(json_decode($json, true));
     echo "I am in try";
   } catch (OAuthException $e) {
     echo "<pre>";
    print $oauth->getLastResponse()."\n";
    print_r($oauth->debugInfo);
    die($e->getMessage());
    echo "I am in catch";
}

Ben Scarola

unread,
Jul 31, 2019, 4:41:41 PM7/31/19
to Etsy API
Hi Raj,

Have you tried plugging your credentials, listing id, and file path into the sample code that I posted? If you get the same error with my code then it may be that your file is indeed too large, or there is still a problem with your libcurl configuration.

Raj Seecharran

unread,
Aug 12, 2019, 3:09:59 PM8/12/19
to Etsy API
Hi Ben 
I followed the way to install the OAUTH Process and installed it properly And It works for me. Now I can push the image.
But Ben new  problem is Begin, When I am sending the multiple images it only take 2 Images First and last image. But when I am printing the request respnse it shows me the all images pushed successfully and I get there url too. When I am going to my etsy account and check that product there is only two images.
Can you please help me why is this happening?
Thanks In advance.  

Raj Seecharran

unread,
Aug 12, 2019, 3:10:06 PM8/12/19
to Etsy API
Hi Ben 
Greetings!!!

Now My Image is pushing fine. But Here is new problem.
When I am pushing more than 2 Images in loop it only show two images in my products first and last image from the loop but when I am printing the response it showing the all images etsy URL. Even I conatct with support about that And I also send them my code and file url with response. And they are unable to find the problem. Please help me how I can fix this?
  


On Thursday, August 1, 2019 at 2:11:41 AM UTC+5:30, Ben Scarola wrote:

Ngan Pham

unread,
Aug 16, 2019, 6:50:46 PM8/16/19
to etsy-...@googlegroups.com
You must set rank = incremental number starting with 1-2-3... 

--
You received this message because you are subscribed to the Google Groups "Etsy API" group.
To unsubscribe from this group and stop receiving emails from it, send an email to etsy-api-v2...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/etsy-api-v2/39657164-b254-4de6-94cf-5d4bb89867d5%40googlegroups.com.


--

Ngan Pham (Nigel)

Raj Seecharran

unread,
Aug 21, 2019, 12:02:44 PM8/21/19
to Etsy API
Thanks Dear.

Now It is working fine. 
To unsubscribe from this group and stop receiving emails from it, send an email to etsy-...@googlegroups.com.


--

Ngan Pham (Nigel)

Karen Galstyan

unread,
Jan 31, 2020, 5:06:23 PM1/31/20
to Etsy API
Hi dear.
I have the same problem with images. I have tried to send data with Postman, it is working, but when I am trying to put the Postman generated code into my codes, it doesn't work. I am getting "Invalid auth/bad request (got a 400, expected HTTP/1.1 20X or a redirect" or "The request body is too large".
Can you please send an example of code, or just tell what exactlly you did to make it work?
Thanks in advance.



среда, 24 июля 2019 г., 18:55:47 UTC+4 пользователь Raj Seecharran написал:

Jitesh Patel

unread,
Jun 28, 2020, 10:10:28 AM6/28/20
to Etsy API
On a ubuntu machine follow the below steps to ensure that your installation is correct for PECL with curl as request engine and post that ETSY code should work like a breeze for you to upload images:

# Uninstall oauth if already installed ('sudo pecl uninstall oauth')
sudo apt install libcurl4-gnutls-dev
cd /usr/include && sudo ln -s x86_64-linux-gnu/curl
sudo pecl install oauth
 
Reply all
Reply to author
Forward
0 new messages