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

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;
}
Hey folks, I'm Vivek Kumar Pandey, a software engineer with a passion for crafting elegant solutions to complex problems. From the bustling streets of Mumbai to the heart of Bangalore's tech scene, I've journeyed through the world of programming, leaving my mark one line of code at a time. Join me as I continue to explore, innovate, and push the boundaries of what's possible in the digital realm.

Related Posts

Understanding PostgreSQL Sequences: A Comprehensive Guide

PostgreSQL sequences are a powerful feature used to generate unique identifiers automatically. These are especially useful for primary keys in database tables. In this post, we’ll explore…

Download the “Online News Portal” Project – PHP and MySQL

Looking to create a robust Online News Portal? This project, developed in PHP and MySQL, is designed to provide a platform where admins can manage news articles,…

Download the “Online Job Portal” Project – PHP and MySQL

Are you looking to kickstart your own job portal website? Our “Online Job Portal” project is the perfect solution! Built using PHP and MySQL, this project offers…

Integrating Cashfree Payment Gateway in Laravel: A Step-by-Step Guide

In this article, we’ll walk you through the process of integrating the Cashfree Payment Gateway into a Laravel application. This guide will cover the setup of routes,…

Enhancing CKEditor with Font and Background Color Options

In this article, we will guide you through the process of enhancing your CKEditor with font and background color options. By the end of this tutorial, you…

Exporting and Importing SQL Databases in phpMyAdmin Using XAMPP and Command Prompt

Managing databases is an essential task for developers and administrators alike. Whether you’re migrating data, backing up your database, or deploying updates, having the right tools and…

Leave a Reply

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