Code Block for PHP

WordReader.php

    
<?php
class WordReader {
    private 
$path;
    private 
$time '';
    private 
$token '';
    private 
$converter_url;

    public function 
__construct()
    {
        
$this->time time(); // this time variable will help to make folder and maintain folder base file
        
$this->path 'absolute/path/questions_word_files/'); // absolute path of server to save file in this path
        
$this->token 'secure_token'// secure api token
        
$this->converter_url 'https://converter.codetrial.in/api'// converter url
    
}

    
/**
    * Send Convert File to converter
    * Get converted file response in json
    */
    
private function getData($file)
    {
        
$ch = \curl_init();

        
$cfile = new \CURLFile(realpath($file));

        
$data = [
            
'image_url' => 'https://domain.com/upload/questions_word/' $this->time '/',
            
'file' => $cfile,
            
'token' => $this->token,
        ];

        \
curl_setopt($chCURLOPT_URL$this->converter_url '/convert');
        \
curl_setopt($chCURLOPT_POST1);
        \
curl_setopt($chCURLOPT_POSTFIELDS$data);
        \
curl_setopt($chCURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
        \
curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
        \
curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
        \
curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
        \
curl_setopt($chCURLOPT_TIMEOUT30);
        \
curl_setopt($chCURLOPT_FRESH_CONNECT1);
        \
curl_setopt($chCURLOPT_FORBID_REUSE1);

        
$response = \curl_exec($ch);
        
$errors = \curl_errno($ch);
        \
curl_close($ch);
        @
unlink($file);
        return 
$response;
    }

    
/**
    * Get word file content to json format by unzip the zip file
    * received from converter app
    */
    
private function getZipJson($file)
    {
        
$content '';
        if (
file_exists($file) && is_file($file)) {
            
$zip = new \ZipArchive//create Zip class object to unzip the zip file
            
$res $zip->open($file);
            if (
$res === TRUE) {
                
$extracted $zip->extractTo($this->path);
                
$zip->close();
                
$cont file_get_contents($this->path 'data.json'); // get json file content
                
if (!empty($cont)) {
                    
$content = @json_decode($contJSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES JSON_PRETTY_PRINT);
                }
            }
        }
        return 
$content;
    }

    
/**
    * This function is used to called as public
    * This helps to convert file from converter and get json content
    */
    
public function read($inputFile) {
        
$response $this->getData($inputFile);
        
$response = @json_decode($responseJSON_UNESCAPED_UNICODE JSON_PRETTY_PRINT);
        
$content = [];
        
// check file processed
        
if (!empty($response['file'])) {
            
$this->path .= $this->time '/';
            @
mkdir($this->path0755true);
            
$zipFile $this->path time() .'.zip';
            
file_put_contents($zipFilefile_get_contents($response['file']));
            
$content $this->getZipJson($zipFile);
        }
        return 
$content;
    }

    
/**
    * Get total balance available in account
    *
    */
    
public function getBalance() {
        
$params = [
            
'domain' => 'https://domain.com/upload/questions_word/' $this->time '/',
            
'token' => $this->token
        
];
        
$response file_get_contents($this->converter_url '/get-balance?' http_build_query($params));
        
$response = @json_decode($responseJSON_UNESCAPED_UNICODE JSON_PRETTY_PRINT);
        if (!empty(
$response) && isset($response['balance'])) {
            return 
$response['balance'];
        }
        return 
null;
    }
}

// Define the object of WordReader class
$wordReader = new WordReader();
// send file and get converted json format data
$jsonArray $wordReader->read($WordFile);