2
votes

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.

2
The extrenal images will be get using wp_remote_get(). Do you have cURL installed? Or something blocking extrenal requests? - Claudio Sanches
"curl is enabled". Cool, but is configured correctly? Have you tested external requests? - Claudio Sanches
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. Have you tried it with an http link rather thank https? See this stackoverflow.com/questions/26461966/… - Kirby
I'm pretty certain that image['position'] should be an integer too, though that doesn't seem to be throwing the error. - Benjineer

2 Answers

2
votes

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
2
votes

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,
);