admin管理员组

文章数量:1291024

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this 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 Automattic\WooCommerce\Client;

$woocommerce = new Client(
    'site_url_example',
    'ck_00000000000000000',
    'cs_11111111111111111',
    [ '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' => '.jpg' ] ]
];
$result = $woocommerce->post('products', $data);

print_r($result);

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

'images' => [ [ 'src' => 'C:\Users\579\Desktop\2.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?

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 3 years ago.

Improve this 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 Automattic\WooCommerce\Client;

$woocommerce = new Client(
    'site_url_example',
    'ck_00000000000000000',
    'cs_11111111111111111',
    [ '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);

print_r($result);

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

'images' => [ [ 'src' => 'C:\Users\579\Desktop\2.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?

Share Improve this question edited Jun 12, 2021 at 15:12 tiago calado asked Jun 12, 2021 at 15:05 tiago caladotiago calado 6324 silver badges17 bronze badges 2
  • 1 Questions regarding third-party products are currently off-topic within our Stack. Your question would be best addressed in WooCommerce's support channels. – bosco Commented Jun 12, 2021 at 16:12
  • WooCommerce and other 3rd party plugin/theme dev support is off topic and not in this stacks scope. You should ask via their official support routes or in their groups and communities, this is not a place to ask for WooCommerce help – Tom J Nowell Commented Jun 12, 2021 at 19:04
Add a comment  | 

1 Answer 1

Reset to default 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:\Users\579\Desktop\2.1.jpg') ]

What you need to add to your file:

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

// Your WordPress username
DEFINE('WORDPRESS_LOGIN', 'admin');

// Create a new Application Password in dashboard /wp-admin/profile.php
DEFINE('WORDPRESS_APPLICATION_PASSWORD', 'TBIg TthU rJG3 moMe Qjor Vtl2');



/**
 * 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];
}

本文标签: wp apiwhy woocommerce api only do not upload images from local computer