File.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. require_once('function.php');
  5. class File extends Controller{
  6. public $accessToken ="";
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. $this->accessToken =get_access_token();
  11. }
  12. // 上传临时素材
  13. public function lsupload()
  14. {
  15. $url="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$this->accessToken."&type=image";
  16. $file =dirname(THINK_PATH).'/1.jpg';
  17. $data = array(
  18. "media"=>new \CurlFile($file)
  19. );
  20. $res = http_curl($url,$data,'post');
  21. dump($res);
  22. }
  23. // 添加图文素材
  24. public function addNews()
  25. {
  26. $url = 'https://api.weixin.qq.com/cgi-bin/material/add_news?access_token='.$this->accessToken;
  27. $data['articles'] = db('newsMedia')->select();
  28. $list = json_encode($data,JSON_UNESCAPED_UNICODE);
  29. echo $list;
  30. $res = http_curl($url,$list,'post');
  31. dump($res);
  32. }
  33. // 手动缩略图的上传
  34. public function addThumb()
  35. {
  36. $url = 'https://api.weixin.qq.com/cgi-bin/material/add_material?access_token='.$this->accessToken.'&type=image';
  37. $file = dirname(THINK_PATH).'/1.jpg';
  38. $data = array(
  39. 'media'=>new \CurlFile($file)
  40. );
  41. $res = http_curl($url,$data,'post');
  42. dump($res);
  43. }
  44. // 创建标签
  45. public function createTag()
  46. {
  47. $url = 'https://api.weixin.qq.com/cgi-bin/tags/create?access_token='.$this->accessToken;
  48. $data = '{"tag" : {"name" : "中国好声音"}}';
  49. $res = http_curl($url,$data,'post');
  50. dump($res);
  51. }
  52. // 查看已有标签
  53. public function getTag()
  54. {
  55. $url = 'https://api.weixin.qq.com/cgi-bin/tags/get?access_token='.$this->accessToken;
  56. $res = http_curl($url);
  57. dump($res);
  58. }
  59. //设置标签
  60. public function setTag()
  61. {
  62. $url = 'https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token='.$this->accessToken;
  63. $data = '{"openid_list" : ["oOnXGwpc7_8My4YLpDkTnbSFdrak","oOnXGwsELMp54MG3Vl0abY4aLluk" ], "tagid" : 100 }';
  64. $res = http_curl($url,$data,'post');
  65. dump($res);
  66. }
  67. // 按照标签给用户群发消息
  68. public function send()
  69. {
  70. $url = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token='.$this->accessToken;
  71. // 根据要发送的消息的类型判断拼接不同的json格式数据
  72. $data='{
  73. "filter":{
  74. "is_to_all":false,
  75. "tag_id":100
  76. },
  77. "text":{
  78. "content":"测试按照标签,群发消息"
  79. },
  80. "msgtype":"text"
  81. }';
  82. $res = http_curl($url,$data,'post');
  83. dump($res);
  84. }
  85. }