I am developing a module for Prestashop that is responsible for synchronizing elements between stores. I have managed to create a product from the store that has installed the module in the store that has the Webservices activated, the problem comes to me when trying to upload an image to said product.
The Prestashop with the module is 1.7.2.4 + PHP 7.0.27 The Prestashop with activated webservices is 1.6.1.17 + PHP 5.4.45
I leave you the piece of code with which I try to do this process.
$images = $prod->getImages();
foreach($images as $img => $i) {
$image = new Image($i['id_image']);
$key = $this->wsCon->api_key;
$shop_path = $this->wsCon->url.'/api/images/products/29';
$img = _PS_BASE_URL_._THEME_PROD_DIR_.$image->getExistingImgPath().".jpg";
$fields = ['image' => new \CurlFile($img, 'image/jpg', 'test.jpg')];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $shop_path);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_USERPWD, $key.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
$result = curl_exec($ch);
if (curl_exec($ch) == false) {
echo "Error actualizando imagen en Presashop";
echo curl_error($ch) . " ";
} else {
echo "Imagen subida correctamente en nuestra tienda.";}
}
I explain briefly, I have a Product object ($ prod), from which I get the images in the store with the module installed, then for each of them, I create an object Image with its id, I set it in the $ shop_path the url of the webservice of the product to which I want to add the image and in $ img the complete url of the image on the Prestashop server with the module installed. Then I start the upload process with curl.
This operation always returns me as an error 'could not open file + the url of the image'.