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

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…

Mastering Cron Expressions: A Comprehensive Guide

Cron expressions are a powerful tool that enables automation and task scheduling on Unix-like operating systems. Whether you’re a systems administrator, a developer, or simply someone interested…

How to Install an SSL Certificate on a Bitnami LAMP Stack Running on CentOS 7

Securing your website with an SSL certificate is essential to protect sensitive information and build trust with your visitors. In this tutorial, we’ll guide you through the…

Transfer Files using SFTP In Window

In this article, I am going to learn you how you can download any file from any server, for that no software is required, just you must…

Leave a Reply

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