why woocommerce api only do not upload images from local computer?

The question:

I want to upload images on my computer to a wordpress website, however the woocommerce api is only working well with everything except images on my computer.
Here is the code I am using when it works just fine, creates the product and uploads the image:

require __DIR__ . '/vendor/autoload.php';
use AutomatticWooCommerceClient;

$woocommerce = new Client(
    [ 'version' => 'wc/v3', ]

$data = [
    'name' => 'Premium Quality',
    'type' => 'simple',
    'regular_price' => '21.99',
    'description' => 'Pellentesque hec eu libero sit amet quamleo.',
    'short_description' => 'Pellentesque habitant morbi tristiqeq.',
    'categories' => [ [ 'id' => 35 ] ],
    'images' => [ [ 'src' => 'https://www.gettyimages.pt/gi-resources/images/500px/983794168.jpg' ] ]
$result = $woocommerce->post('products', $data);


then I change the image path from online somewhere to a path of my computer like so:

'images' => [ [ 'src' => 'C:Users579Desktop2.1.jpg' ] ]

But doesn’t create the product or uploads the image throwing this error: Invalid URL Provided. [woocommerce_product_image_upload_error]

My question is how to upload images from local pc to wordpress website?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Because you are just passing the location of the image, not the image itself. So you are basically telling WordPress where your image is located, on your local computer. The server will not be able to access files on your local computer.

Solve it by first uploading the image to WordPress, and then pass in the image ID in your WooCommmerce import script.

Use the script below, to be able to do the following:

'images' => [ file_or_url_to_wordpress_image('C:Users579Desktop2.1.jpg') ]

What you need to add to your file:

DEFINE('WORDPRESS_BASE_URL', 'https://remote-wordpress-site.com');

// Your WordPress username

// Create a new Application Password in dashboard /wp-admin/profile.php

 * Takes a file or a url and uploads it to WordPress
function file_or_url_to_wordpress_image($image_path){

    // If the input is a URl, we can process it
    if (filter_var($image_path, FILTER_VALIDATE_URL)) {
        return ['src'=>$image_path];

     // Make sure the image exist
    if (!file_exists($image_path)){return;}

    // Load the image
    $file = file_get_contents( $image_path );

    // Get the filename
    $filename = basename($image_path);

    // Initiate curl.
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt( $ch, CURLOPT_URL, WORDPRESS_BASE_URL .'/wp-json/wp/v2/media/' );
    curl_setopt( $ch, CURLOPT_POST, 1 );
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $file );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, [
        "Content-Disposition: form-data; filename="$filename"",
        'Authorization: Basic ' . base64_encode( WORDPRESS_LOGIN. ':' . WORDPRESS_APPLICATION_PASSWORD ),
    ] );
    $result = curl_exec( $ch );
    curl_close( $ch );

    // Decode the response
    $api_response = json_decode($result);

    // Return the ID of the image that is now uploaded to the WordPress site.
    return ['id' => $api_response];

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment