Webmestre avivia Posted June 17, 2018 Share Posted June 17, 2018 I've figured out how to add products using the webservice, and now, I'd like to set images to these new products. I've tried various code, some even seem to work, but when I go in the catalog, these images don't seem to be set for the given products. In fact, I'm wondering if these examples, are for previous PS versions and this could explain why they fail. For example, images should be put in this folder PS_SHOP_PATH. 'api/images/products/'.$produitID; But when I go in my PS installation, I see that product images are rather in a /img/P folder... Does someone have a current script example I could use to set a product image from an URL, using webservice in PS 1.7.3.2 ? Link to comment Share on other sites More sharing options...
jgamio Posted June 17, 2018 Share Posted June 17, 2018 The old code works on the new version no changes in there For example, images should be put in this folder PS_SHOP_PATH. 'api/images/products/'.$produitID; No you are wrong the image no go there the place of the products images is om img/p If you see the images on img/p you did the job what is the problem better put your code here to check out Link to comment Share on other sites More sharing options...
Webmestre avivia Posted June 17, 2018 Author Share Posted June 17, 2018 Hi and thanx for the quick reply! One more thing that I must mention is that I was not entirely sure where to put my webservice related files, so instead of the site’s root, I put them into the webservice folder itself, into which I have added an images folder, I have put my PSWebServiceLibrary.php file there as well. Don’t hesitate to suggest a better practice if you have one, I still quite new to this. So here’s my code: <?php require_once('../config/config.inc.php'); // PS_SHOP_PATH etc. require_once('PSWebServiceLibrary.php'); define('DEBUG', true); define('PS_SHOP_PATH', 'http://dev.mydomain.ca/magasin/'); define('PS_WS_AUTH_KEY', 'MYAUTHKECODE'); $id_product = '682'; // external image URL from where you will download the image to local folder $remoteImageURL = "http://imagesdesproduits.aviviamobilier.com/imagesDesProduits/selecteur/v4/grandes/caissons_pieces/zonesArmoires84.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; $img_path = $_SERVER['DOCUMENT_ROOT'].'/magasin/webservice/images/'.$image_name; //echo($img_path); exit; //image will be associated with product id 4 $url = PS_SHOP_PATH. 'api/images/products/'.$id_product; // echo($url); exit; $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 for product ID '.$id_product; echo($url); } curl_close($ch); ?> Link to comment Share on other sites More sharing options...
Webmestre avivia Posted June 17, 2018 Author Share Posted June 17, 2018 Hi and thanx for the quick reply! One more thing that I must mention is that I was not entirely sure where to put my webservice related files, so instead of the site’s root, I put them into the webservice folder itself, into which I have added an images folder, I have put my PSWebServiceLibrary.php file there as well. Don’t hesitate to suggest a better practice if you have one, I still quite new to this. So here’s my code: <?php require_once('../config/config.inc.php'); // PS_SHOP_PATH etc. require_once('PSWebServiceLibrary.php'); define('DEBUG', true); define('PS_SHOP_PATH', 'http://dev.mydomain.ca/magasin/'); define('PS_WS_AUTH_KEY', 'MYAUTHKECODE'); $id_product = '682'; // external image URL from where you will download the image to local folder $remoteImageURL = "http://imagesdesproduits.aviviamobilier.com/imagesDesProduits/selecteur/v4/grandes/caissons_pieces/zonesArmoires84.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; $img_path = $_SERVER['DOCUMENT_ROOT'].'/magasin/webservice/images/'.$image_name; //echo($img_path); exit; //image will be associated with product id 4 $url = PS_SHOP_PATH. 'api/images/products/'.$id_product; // echo($url); exit; $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 for product ID '.$id_product; echo($url); } curl_close($ch); ?> Link to comment Share on other sites More sharing options...
Webmestre avivia Posted June 17, 2018 Author Share Posted June 17, 2018 Sorry for the double post. I'm not sure how to delete these. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now