产品图像(未找到)通过Web服务

时间:2015-12-03 17:41:02

标签: php web-services api upload prestashop

我有一个问题,当我上传一个带有Web Service的产品时,它创建没问题,但找不到它的图像。下面我发布产品和图片上传代码以及一些照片。

       <?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 'Added Product';
            }
        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;
    }


}


?>

[IMG] http://i67.tinypic.com/b67w47.jpg[/IMG]

[IMG] http://i67.tinypic.com/6ydu2u.jpg[/IMG]

我需要帮助,这对我来说非常重要

3 个答案:

答案 0 :(得分:1)

如果使用url添加图像,则首先需要将该图像存储在本地文件夹中,然后需要将该图像的本地路径添加到$ file_name。 另外在curl_setopt中使用@而不是使用curlfile它会更好。

答案 1 :(得分:0)

问题是此代码不适用于任何产品。

 //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);

我想为所创建的任何产品上传图片,但它不起作用。

答案 2 :(得分:0)

我尝试了与您相同的代码,实际上它是您在Prestashop documentation中找到的代码并且它可以正常工作。 我认为唯一的区别在于代码中出现在路径中的图像类型。该文档没有使用该类型。