Read More
Read More
Menu Close

    How to send PUT/POST request in JSON using PrestaShop webservices

    In this blog, We are going to learn how we can send JSON data as PUT/POST requests in the PrestaShop webservice.

    As we know that in PrestaShop, We can get output data as JSON with output_format=JSON but can’t post body as JSON. For this, we need to convert our JSON request data into XML.

    To achieve this, You have to follow some steps, which we have explained below.

    Firstly, We need to make some changes in the PrestaShop WebserviceRequest class. Here is the file path below.

    Path: ROOT_DIR/classes/webservice/WebserviceRequest.php

    Searching for an experienced
    Prestashop Company ?
    Read More

    Now we need to modify the existing “saveEntityFromXml” function to convert request data JSON to XML. Find “saveEntityFromXml” & add this below code at the starting of this function.

    // Code added to convert JSON data into XML
    $requestJson = false;
    if ($_GET['output_format'] == 'JSON') {
        $requestJson = true;
    }
    if ($requestJson){
        $this->_inputXml = $this->jsonToXml($this->_inputXml);
    }
    // Code end

    Like this:

    Code

    Note: We need to add “output_format=JSON” as a query parameter in API to manage things.

    Now there are some functions that you need to put in the same (WebserviceRequest) class. These functions will convert JSON to XML.

    /**
    * Function to convert json to XMl
    *
    * @param [type] $json
    * @return void
    */
    public function jsonToXml($json) {
        $obj = json_decode($json, true);
        $xml = $this->arrayToXml($obj);
    
        return $xml;
    }
    
    public function arrayToXml($array)
    {
        $output = '';
        header('Content-type: application/xml');
        $output = '<?xml version="1.0"?>'."\n";
        foreach($array as $key => $value){
            if (is_array($value)) {
                $output .= $this->arrayOfArrayToXml($value, $key);
            } else {
                if (!is_array($value)) {
                    $node_content = '<![CDATA['.$value.']]>';
                    $output .= '<'.$key.'>'.$node_content.'</'.$key.'>'."\n";
                }
            }
        }
    
        return $output;
    }
    
    public function arrayOfArrayToXml($array, $key, $output = '')
    {
        $output .= '<'.$key.'>'."\n";
        foreach ($array as $name => $val) {
            if (is_array($val)) {
                $output .= $this->arrayOfArrayToXml($val, $name);
            } else {
                $output .= '<'.$name.'><![CDATA['.$val.']]></'.$name.'>'."\n";
            }
        }
        $output .= '</'.$key.'>'."\n";
    
        return $output;
    }

    PUT/POST Request:

    {
       "customer": {
          "lastname": "Doe",
          "firstname": "John",
          "email": "[email protected]",
          "associations": {
             "groups": {
                "group": {
                   "id": 2
                }
             }
          }
       }
    }

    In conclusion, You can see that you have passed the JSON data as request & it’s changed in XML to manage the further processes. Check the result below.

    Result-1

    That’s all about this blog. Hope it will help you.

    If you are facing any issues or doubts in the above process, please feel free to contact us through the comment section.

    Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

    For any doubt contact us at [email protected]

    . . .
    Discuss on Helpdesk

    Leave a Comment

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


    Be the first to comment.

    Back to Top