Jump to content

Add product image via webservice


Recommended Posts

I set up a webservice key that has image and products full permissions. But when pass a POST request to add a image to a product, for example /api/images/products/1, the server response is code 66 message Unable to save this image, no further info.

 

Checking the back access we have the following:

GET api/

<images xlink:href="http://192.168.56.101/tienda/api/images" get="true" put="true" post="true" delete="true" head="true">

GET api/images/

<products xlink:href="http://192.168.56.101/tienda/api/images/products" get="true" put="false" post="false" delete="false" head="true" upload_allowed_mimetypes="image/gif, image/jpg, image/jpeg, image/pjpeg, image/png, image/x-png"/>

 

For that reason I think is a permission issue but I can't find any documentation that help me. My question is, what permission is need it to have PUT and POST methods over the /images/products resource?

Link to comment
Share on other sites

  • 1 month later...
  • 1 year later...
  • 4 months later...
<?php

define('PS_SHOP_PATH', 'http://localhost/prestashop');
define('PS_WS_AUTH_KEY', 'your-key-here');

// external image URL from where you will download the image to local folder
$remoteImageURL = "http://---yoururlforimage---.jpg";


// save the image to local folder
$dir_path_to_save = 'images/'; 


class GetImage {

/*
-------------------------------------------------------------------------
GetImage class Credits: Bit Repository
URL: http://www.bitrepository.com/web-programming/php/download-image.html
-------------------------------------------------------------------------
*/

var $source;
var $save_to;
var $set_extension;
var $quality;

function download($method = 'curl') // default method: cURL
{
$info = @GetImageSize($this->source);
$mime = $info['mime'];

if(!$mime) exit('Could not obtain mime-type information. Make sure that the remote file is actually a valid image.');

// What sort of image?
$type = substr(strrchr($mime, '/'), 1);

switch ($type) 
{
case 'jpeg':
    $image_create_func = 'ImageCreateFromJPEG';
    $image_save_func = 'ImageJPEG';
	$new_image_ext = 'jpg';
	
	// Best Quality: 100
	$quality = isSet($this->quality) ? $this->quality : 100; 
    break;

case 'png':
    $image_create_func = 'ImageCreateFromPNG';
    $image_save_func = 'ImagePNG';
	$new_image_ext = 'png';
	
	// Compression Level: from 0  (no compression) to 9
	$quality = isSet($this->quality) ? $this->quality : 0;
    break;

case 'bmp':
    $image_create_func = 'ImageCreateFromBMP';
    $image_save_func = 'ImageBMP';
	$new_image_ext = 'bmp';
    break;

case 'gif':
    $image_create_func = 'ImageCreateFromGIF';
    $image_save_func = 'ImageGIF';
	$new_image_ext = 'gif';
    break;

case 'vnd.wap.wbmp':
    $image_create_func = 'ImageCreateFromWBMP';
    $image_save_func = 'ImageWBMP';
	$new_image_ext = 'bmp';
    break;

case 'xbm':
    $image_create_func = 'ImageCreateFromXBM';
    $image_save_func = 'ImageXBM';
	$new_image_ext = 'xbm';
    break;

default: 
	$image_create_func = 'ImageCreateFromJPEG';
    $image_save_func = 'ImageJPEG';
	$new_image_ext = 'jpg';
}

if(isSet($this->set_extension))
{
$ext = strrchr($this->source, ".");
$strlen = strlen($ext);
$new_name = basename(substr($this->source, 0, -$strlen)).'.'.$new_image_ext;
}
else
{
$new_name = basename($this->source);
}

$save_to = $this->save_to.$new_name;

    if($method == 'curl')
	{
    $save_image = $this->LoadImageCURL($save_to);
	}
	elseif($method == 'gd')
	{
	$img = $image_create_func($this->source);

	    if(isSet($quality))
	    {
		   $save_image = $image_save_func($img, $save_to, $quality);
		}
		else
		{
		   $save_image = $image_save_func($img, $save_to);
		}
	}
	
	return $save_image;
}

function LoadImageCURL($save_to)
{
$ch = curl_init($this->source);
$fp = fopen($save_to, "wb");

// set URL and other appropriate options
$options = array(CURLOPT_FILE => $fp,
                 CURLOPT_HEADER => 0,
                 CURLOPT_FOLLOWLOCATION => 1,
	             CURLOPT_TIMEOUT => 60); // 1 minute timeout (should be enough)

curl_setopt_array($ch, $options);

$save = curl_exec($ch);
curl_close($ch);
fclose($fp);

return $save;
}
}






// initialize the class
$image = new GetImage;



$image->source = $remoteImageURL;

$image->save_to = $dir_path_to_save; // with trailing slash at the end

$get = $image->download('curl'); // using GD

if($get)
{
echo "The image has been saved.";
}

$image_name = basename($remoteImageURL);


// change the local path where image has been downloaded "presta-api" is my local folder from where i run API script
$img_path = '\wamp\www\presta-api\images\\'. $image_name;


//image will be associated with product id 4
$url = PS_SHOP_PATH. '/api/images/products/4';
 
$ch = curl_init();


curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_PUT, true); To edit a picture

curl_setopt($ch, CURLOPT_USERPWD, PS_WS_AUTH_KEY.':');
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image'=>"@".$img_path.";type=image/jpeg"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);


if(curl_exec($ch) === false)
{ 
echo "<br><br>Error : ".curl_error($ch)."<br>"; }
else { echo '<br><br> Image added'; }
curl_close($ch);


?>

above script will download an image from an URL and add to a prestashop product through webservices API

Link to comment
Share on other sites

  • 4 weeks later...

Hi,

i'am ok on POST an image but if i want to PUT a new image for the product nothing seem to work, can you explain how to update an image for the product ?

above code is for uploading products to product id 4

 

see this   $url = PS_SHOP_PATH. '/api/images/products/4';

Link to comment
Share on other sites

  • 4 weeks later...

In php>5.5 you have to use curl_file_create instead of this:

curl_setopt($ch, CURLOPT_POSTFIELDS, array('image'=>"@".$img_path.";type=image/jpeg"));

I've tried  this:

$cfile = curl_file_create($img_url,'image/jpg','image');

curl_setopt($ch, CURLOPT_POSTFIELDS, $cfile);

but the response return me the error "Unable to save this image".

Do you have an idea of how could I solve the problem?

Link to comment
Share on other sites

  • 11 months later...
×
×
  • Create New...