Php

Prestashop Webservice In Post/Put JSON Data In the body

To Post/Put data Prestashop Web service uses XML. If you want to use JSON, you need to modify Webservice and convert JSON to XML or directly JSON to data. This article is on the same topic that how we will use the data in json format in post or put method.

In Prestashop’s Webservice, if you want to get the data in json format, then it becomes, we can get output data as JSON with output_format=JSON but seems that can’t post body as JSON.

You have to follow some steps, which I am explaining step by step below.

1. PHP library XML_Serializer

In this step you have to add the PHP library XML_Serializer, you can put it in a new directory called “XML”

prestashop_directory\classes\webservice\XML\

Now you can download the PHP library Now you can download the PHP library file given below and unzip it inside the xml folder

Download PHP Library

Like that :

After zipping you will find the Serializer.php file in xml/XML_Serializer-master/XML/Serializer.php

We are going to use this file to convert the data from json to xml.

2. Include the library to the file: \ classes \ webservice \ WebserviceRequest.php

include("xml/XML_Serializer-master/XML/Serializer.php"); 

In WebserviceRequestCore file you have to include Serializer.php as you can see above code has to be added.

Now you have to add this function in WebserviceRequestCore class, this function will convert json to xml

    public function jsonToxml($json) {
        $serializer = new XML_Serializer();
        $obj = json_decode($json);

        if ($serializer->serialize($obj)) {
            return $serializer->getSerializedData();
        }
        else {
            return null;
        }
    }

You can see in the image below how I have added this function

4. $requestJson variable in webserviceRequest.php

Find “saveEntityFromXml”

You have to add this line in this function.

     $requestJson = true;

    if ($requestJson){
        $this->_inputXml = $this->jsonToxml($this->_inputXml);
    }

Like This

 protected function saveEntityFromXml($successReturnCode)
    {

         $requestJson = true;

        if ($requestJson){
            $this->_inputXml = $this->jsonToxml($this->_inputXml);
        }

        try {
            $xml = new SimpleXMLElement($this->_inputXml);
        } catch (Exception $error) {
            $this->setError(500, 'XML error : ' . $error->getMessage() . "\n" . 'XML length : ' . strlen($this->_inputXml) . "\n" . 'Original XML : ' . $this->_inputXml, 127);

            return;
        }

        /** @var SimpleXMLElement|Countable $xmlEntities */
        $xmlEntities = $xml->children();
        $object = null;

        $ids = [];
        foreach ($xmlEntities as $entity) {
            // To cast in string allow to check null values
            if ((string) $entity->id != '') {
                $ids[] = (int) $entity->id;
            }
        }

4. switch ($type) in default as “WebserviceOutputJSON”

Change switch type to default as “WebserviceOutputJSON” so bydefault request will be stored in json format.

Find “getOutputObject”

And Add this line

          case 'XML':
                 $obj_render = new WebserviceOutputXML();
                  break;

         default:
              require_once dirname(__FILE__).'/WebserviceOutputJSON.php';
                $obj_render = new WebserviceOutputJSON();
                break;

Full Function

    protected function getOutputObject($type)
    {
        // set header param in header or as get param
        $headers = self::getallheaders();
        if (isset($headers['Io-Format'])) {
            $type = $headers['Io-Format'];
        } elseif (isset($headers['Output-Format'])) {
            $type = $headers['Output-Format'];
        } elseif (isset($_GET['output_format'])) {
            $type = $_GET['output_format'];
        } elseif (isset($_GET['io_format'])) {
            $type = $_GET['io_format'];
        }
        $this->outputFormat = $type;
        switch ($type) {
            case 'JSON':
                require_once __DIR__ . '/WebserviceOutputJSON.php';
                $obj_render = new WebserviceOutputJSON();

                break;
            case 'XML':
                 $obj_render = new WebserviceOutputXML();
            default:
              require_once dirname(__FILE__).'/WebserviceOutputJSON.php';
                $obj_render = new WebserviceOutputJSON();
                break;

               
        }

        return $obj_render;
    }

Now you are ready now you can send data in json format in your post or put method Try the POST or PUT method from Postman or SoapUI

One reply on “Prestashop Webservice In Post/Put JSON Data In the body”

Leave a Reply

Your email address will not be published. Required fields are marked *