PushWarnController.class.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class PushwarnController extends Controller {
  5. //public function s_curl($url){
  6. // $ch = curl_init();
  7. // curl_setopt ($ch, CURLOPT_URL, $url);
  8. // curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  9. // curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5);
  10. // $file_contents = curl_exec($ch);
  11. // curl_close($ch);
  12. // $arr = json_decode($file_contents,true);
  13. // return $arr;
  14. // }
  15. public function curl($url,$data=''){
  16. $ch = curl_init();
  17. curl_setopt($ch, CURLOPT_URL, $url);
  18. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  19. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  20. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  21. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  22. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  23. curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  24. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  25. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  26. $info = curl_exec($ch);
  27. if (curl_errno($ch)) {return 'Errno'.curl_error($ch);}else{ return $info;}
  28. curl_close($ch);
  29. }
  30. /**
  31. * [array_column1 ] 多维数组改成关联数组转字符串,
  32. * @param $rows [description]必需。多维数组。
  33. * @param [type] $column_key [description]必需。可以是索引数组的列的整数索引,或者是关联数组的列的字符串键值。
  34. * @param [type] $index_key [description]可选。用作返回数组的索引/键的列。
  35. * @return [type] [description](两个参数)返回用逗号分割的字符串,(三个参数)返回以第三个参数为列的索引数组
  36. */
  37. public function array_column1($rows, $column_key, $index_key = null) {
  38. $data = array();
  39. if (empty($index_key)) {
  40. foreach ($rows as $row) {
  41. $data[] = $row[$column_key];
  42. }
  43. $data=implode(",",$data);
  44. } else {
  45. foreach ($rows as $row) {
  46. $data[$row[$index_key]] = $row[$column_key];
  47. }
  48. }
  49. return $data;
  50. }
  51. public function SendDeviceAlarm($access_token,$openid,$address,$deviceid,$type,$time,$remark,$url){
  52. $data = array(
  53. "touser"=> $openid,
  54. "template_id"=>"9cYn9IdoLE7NhCAPTIc1Ip4dV5JPrbbATxQT-v944Vw",
  55. "url" =>$url,
  56. //"url" =>"http://zt-iot.com/ztxf/weixin.php/Home/Fire/water",
  57. "data"=> array(
  58. "first"=> array(
  59. "value"=> $address,
  60. "color"=>"#44b549"
  61. ),
  62. "keyword1"=>array(
  63. "value"=> $deviceid,
  64. "color"=>"#173177"
  65. ),
  66. "keyword2"=> array(
  67. "value"=> $type,
  68. "color"=>"#173177"
  69. ),
  70. "keyword3"=> array(
  71. "value"=> $time,
  72. "color"=>"#173177"
  73. ),
  74. "remark"=>array(
  75. "value"=> $remark,
  76. "color"=>"#aaaaaa"
  77. )
  78. )
  79. );
  80. //{{first.DATA}}
  81. //设备号:{{keyword1.DATA}}
  82. //报警类型:{{keyword2.DATA}}
  83. //时间:{{keyword3.DATA}}
  84. //{{remark.DATA}}
  85. $data = json_encode($data);
  86. $QUEST_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
  87. $res = $this->curl($QUEST_URL,$data);
  88. return json_decode($res,true);
  89. }
  90. public function printing_log($Data,$filename_log){
  91. $log = "[时间" . date("Y-m-d H:i:s") . "]报警数据:" . json_encode($Data) . "\r\n";
  92. file_put_contents($filename_log, $log, FILE_APPEND | LOCK_EX);
  93. }
  94. public function SendDeviceAlarm3($access_token,$openid,$address,$deviceid,$type,$time,$remark,$url){
  95. $data = array(
  96. "touser"=> $openid,
  97. "template_id"=>"kMIOm99qgqAQ-nqsSxWlUToz4Krm-_TopRBJolXNBB8",
  98. "url" =>$url,
  99. //"url" =>"http://zt-iot.com/ztxf/weixin.php/Home/Fire/water",
  100. "data"=> array(
  101. "first"=> array(
  102. "value"=> $address,
  103. "color"=>"#44b549"
  104. ),
  105. "keyword1"=>array(
  106. "value"=> $deviceid,
  107. "color"=>"#173177"
  108. ),
  109. "keyword2"=> array(
  110. "value"=> $type,
  111. "color"=>"#173177"
  112. ),
  113. "keyword3"=> array(
  114. "value"=> $time,
  115. "color"=>"#173177"
  116. ),
  117. "remark"=>array(
  118. "value"=> $remark,
  119. "color"=>"#aaaaaa"
  120. )
  121. )
  122. );
  123. //{{first.DATA}}
  124. //设备号:{{keyword1.DATA}}
  125. //报警类型:{{keyword2.DATA}}
  126. //时间:{{keyword3.DATA}}
  127. //{{remark.DATA}}
  128. $data = json_encode($data);
  129. $QUEST_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
  130. $res = $this->curl($QUEST_URL,$data);
  131. return json_decode($res,true);
  132. }
  133. }