MediaApi.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * MediaApi.php UTF-8
  4. * 素材接口
  5. *
  6. * @date : 2018/9/25 18:27
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huoMpMsg\controller;
  13. use huo\controller\common\Base;
  14. use huoMpMsg\status\OfficialAccountStatus;
  15. class MediaApi extends Base {
  16. const API_URL_PREFIX = 'https://api.weixin.qq.com/cgi-bin';
  17. const MEDIA_FOREVER_UPLOAD_URL = '/material/add_material?';
  18. const MEDIA_FOREVER_NEWS_UPLOAD_URL = '/material/add_news?';
  19. const MEDIA_FOREVER_NEWS_UPDATE_URL = '/material/update_news?';
  20. const MEDIA_FOREVER_GET_URL = '/material/get_material?';
  21. const MEDIA_FOREVER_DEL_URL = '/material/del_material?';
  22. const MEDIA_FOREVER_COUNT_URL = '/material/get_materialcount?';
  23. const MEDIA_FOREVER_BATCHGET_URL = '/material/batchget_material?';
  24. /**
  25. * 获取永久素材列表
  26. *
  27. * @param string $wx_app_id 第三方用户唯一凭证
  28. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  29. * @param string $type 类型有image,vedio和audio
  30. * @param int $offset 起始位置,0表示从第一个
  31. * @param int $count 个数,区间为0~20
  32. *
  33. * @return array
  34. */
  35. public function getForeverList($wx_app_id, $wx_app_secret, $type, $offset, $count) {
  36. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  37. $_url = self::API_URL_PREFIX.self::MEDIA_FOREVER_BATCHGET_URL.'access_token='.$_access_token;
  38. $_param = array(
  39. 'type' => $type,
  40. 'offset' => $offset,
  41. 'count' => $count,
  42. );
  43. $_param_json = json_encode($_param);
  44. $_header = ['Content-Type: application/octet-stream; charset=utf-8'];
  45. $_ret = Common::curl($_url, $_param_json, 'POST', $_header);
  46. $_rdata = [];
  47. if (Common::isJson($_ret)) {
  48. $_rdata = json_decode($_ret, true);
  49. }
  50. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  51. $_code = $_rdata['errcode'];
  52. return $this->huoError($_code, $_rdata['errmsg']);
  53. }
  54. $_code = OfficialAccountStatus::NO_ERROR;
  55. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata);
  56. }
  57. /**
  58. * 上传永久图文素材(认证后的订阅号可用)
  59. *
  60. * @param string $wx_app_id 第三方用户唯一凭证
  61. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  62. * @param string $data a 消息结构{"articles":[{...}]}
  63. *
  64. * @return array
  65. */
  66. public function uploadForeverArticles($wx_app_id, $wx_app_secret, $data) {
  67. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  68. $_url = self::API_URL_PREFIX.self::MEDIA_FOREVER_NEWS_UPLOAD_URL.'access_token='.$_access_token;
  69. $_param = $data;
  70. $_param_json = json_encode($_param);
  71. $_header = ['Content-Type: application/octet-stream; charset=utf-8'];
  72. $_ret = Common::curl($_url, $_param_json, 'POST', $_header);
  73. $_rdata = [];
  74. if (Common::isJson($_ret)) {
  75. $_rdata = json_decode($_ret, true);
  76. }
  77. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  78. $_code = $_rdata['errcode'];
  79. return $this->huoError($_code, $_rdata['errmsg']);
  80. }
  81. $_code = OfficialAccountStatus::NO_ERROR;
  82. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata);
  83. }
  84. /**
  85. * 发送模板消息
  86. * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277
  87. *
  88. * @param string $wx_app_id 第三方用户唯一凭证
  89. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  90. * @param string $file_path 图片绝对路径
  91. *
  92. * @return array
  93. */
  94. public function imageUpload($wx_app_id, $wx_app_secret, $file_path) {
  95. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  96. $_url = self::API_URL_PREFIX.self::MEDIA_FOREVER_UPLOAD_URL;
  97. $_type = "image";
  98. $_url = $_url.'access_token='.$_access_token.'&type='.$_type;
  99. //这里声明文件的路径,使用绝对路径
  100. $_file_data = array("media" => new \CURLFile($file_path));
  101. $ch = curl_init();
  102. curl_setopt($ch, CURLOPT_URL, $_url);
  103. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  104. curl_setopt($ch, CURLOPT_POST, 1);
  105. curl_setopt($ch, CURLOPT_POSTFIELDS, $_file_data);
  106. $_ret = curl_exec($ch);//发送请求获取结果
  107. curl_close($ch);//关闭会话
  108. $_rdata = [];
  109. if (Common::isJson($_ret)) {
  110. $_rdata = json_decode($_ret, true);
  111. }
  112. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  113. $_code = $_rdata['errcode'];
  114. return $this->huoError($_code, $_rdata['errmsg']);
  115. }
  116. $_code = OfficialAccountStatus::NO_ERROR;
  117. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata);
  118. }
  119. }