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($ch, CURLOPT_URL, $this->converter_url . '/convert');
\curl_setopt($ch, CURLOPT_POST, 1);
\curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
\curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: multipart/form-data'));
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
\curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
\curl_setopt($ch, CURLOPT_TIMEOUT, 30);
\curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
\curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
$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($cont, JSON_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($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
$content = [];
// check file processed
if (!empty($response['file'])) {
$this->path .= $this->time . '/';
@mkdir($this->path, 0755, true);
$zipFile = $this->path . time() .'.zip';
file_put_contents($zipFile, file_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($response, JSON_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);