MainController.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use yii\BaseYii;
  5. use yii\db\Query;
  6. use app\models\User;
  7. use app\models\Person;
  8. use yii\rest\Controller;
  9. use app\behaviors\MyBehavior;
  10. use app\filters\BodyParamAuth;
  11. class MainController extends Controller
  12. {
  13. public $datas = ['success' => false, 'authorized' => false];
  14. public $errorCurlRequest;
  15. /*
  16. / определяет ключ авторизации из заголовка
  17. /
  18. */
  19. public function beforeAction($action)
  20. {
  21. //$this->checkAuth();
  22. return parent::beforeAction($action);
  23. //Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  24. // return parent::beforeAction($action);
  25. }
  26. public function checkAuth($key = '')
  27. {
  28. if( Yii::$app->params['api']['authAppKey'] == $key ) {
  29. $this->datas['authorized'] = true;
  30. } else {
  31. throw new \yii\web\HttpException(401);
  32. }
  33. }
  34. public function curlRequest($url, $arrayParams = [] )
  35. {
  36. $arrayParams = $json_encode($arrayParams);
  37. $result = '';
  38. //$url = $arrayParams['url'];//'http://localhost/api/web/locomotive/index';
  39. $curl = curl_init();
  40. curl_setopt($curl, CURLOPT_URL, $url);
  41. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Accept: application/json","AppKey: " . Yii::$app->params['api']['appKey']));
  42. curl_setopt($curl, CURLOPT_TIMEOUT, 20);
  43. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  44. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  45. curl_setopt($curl, CURLOPT_POST, 1);
  46. curl_setopt($curl, CURLOPT_POSTFIELDS, $arrayParams);
  47. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  48. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  49. $result = curl_exec($curl);
  50. if ( !$result ){
  51. $this->errorCurlRequest = curl_getinfo($curl, CURLINFO_HEADER_OUT);
  52. return '';
  53. }
  54. curl_close($curl);
  55. return $result;
  56. }
  57. /*
  58. * Возвращает время в милисекундах
  59. */
  60. public function seconds()
  61. {
  62. return explode(' ', microtime())[1];
  63. //$mt = explode(' ', microtime());
  64. //((int)$mt[1]) * 1000 + ((int)round($mt[0] * 1000));
  65. }
  66. }
  67. ?>