Prestashop create and update resources via url webservice api

I (unfortunately) have to write the client application as a front-end to a website based on a preliminary protocol. I would need to perform all CRUD operations on resources through the url web service, and I'm stuck on creating and updating. I was very surprised to learn how few examples are available on the Internet, since prestashop should be a widely used cms. However, so far I have come to:

GET http://www.myshop.com/api/myresource/id 

to get xml for the resource with the given id or

 GET http://www.myshop.com/api/myresource/?display=full&filter[field]=[value] 

to filter the results. I write this only because I had to fight with Google in order to achieve this information, these filters can help someone in the future.

 DELETE http://www.myshop.com/api/myresource/id 

to remove a resource (I suppose filters also work, but have not tried so far)

Now, does anyone know how to create and update resources? I tried

 POST/PUT http://www.myshop.com/api/myresource/ 

providing pairs of field values ​​in the parameters of the http request with no luck, I get an internal server error. No, I don’t have direct access to the server, so I don’t know what error was thrown, and I hope that it can be solved without the support of websites, I would like to keep my ammunition.

The only useful resource I found on the Internet, this one , I also looked among other questions about SO, and the suggested links in this question were without help.

+4
source share
3 answers

If you edit the config / config.inc.php file and add the following, you may see errors in the response (error) (1by1, which is very unpleasant when you try to understand why the creation fails). Remember to comment out the line when you are done.

 /*DEBUGGING ON*/ ini_set('display_errors', 'on'); 

You also need to make sure that you add data to your POST / PUT operation using xml = <prestashop>......</prestashop> (I use C # for my Client, so this may not be necessary with PHP)

if you use ?schema=synopsis to get yours empty, make sure you look at any elements in the XML that are there as placeholders and delete them. For example, your (products) will be broken, but work if you do not.

I also added to my code that in the initial XML pass, it completely walks the tree, searches for something with the not_filterable attribute and deletes these nodes (once again there is not much documentation to find out if this thing is right to do).

EDIT: Another note in and out of language-based elements, make sure you use CDATA for the elements.

I just succeeded in creating my products by doing this, so I hope for help. I have yet to upgrade an existing one.

UPDATE: since then I upgraded to 1.5.4.1 Prestashop and started updating resources in my case

Refresh Resource

URI: http://site/api/products/1

Use Method = "PUT"

content type = "text/xml"

Be sure to remove all items that have the not_filterable attribute. (I do not understand why, but it will not work if you do not)

Delete Resource

URI: http://site/api/products/1

Use Method = "DELETE"

content type = "application/x-www-form-urlencoded"

I found that you do not need a body, so you can set ContentLength to 0. and probaly really does not need to set the content type, but it works.

+2
source

I found the answer in the source code, prestashop / webservice / dispatcher.php, it was not necessary to set the fields / values ​​as http parameters, but rather to pass the whole xml containing at least all the required fields if the message is used to create a new record or just the fields you want to update if you request put, therefore

 http://www.myshop.com/api/myresource/?xml=myXmlString 
+1
source

If you look at the documentation, you can request an empty xml file with all the fields of each resource. You must do:

 http://mystore.com/api/[resource name]?schema=blank 

Then, if you need more information about the fields (as some of them are required), simply do:

 http://mystore.com/api/[resource name]?schema=synopsis 
-2
source

Source: https://habr.com/ru/post/1480778/


All Articles