MainModel.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\Model;
  5. use app\models\entity\Accounts;
  6. use app\models\Logs;
  7. use app\models\logger\LoggerFile;
  8. class MainModel extends Model
  9. {
  10. public $errors = [];
  11. public $message = [];
  12. protected $logger;
  13. protected $log;
  14. public function __construct() {
  15. $this->log = new LoggerFile();
  16. }
  17. /*
  18. * Делает пост запрос
  19. */
  20. public function curlRequest($url, $arrayParams = [] )
  21. {
  22. // var_dump($arrayParams);
  23. // var_dump($url);
  24. $arrayParams = json_encode($arrayParams);
  25. $result = '';
  26. $curl = curl_init();
  27. curl_setopt($curl, CURLOPT_URL, $url);
  28. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Accept: application/json","AppKey: " . Yii::$app->params['api']['appKey']));
  29. //curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Accept: application/json"));
  30. curl_setopt($curl, CURLOPT_USERPWD, "ПройдаковИА:Ip15351112");
  31. curl_setopt($curl, CURLOPT_TIMEOUT, 360);
  32. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  33. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  34. curl_setopt($curl, CURLOPT_POST, 1);
  35. curl_setopt($curl, CURLOPT_POSTFIELDS, $arrayParams);
  36. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  37. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  38. $result = curl_exec($curl);
  39. //var_dump(curl_getinfo($curl, CURLINFO_RESPONSE_CODE));
  40. if ( !$result ){
  41. $this->errors['surlRequest'] = curl_getinfo($curl, CURLINFO_HEADER_OUT);
  42. return '';
  43. }
  44. curl_close($curl);
  45. return $result;
  46. }
  47. public function curlRequest2($params)
  48. {
  49. $urlWithParam = $params['url'] . '?user_id=' . $params['param'];
  50. $curl = curl_init();
  51. curl_setopt($curl, CURLOPT_URL, $urlWithParam);
  52. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  53. curl_setopt($curl, CURLOPT_USERPWD, $params['login'] . ":" . $params['pass']);
  54. //curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Accept: application/json","AppKey:6a7bdbaa-6bc1-4159-a2f4-07a13c8907d1"));
  55. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Accept: application/json","AppKey:33cd23bb-3b03-4638-afef-7104a13e9211"));
  56. curl_setopt($curl, CURLOPT_TIMEOUT, 20);
  57. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  58. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  59. curl_setopt($curl, CURLOPT_POST, 1);
  60. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  61. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  62. $result = curl_exec($curl);
  63. curl_close($curl);
  64. return $result;
  65. }
  66. public function curlRequestUni( $params , $method = 'get'){
  67. $par = [];
  68. $curl = curl_init();
  69. foreach( $params['param'] as $key => $item) {
  70. $par[] = $key .'=' . $item ;
  71. }
  72. $parString = implode('&',$par);
  73. //var_dump(json_encode($params['param']));
  74. if ( 'post' == $method ){
  75. $parString = '';
  76. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($params['param']));
  77. curl_setopt($curl, CURLOPT_POST, 1);
  78. }
  79. $urlWithParam = $params['url'].'?'. $parString ;
  80. curl_setopt($curl, CURLOPT_URL, $params['url']);
  81. //curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  82. //curl_setopt($curl, CURLOPT_USERPWD, $params['login'] . ":" . $params['pass']);
  83. //curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Accept: application/json", "AppKey: 6a7bdbaa-6bc1-4159-a2f4-07a13c8907d1"));
  84. curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json","Accept: application/json", "AppKey: 33cd23bb-3b03-4638-afef-7104a13e9211"));
  85. curl_setopt($curl, CURLOPT_TIMEOUT, 30);
  86. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  87. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  88. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  89. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  90. //echo '<pre>';
  91. $result = curl_exec($curl);
  92. //var_dump(curl_getinfo($curl));
  93. //echo '</pre>';
  94. //Logs::addlog(2,$urlWithParam );
  95. // Logs::addlog(2,$result);
  96. return $result;
  97. }
  98. /*
  99. * Возвращает время в милисекундах
  100. */
  101. public function milliseconds()
  102. {
  103. $mt = explode(' ', microtime());
  104. return ((int)$mt[1]) * 1000 + ((int)round($mt[0] * 1000));
  105. }
  106. }