How to add product image via the woocommerce REST API?

11,019

Solution 1

I had a similar issue and the error that cURL was generating inside WordPress which leads to woocommerce_api_invalid_remote_product_image was {"errors":{"http_request_failed":["SSLRead() return error -9806"]},"error_data":[]} which means, according to Asaph in https://stackoverflow.com/a/26538127/266531,

php is compiled with a version of cURL that uses Apple's Secure Transport under Yosemite and the target of the URL request doesn't support SSLv3 (which was probably disabled due to the POODLE vulnerability).

My guess is that you are experiencing errors with using SSL over cURL.

Have you tried it with an http link rather thank https?

If you can debug the server side, take a look at what's happening inside class-wc-api-products.php around line 1700. That's what's generating the error. You may be experiencing SSL errors.

If it is the same type of SSL issue, then your possible solutions are

  1. Use a non-secure image link (http instead of https), or
  2. Follow Asaph's steps to install PHP using OpenSSL instead of SecureSSL in his answer at https://stackoverflow.com/a/26538127/266531

Solution 2

In Woocommerce REST API liblary You can also set option to dont validate SSL.

$options = array(
    'debug'           => true,
    'return_as_array' => false,
    'validate_url'    => false,
    'timeout'         => 60,
    'ssl_verify'      => false,
);
Share:
11,019
Haroldas
Author by

Haroldas

php programmer, front-end developer

Updated on June 17, 2022

Comments

  • Haroldas
    Haroldas almost 2 years

    I am working with the Woocommerce REST API and need to add a product to the store.

    It worked before. Now I have this error:

    stdClass Object ( [errors] => Array ( 
      [0] => stdClass Object ( [code] => 
      woocommerce_api_invalid_remote_product_image 
      [message] => Error getting remote image 
      https://www.google.lt/images/srpr/logo11w.png ) ) )
    

    Here is the documentation for adding a product via the WooCommerce REST API http://woothemes.github.io/woocommerce-rest-api-docs/#create-a-product

    Here is my code:

    $dataArray = array(
                'title' => 'xxxxxxxxxx',
                'description' => 'description1',
                'price' => '69',
                'sku' => 'sku2',
                'tags' => 'tag1, tag2, tag3',
                'color' => array('red', 'blue'),
                'size' => array('S', 'M'),
                'image' => 'https://www.google.lt/images/srpr/logo11w.png'
                );
    
    public function addProduct($data)
    {
        $wc_api = $this->_getClient();
    
    
        $newProductData = array(
            'product' => array(
                'title' => $data['title'],
                'type' => 'variable',
                'regular_price' => $data['price'],
                'description' => $data['description'],
                'sku' => $data['sku'],
                'tags' => [ $data['tags'] ],
                'images' => [ array('src' => $data['image'], 'position' => '0') ],
                'virtual' => true
            )
        );
    
        return $wc_api->create_product($newProductData);
    }
    

    I'm using this client to call the REST API

    https://github.com/kloon/WooCommerce-REST-API-Client-Library

    EDITED: If I get image from wordpress where woocommerce is hosted then all is fine. But, if I use a link from another site then I get an error.