BaseAI.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /*
  3. test yandex api
  4. */
  5. namespace app\models\base;
  6. use yii\base\Component;
  7. use Yii;
  8. class BaseAI extends Component
  9. {
  10. public $header = array();
  11. public $config = array();
  12. public $time = array();
  13. function __construct()
  14. {
  15. $this->config = [];
  16. }
  17. public function SetHeader( $header = null ){
  18. if( !is_array( $header ) ){
  19. $header = array(
  20. 'header'=>"Accept-language: ru\r\n" .
  21. 'Content-Type: application/json; charset=utf-8;'."\r\n"
  22. );
  23. }
  24. $this->header = $header;
  25. }
  26. function Send( $url, $cmd ){
  27. $json = json_encode($cmd);
  28. $curl_handle=curl_init();
  29. curl_setopt($curl_handle, CURLOPT_URL, $url);
  30. curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "POST");
  31. curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
  32. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
  33. curl_setopt($curl_handle, CURLOPT_USERAGENT, 'amicru');
  34. curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false );
  35. curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false);
  36. curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $this->header);
  37. curl_setopt($curl_handle, CURLOPT_POST, 1);
  38. curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $json);
  39. curl_setopt($curl_handle, CURLOPT_VERBOSE, true);
  40. curl_setopt($curl_handle, CURLINFO_HEADER_OUT, true);
  41. $query = curl_exec($curl_handle);
  42. // echo curl_error($curl_handle);
  43. // print_a(curl_getinfo($curl_handle));
  44. $info = curl_getinfo($curl_handle);
  45. $this->time = $info['total_time'];
  46. curl_close($curl_handle);
  47. return $query;
  48. }
  49. function GetTime(){
  50. return $this->time;
  51. }
  52. }