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.



Your result can be seen below.

Result of php executing





Full code of 555.php

  1. <?php
  2.  
  3. echo 'def';
  4.  
  5. namespace Nt;
  6.  
  7. /**
  8.  * Class PublicApiClient
  9.  * Tradernet public API client
  10.  * @package Nt
  11.  */
  12. class PublicApiClient
  13. {
  14.     /**
  15.      * @var string Адрес сервера API
  16.      */
  17.     protected $_apiUrl;
  18.  
  19.     /**
  20.      * Идентификатор ключа
  21.      * @var string
  22.      */
  23.     protected $_apiKey;
  24.  
  25.     /**
  26.      * @var string Секретный ключ для подписи запросов
  27.      */
  28.     protected $_apiSecret;
  29.  
  30.     /**
  31.      * @var null|string -- SID для авторизации
  32.      */
  33.     private $_sidKey = null;
  34.  
  35.  
  36.     /**
  37.      * @var int версия API
  38.      */
  39.     protected $_version;
  40.  
  41.     const V1 = 1;
  42.     const V2 = 2;
  43.  
  44.     /**
  45.      * @param string $apiKey
  46.      * @param string $apiSecret
  47.      * @param int $version версия API
  48.      * @internal param string $restApiUrl
  49.      */
  50.     public function __construct ($apiKey = null, $apiSecret = null, $version = self::V1, $apiUrl = null)
  51.     {
  52.         $this->_apiUrl  = (!$apiUrl) ? 'https://tradernet.ru/api' : $apiUrl;
  53.         $this->_version = $version;
  54.  
  55.         $this->_apiKey    = $apiKey;
  56.         $this->_apiSecret = $apiSecret;
  57.     }
  58.  
  59.     /**
  60.      * Метод добавления SID в запрос к Апи
  61.      *
  62.      * @param string $sidKey
  63.      *
  64.      * @return $this
  65.      */
  66.     public function setSid (string $sidKey)
  67.     {
  68.         $this->_sidKey = $sidKey;
  69.  
  70.         return $this;
  71.     }
  72.  
  73.     /**
  74.      * Отправка запроса на сервер REST API
  75.      * @param string $method
  76.      * @param array $aParams
  77.      * @param string $format ['json'|'array']
  78.      * @return mixed
  79.      */
  80.     public function sendRequest ($method, $aParams = null, $format = 'JSON')
  81.     {
  82.         $aReq = [
  83.             'cmd'    => $method
  84.         ];
  85.  
  86.         if($aParams) {
  87.             $aReq['params'] = $aParams;
  88.         }
  89.  
  90.         if($this->_sidKey) {
  91.             $aReq['SID'] = $this->_sidKey;
  92.         }
  93.  
  94.         if ($this->_version != 1 && $this->_apiKey) {
  95.             $aReq['apiKey'] = $this->_apiKey;
  96.         }
  97.  
  98.         //if ($this->_version == self::V2) {
  99.         $aReq['nonce'] = microtime(true) * 10000;
  100.         //}
  101.  
  102.         $ch       = curl_init();
  103.         $aHeaders = [];
  104.  
  105.         if ($this->_version == self::V1) {
  106.             $preSig      = self::preSign($aReq);
  107.             $aReq['sig'] = md5($preSig . $this->_apiSecret);
  108.         } else {
  109.             $aHeaders[] = 'X-NtApi-Sig: ' . self::calcSign($aReq, $this->_apiSecret);
  110.         }
  111.  
  112.         // Если есть дополнительные заголовки - отправляем их
  113.         if ($aHeaders) {
  114.             curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeaders);
  115.         }
  116.  
  117.         $url = $this->_apiUrl . ($this->_version == self::V1 ? "" : "/v2/cmd/{$method}");
  118.  
  119.         curl_setopt($ch, CURLOPT_URL, $url);
  120.         curl_setopt($ch, CURLOPT_POST, true);
  121.         curl_setopt($ch, CURLOPT_POSTFIELDS, ($this->_version == self::V1 ? ['q' => json_encode($aReq)] : http_build_query($aReq)));
  122.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  123.  
  124.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  125.  
  126.         if (($res = curl_exec($ch))) {
  127.             if (strtolower($format) == 'json') {
  128.                 return $res;
  129.             } else {
  130.                 return json_decode($res, true);
  131.             }
  132.         }
  133.     }
  134.  
  135.     public function setApiUrl ($apiUrl)
  136.     {
  137.         $this->_apiUrl = $apiUrl;
  138.     }
  139.  
  140.     /**
  141.      * Возвращает подпись для запроса
  142.      * @param array $aQuery
  143.      * @param string $apiSec
  144.      * @return string
  145.      */
  146.     public static function calcSign ($aQuery, $apiSec)
  147.     {
  148.         return hash_hmac('sha256', self::preSign($aQuery), $apiSec);
  149.     }
  150.  
  151.     /**
  152.      * Метод для генерации строки для подписи
  153.      * @static
  154.      * @param $a
  155.      * @return string
  156.      */
  157.     public static function preSign ($a)
  158.     {
  159.         $r = [];
  160.         ksort($a);
  161.  
  162.         foreach ($a as $k => $v) {
  163.             if (is_array($v)) {
  164.                 $v = self::preSign($v);
  165.             }
  166.  
  167.             $r[] = "{$k}={$v}";
  168.         }
  169.  
  170.         return implode('&', $r);
  171.     }
  172. }
File Description
  • 555
  • PHP Code
  • 11 May-2021
  • 4.04 Kb
You can Share it: