Jump to content

Product Image (Not Found) Via Web Service


Recommended Posts

Hi,

 

I have a problem, when I upload a product with Web Service its created no problem, but its image is not found. Below i post the code of upload de images and some photos.

 

        $url = 'localhost/tienda/api/images/products/id_del _producto';

        $file_name = realpath('./m.jpg');  //     Trial image

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true);

 
        curl_setopt($ch, CURLOPT_USERPWD,'KEY_DE_WEBSERVICE'.":");

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

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

 

30b2yw9.jpg

 

ieimat.jpg

 

Please I need help, its very important for me.

 

Greetings

Link to comment
Share on other sites

Hi,

 

Of course M.Gigliotti, I post the complete code

 

<?php
require_once 'PSWebServiceLibrary.php';


define('PS_SHOP_PATH', 'localhost/tienda');    



//Descomenta y cambia producto por categorie y anade categoria

try{
    $webService = new PrestaShopWebService( PS_SHOP_PATH ,'KEY_DE_WEBSERVICE',false);
    $xml = $webService -> get(array('url' => PS_SHOP_PATH.'/api/products?schema=blank'));//blank
    $resources = $xml -> children() -> children();

    
    if ($_POST['enviar']){        //'enviar is a button'
        echo "Has pulsado enviar";
        //Values of form HTML
        $nombre = $_POST['nombre'];
        $precio = $_POST['precio'];
        $stock = $_POST['stock'];
        $short_desc = $_POST['short_desc'];

        
        $id_created_product = $resources->id;
        echo $resources->id;
        $resources->active = 1;
        $resources->id_category_default = 3;
        $resources->name->language[0] = $nombre;
        $resources->link_rewrite->language[0] = 'mi_producto';
        $resources->description->language[0][0] = $short_desc;
        $resources->price = floatval($precio);
        
          $resources->id_manufacturer;
      $resources->id_supplier;
      $resources->new;
      $resources->cache_default_attribute;
      $resources->id_default_image = 1;
      $resources->id_default_combination;
      $resources->id_tax_rules_group;
      $resources->position_in_category;
      $resources->type = "virtual";
      $resources->id_shop_default;
      $resources->reference = "ref_product";
      $resources->supplier_reference;
      $resources->location;
      $resources->width;
      $resources->height;
      $resources->depth;
      $resources->weight;
      $resources->quantity_discount;
      $resources->ean13;
      $resources->upc;
      $resources->cache_is_pack;
      $resources->cache_has_attachments;
      $resources->is_virtual;
      $resources->on_sale;
      $resources->online_only;
      $resources->ecotax;
      $resources->minimal_quantity;
      $resources->wholesale_price;
      $resources->unity;
      $resources->unit_price_ratio;
      $resources->additional_shipping_cost;
      $resources->customizable;
      $resources->text_fields;
      $resources->uploadable_files;
      $resources->active = 1;
      //$resource->redirect_type;
      //$resource->id_product_redirected;
      $resources->available_for_order = 1;
      $resources->available_date;
      $resources->condition;
      $resources->show_price = 1;
      $resources->indexed;
      $resources->visibility;
      $resources->advanced_stock_management;
      $resources->date_add;
      $resources->date_upd;
        
        
        $opt = array('resource' => 'products');
        $opt['postXml'] = $xml -> asXML();
        $xml = $webService -> add($opt);

 

       //Upload of product image

 

 

        $url = 'localhost/tienda/api/images/products/id_del _producto';  //Or any number id product of the shop

        $file_name = realpath('./m.jpg');  //     Trial image

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);

        curl_setopt($ch, CURLOPT_POST, true);

 
        curl_setopt($ch, CURLOPT_USERPWD,'KEY_DE_WEBSERVICE'.":");

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

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        if(curl_exec($ch) === false){
            echo 'Error : '.curl_error($ch).'<br>';
            }
        else {
            echo 'Producto añadido';
            //La siguiente variable es la contiene el archivo de imagen proveniente de la camara del móvil
            //echo 'El nombre de la imagen es:  '.$_FILES["file"]["name"];
            }
        curl_close($ch);    

    }
    


}
catch (Exception $ex) {
    echo 'Se ha producido un error: <br/>'. $ex->getMessage();    
}

if(!defined('_PS_VERSION_'))
    exit;

class Prueba_2 extends Module{
    
    public function __construct(){
        $this->name = 'prueba_2';
        $this->tab = 'front_office_features';
        $this->version = '1.0';
        $this->author = 'myname';
        $this->ps_versions_compliancy = array('min' => '1.5', 'max' => '1.6');
        
        parent::__construct();
        
        $this->displayName = $this->l('Trial 2');
        $this->description = $this->l('This is a description');
        $this->confirmUnistall = $this->l('Are you sure to want to uninstall?');
    }
    
    public function install() {
        if(parent::install()==false)
            return false;
        return true;
    }
    

}


?>

 

Sorry, I failed to copy. Post edited.

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

Problem is that this code doesn't work with any product.


 //Upload of product image

    $url = 'localhost/tienda/api/images/products/id_any_product';   //Or any number id product of the shop

    $file_name = realpath('./m.jpg');  //     Trial image

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_POST, true);

    curl_setopt($ch, CURLOPT_USERPWD,'KEY_DE_WEBSERVICE'.":");

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

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

I want to upload a image for any product created but it doesn't work.

Link to comment
Share on other sites

×
×
  • Create New...