123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- namespace app\controllers;
- use Yii;
- use yii\BaseYii;
- use yii\db\Query;
- use app\models\User;
- use app\models\Person;
- use yii\rest\Controller;
- use app\behaviors\MyBehavior;
- use app\filters\BodyParamAuth;
- class MainController extends Controller
- {
- public $datas = ['success' => false, 'authorized' => false];
- public $errorCurlRequest;
- /*
- / определяет ключ авторизации из заголовка
- /
- */
-
- public function beforeAction($action)
- {
-
- //$this->checkAuth();
- return parent::beforeAction($action);
- //Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
- // return parent::beforeAction($action);
- }
-
- public function checkAuth($key = '')
- {
-
- if( Yii::$app->params['api']['authAppKey'] == $key ) {
- $this->datas['authorized'] = true;
- } else {
- throw new \yii\web\HttpException(401);
- }
- }
-
- public function curlRequest($url, $arrayParams = [] )
- {
-
- $arrayParams = $json_encode($arrayParams);
- $result = '';
- //$url = $arrayParams['url'];//'http://localhost/api/web/locomotive/index';
-
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Accept: application/json","AppKey: " . Yii::$app->params['api']['appKey']));
- curl_setopt($curl, CURLOPT_TIMEOUT, 20);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $arrayParams);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- $result = curl_exec($curl);
- if ( !$result ){
- $this->errorCurlRequest = curl_getinfo($curl, CURLINFO_HEADER_OUT);
- return '';
- }
- curl_close($curl);
- return $result;
-
- }
-
- /*
- * Возвращает время в милисекундах
- */
-
- public function seconds()
- {
- return explode(' ', microtime())[1];
- //$mt = explode(' ', microtime());
- //((int)$mt[1]) * 1000 + ((int)round($mt[0] * 1000));
- }
-
- }
- ?>
|