Jump to content

[Solved] Prestashop API: Error 500 when adding product images


Recommended Posts

Hi! I'm trying to add/upload product images via Prestashop API, but I'm getting server error 500. What can be wrong with the code? Or maybe there's something wrong with the server configuration?

Prestashop version: 1.7.6.4

PHP script:

error_reporting(-1);
ini_set('display_errors', 'On');

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://[email protected]//api/images/products/24/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, 'MY_AUTH_KEY:');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' =>'@http://my-shop.com/img/my-shop-logo-1584646645.jpg;type=image/jpg'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);

print_r($curlinfo);

This results in [http_code] => 500. There's no error or anything. I have access to the hosting provider's server error log, but there's nothing in there...
I've enabled debug mode in the back office, but it doesn't seem to affect the API. Is there any other way to debug it?

The script is based on the Prestashop docs: https://devdocs.prestashop.com/1.7/development/webservice/tutorials/change_product_image/

Edited by absens00 (see edit history)
Link to comment
Share on other sites

I've also tried using CurlFile, but in this case I'm not getting any HTTP code at all...

error_reporting(-1);
ini_set('display_errors', 'On');

$image_path = 'http://my-shop.com/img/my-shop-logo-1584646645.jpg';
$image_mime = 'image/jpg';

$args['image'] = new CurlFile($image_path, $image_mime);

$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_URL, 'http://my-shop.com/api/images/products/24/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_USERPWD, 'MY_AUTH_KEY:');
curl_setopt($ch, CURLOPT_POSTFIELDS, $args);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
curl_close($ch);

print_r($curlinfo);

What I get is:

Array
(
    [url] => http://my-shop.com/api/images/products/24/
    [content_type] => 
    [http_code] => 0
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1

I can't figure out what's wrong

Link to comment
Share on other sites

I've found the answer!

You cannot upload a remote file with cURL. It has to be a local file. So, at first you need to download remote file and store it locally, and then upload the local file.

The second script works fine after changing image path to following:

$image_path = '../picture.jpg';

 

  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...