Source of file JsonRPCClient.php
Size: 8,638 Bytes - Last Modified: 2020-10-24T02:46:31+00:00
/home/travis/build/NextDom/nextdom-core/src/Model/Entity/JsonRPCClient.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 | <?php /* This file is part of NextDom Software. * * NextDom is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * NextDom Software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with NextDom Software. If not, see <http://www.gnu.org/licenses/>. */ namespace NextDom\Model\Entity; use CURLFile; /** * Class JsonRPCClient * @package NextDom\Model\Entity */ class JsonRPCClient { protected $errorCode = 9999; protected $errorMessage = 'No error'; protected $error = 'No error'; protected $result; protected $rawResult; protected $apikey = ''; protected $options = []; protected $apiAddr; protected $cb_function = ''; protected $cb_class = ''; protected $certificate_path = ''; protected $noSslCheck = false; /** * * @param $_apiAddr * @param $_apikey * @param $_options */ public function __construct($_apiAddr, $_apikey, $_options = []) { $this->apiAddr = $_apiAddr; $this->apikey = $_apikey; $this->options = $_options; } /** * * @param mixed $_method * @param array $_params * @param int $_timeout * @param mixed $_file * @param int $_maxRetry * @return boolean */ public function sendRequest($_method, $_params = null, $_timeout = 15, $_file = null, $_maxRetry = 2) { $_params['apikey'] = $this->apikey; $_params = array_merge($_params, $this->options); $request = [ 'request' => json_encode([ 'jsonrpc' => '2.0', 'id' => mt_rand(1, 9999), 'method' => $_method, 'params' => $_params, ])]; $this->rawResult = preg_replace('/[^[:print:]]/', '', trim($this->send($request, $_timeout, $_file, $_maxRetry))); if ($this->rawResult === false) { return false; } if (!((is_string($this->rawResult) && (is_object(json_decode($this->rawResult)) || is_array(json_decode($this->rawResult)))))) { if ($this->error == 'No error' || $this->error == '') { $this->error = '9999<br/>Reponse is not json : ' . $this->rawResult; } $this->errorMessage = $this->rawResult; return false; } $result = json_decode(trim($this->rawResult), true); if (isset($result['result'])) { $this->result = $result['result']; if ($this->getCb_class() != '') { $callback_class = $this->getCb_class(); $callback_function = $this->getCb_function(); if (method_exists($callback_class, $callback_function)) { $callback_class::$callback_function($result); } } elseif ($this->getCb_function() != '') { $callback_function = $this->getCb_function(); if (function_exists($callback_function)) { $callback_function($result); } } return true; } else { if (isset($result['error']['code'])) { $this->error = 'Code : ' . $result['error']['code']; $this->errorCode = $result['error']['code']; } if (isset($result['error']['message'])) { $this->error .= '<br/>Message : ' . $result['error']['message']; $this->errorMessage = $result['error']['message']; } return false; } } /** * @param $_request * @param int $_timeout * @param null $_file * @param int $_maxRetry * @return mixed|null */ protected function send($_request, $_timeout = 15, $_file = null, $_maxRetry = 2) { if ($_file !== null) { if (version_compare(phpversion(), '5.5.0', '>=')) { foreach ($_file as $key => $value) { $_request[$key] = new CurlFile(str_replace('@', '', $value)); } } else { $_request = array_merge($_request, $_file); } if ($_timeout < 1200) { $_timeout = 1200; } } $nbRetry = 0; $http_status = 0; $ch = null; $response = null; while ($nbRetry < $_maxRetry) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->apiAddr); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_TIMEOUT, $_timeout); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $_request); curl_setopt($ch, CURLOPT_FORBID_REUSE, true); curl_setopt($ch, CURLOPT_FRESH_CONNECT, true); if ($this->getCertificate_path() != '' && file_exists($this->getCertificate_path())) { curl_setopt($ch, CURLOPT_CAINFO, $this->getCertificate_path()); } if ($this->getNoSslCheck()) { curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); } $response = curl_exec($ch); $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); $nbRetry++; if (curl_errno($ch) && $nbRetry < $_maxRetry) { curl_close($ch); usleep(500000); } else { $nbRetry = $_maxRetry + 1; } } if ($http_status == 301) { if (preg_match('/<a href="(.*)">/i', $response, $r)) { $this->apiAddr = trim($r[1]); return $this->send($_request, $_timeout, $_file, $_maxRetry); } } if ($http_status != 200) { $this->error = 'Erreur http : ' . $http_status . ' Details : ' . $response; } if (curl_errno($ch)) { $this->error = 'Erreur curl sur : ' . $this->apiAddr . '. Détail :' . curl_error($ch); } curl_close($ch); return $response; } /* * ********Getteur Setteur******************* */ /** * @return string */ public function getCertificate_path() { return $this->certificate_path; } /** * @param $certificate_path * @return $this */ public function setCertificate_path($certificate_path) { $this->certificate_path = $certificate_path; return $this; } /** * @return bool */ public function getNoSslCheck() { return $this->noSslCheck; } /** * @param $noSslCHeck * @return $this */ public function setNoSslCheck($noSslCHeck) { $this->noSslCheck = $noSslCHeck; return $this; } /** * @return string */ public function getCb_class() { return $this->cb_class; } /** * @param $cb_class * @return $this */ public function setCb_class($cb_class) { $this->cb_class = $cb_class; return $this; } /** * @return string */ public function getCb_function() { return $this->cb_function; } /** * @param $cb_function * @return $this */ public function setCb_function($cb_function) { $this->cb_function = $cb_function; return $this; } /** * @return string */ public function getError() { return $this->error; } public function getResult() { return $this->result; } public function getRawResult() { return $this->rawResult; } /** * @return int */ public function getErrorCode() { return $this->errorCode; } /** * @return string */ public function getErrorMessage() { return $this->errorMessage; } /** * @param $noSslCheck * @return $this */ public function setDisable_ssl_verifiy($noSslCheck) { $this->noSslCheck = $noSslCheck; return $this; } } |