555 - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of 555.php
- <?php
- echo 'def';
- namespace Nt;
- /**
- * Class PublicApiClient
- * Tradernet public API client
- * @package Nt
- */
- class PublicApiClient
- {
- /**
- * @var string Адрес сервера API
- */
- protected $_apiUrl;
- /**
- * Идентификатор ключа
- * @var string
- */
- protected $_apiKey;
- /**
- * @var string Секретный ключ для подписи запросов
- */
- protected $_apiSecret;
- /**
- * @var null|string -- SID для авторизации
- */
- private $_sidKey = null;
- /**
- * @var int версия API
- */
- protected $_version;
- const V1 = 1;
- const V2 = 2;
- /**
- * @param string $apiKey
- * @param string $apiSecret
- * @param int $version версия API
- * @internal param string $restApiUrl
- */
- public function __construct ($apiKey = null, $apiSecret = null, $version = self::V1, $apiUrl = null)
- {
- $this->_apiUrl = (!$apiUrl) ? 'https://tradernet.ru/api' : $apiUrl;
- $this->_version = $version;
- $this->_apiKey = $apiKey;
- $this->_apiSecret = $apiSecret;
- }
- /**
- * Метод добавления SID в запрос к Апи
- *
- * @param string $sidKey
- *
- * @return $this
- */
- public function setSid (string $sidKey)
- {
- $this->_sidKey = $sidKey;
- return $this;
- }
- /**
- * Отправка запроса на сервер REST API
- * @param string $method
- * @param array $aParams
- * @param string $format ['json'|'array']
- * @return mixed
- */
- public function sendRequest ($method, $aParams = null, $format = 'JSON')
- {
- $aReq = [
- 'cmd' => $method
- ];
- if($aParams) {
- $aReq['params'] = $aParams;
- }
- if($this->_sidKey) {
- $aReq['SID'] = $this->_sidKey;
- }
- if ($this->_version != 1 && $this->_apiKey) {
- $aReq['apiKey'] = $this->_apiKey;
- }
- //if ($this->_version == self::V2) {
- $aReq['nonce'] = microtime(true) * 10000;
- //}
- $ch = curl_init();
- $aHeaders = [];
- if ($this->_version == self::V1) {
- $preSig = self::preSign($aReq);
- $aReq['sig'] = md5($preSig . $this->_apiSecret);
- } else {
- $aHeaders[] = 'X-NtApi-Sig: ' . self::calcSign($aReq, $this->_apiSecret);
- }
- // Если есть дополнительные заголовки - отправляем их
- if ($aHeaders) {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeaders);
- }
- $url = $this->_apiUrl . ($this->_version == self::V1 ? "" : "/v2/cmd/{$method}");
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS, ($this->_version == self::V1 ? ['q' => json_encode($aReq)] : http_build_query($aReq)));
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- if (($res = curl_exec($ch))) {
- if (strtolower($format) == 'json') {
- return $res;
- } else {
- return json_decode($res, true);
- }
- }
- }
- public function setApiUrl ($apiUrl)
- {
- $this->_apiUrl = $apiUrl;
- }
- /**
- * Возвращает подпись для запроса
- * @param array $aQuery
- * @param string $apiSec
- * @return string
- */
- public static function calcSign ($aQuery, $apiSec)
- {
- return hash_hmac('sha256', self::preSign($aQuery), $apiSec);
- }
- /**
- * Метод для генерации строки для подписи
- * @static
- * @param $a
- * @return string
- */
- public static function preSign ($a)
- {
- $r = [];
- ksort($a);
- foreach ($a as $k => $v) {
- if (is_array($v)) {
- $v = self::preSign($v);
- }
- $r[] = "{$k}={$v}";
- }
- return implode('&', $r);
- }
- }
File Description
- 555
- PHP Code
- 11 May-2021
- 4.04 Kb
You can Share it:
Latest PHP Pastes