BaseAI.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. private $header = array();
  11. public $config = array();
  12. function __construct()
  13. {
  14. $this->config = [];
  15. }
  16. function SetHeader( $header = null ){
  17. if( !is_array( $header ) ){
  18. $header = array(
  19. 'header'=>"Accept-language: ru\r\n" .
  20. 'Content-Type: application/json; charset=utf-8;'."\r\n"
  21. );
  22. }
  23. $this->header = $header;
  24. }
  25. function Send( $url, $cmd ){
  26. $json = json_encode($cmd);
  27. $curl_handle=curl_init();
  28. curl_setopt($curl_handle, CURLOPT_URL, $url);
  29. curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "POST");
  30. curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 20);
  31. curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
  32. curl_setopt($curl_handle, CURLOPT_USERAGENT, 'amicru');
  33. curl_setopt($curl_handle, CURLOPT_SSL_VERIFYPEER, false );
  34. curl_setopt($curl_handle, CURLOPT_SSL_VERIFYHOST, false);
  35. curl_setopt($curl_handle, CURLOPT_HTTPHEADER, $this->header);
  36. curl_setopt($curl_handle, CURLOPT_POST, 1);
  37. curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $json);
  38. $query = curl_exec($curl_handle);
  39. // echo curl_error($curl_handle);
  40. // print_a(curl_getinfo($curl_handle));
  41. curl_close($curl_handle);
  42. return $query;
  43. }
  44. }