Prestashop Custom Webservice

2

Good morning, I am developing a module that will make use of the webservices functionality of Prestashop 1.6.

In my case I try to send data from one store to another with a custom web service, that is, I have created a webservice of my own, activated the methods that interest me, but when implementing the CRUD functions, I am a bit lost .

I would like to know how URLs are interpreted by passing parameters to, for example, consult an item by ID, delete an item by ID or create a new one. Yes, using the way to implement as done here: link

Edit: I put here the explanation and the code. The data to recover and record is recovered / saved in a new table that I have registered.

This is the class that extends the webservice request core to implement my own webservice

class WebserviceRequest extends WebserviceRequestCore
{
    public function __construct()          
    {
        include_once(_PS_MODULE_DIR_.'directorioModulo'.DIRECTORY_SEPARATOR.'models'.DIRECTORY_SEPARATOR.'ClaseParaImplementarModuloCustom.php');
    }
}

This ClassForImplementModuleCustom.php is where all the Webservice logic should be done. (I understand the CRUD functions)

class CentralizedStoreAssociated extends ObjectModel
{
    //Campos de la clase
    public $id;
    public $store_name;
    public $store_url;
    public $store_api_key;
    public $store_contact_email;

    //Definición de la clase/objeto
    public static $definition = [
        'table'     => 'centralized_stores',
        'primary'   => 'id_centralizedstore',
        'multilang' => false,
        'multilang_shop' => false,
        'fields' => array(
            'store_id' => ['type' => self::TYPE_INT, 'validate' => 'isInt', 'size' => 256],
        ),
    ];
}

//Definición de los parámetros del servicio web. Aquí es donde entiendo que deben definirse las funciones para el CRUD en associations.
protected $webserviceParameters = [
        'objectNodeName' => 'store',
        'objectsNodeName' =>'centralized_stores',
        'associations' => [
            'storeDetails' => array('getter' => 'getStoreDetails',     'resource' => 'storeDetails',
            'fields' => array(
                'store_name'            => ['type' => self::TYPE_STRING, 'validate' => 'isString', 'size' => 256],
                'store_url'             => ['type' => self::TYPE_STRING, 'validate' => 'isUrl', 'size' => 2038],
                'store_api_key'         => ['type' => self::TYPE_STRING, 'validate' => 'isString', 'size' => 32],
                'store_contact_email'   => ['type' => self::TYPE_STRING, 'validate' => 'isEmail', 'size' => 254],
            ),
        ),
    ]
];

Finally I have defined a function for the getter = > getStoreDetails that is nothing more than a function to obtain the data of a store from the custom table, by means of the URL link

I hope I have made everything clearer.

    
asked by Makros 19.10.2016 в 08:27
source

1 answer

0

Good, you will have to make use of $ _ POST and $ _ GET , both transport data, but the difference is:

GET: takes the data in a "visible" way to the client (web browser). The means of delivery is the URL. The data can be seen by anyone. Ex: link questions / 28654 / prestashop-custom-webservice strong>.

POST: consists of "hidden" data (because the client does not see them) sent by a form whose post method is. It is suitable for forms. The data is not visible.

The advantage of using POST is that this data is not visible to the user of the web. In the case of using get, the user could modify the URL by writing different parameters to the real ones in his browser, giving rise to the fact that the information treated is not the one planned.

From here you have to make your CRUD, I recommend you use the POST method, I leave you a simple example of code:

<body>
<form name="formularioDatos" method="post" action="ejemploRequest2.php">
<p> CÁLCULO DEL PRECIO MEDIO DE UN PRODUCTO </p>
<br/>
Introduzca el precio del producto en el establecimiento número 1, en euros: <input type="text" name="precio1" value="">
<br/> <br/>
Introduzca el precio del producto en el establecimiento número 1, en euros: <input type="text" name="precio2" value="">
<br/> <br/>
Introduzca el precio del producto en el establecimiento número 3, en euros: <input type="text" name="precio3" value="">
<br/> <br/>
<input value="Calcular" type="submit" />
</form>
</body>

    
answered by 19.10.2016 в 08:37