MpAdApi.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * MpAdApi.php UTF-8
  4. * 广告行为数据上报接口
  5. *
  6. * @date : 2018/11/16 11:08
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : chenbingling <cbl@huosdk.com>
  10. * @version : HuoMp 1.0
  11. */
  12. namespace huoMpAd;
  13. use huo\controller\common\Base;
  14. use huoMpMsg\controller\Common;
  15. use huoMpMsg\status\OfficialAccountStatus;
  16. class MpAdApi extends Base {
  17. /**
  18. * 创建数据源
  19. *
  20. * @param string $wx_app_id 第三方用户唯一凭证
  21. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  22. * @param array $data 请求参数
  23. *
  24. * @return array
  25. */
  26. public function createDataSource($wx_app_id, $wx_app_secret, $data) {
  27. $_url = 'https://api.weixin.qq.com/marketing/user_action_sets/add?version=v1.0';
  28. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  29. $_url = $_url.'&access_token='.$_access_token;
  30. $_param = json_encode($data, JSON_UNESCAPED_UNICODE);
  31. $_header = ['Content-Type: application/json; charset=utf-8'];
  32. $_ret = Common::curl($_url, $_param, 'POST', $_header);
  33. $_rdata = [];
  34. if (Common::isJson($_ret)) {
  35. $_rdata = json_decode($_ret, true);
  36. }
  37. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  38. $_code = $_rdata['errcode'];
  39. return $this->huoError($_code, $_rdata['errmsg']);
  40. }
  41. // $_rdata['data'] = [
  42. // 'user_action_set_id' => '9999999'
  43. // ];
  44. $_code = OfficialAccountStatus::NO_ERROR;
  45. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata['data']);
  46. }
  47. /**
  48. * 回传数据
  49. *
  50. * @param string $wx_app_id 第三方用户唯一凭证
  51. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  52. * @param array $data 请求参数
  53. * @param bool $read_cache
  54. *
  55. * @return array
  56. */
  57. public function dataReport($wx_app_id, $wx_app_secret, $data, $read_cache = true) {
  58. $_url = 'https://api.weixin.qq.com/marketing/user_actions/add?version=v1.0';
  59. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret, $read_cache);
  60. $_url = $_url.'&access_token='.$_access_token;
  61. $_param = json_encode($data, JSON_UNESCAPED_UNICODE);
  62. $_header = ['Content-Type: application/json; charset=utf-8'];
  63. $_ret = Common::curl($_url, $_param, 'POST', $_header);
  64. $_rdata = [];
  65. if (Common::isJson($_ret)) {
  66. $_rdata = json_decode($_ret, true);
  67. }
  68. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  69. if ($_rdata['errcode'] == '40001' && $read_cache) {
  70. return $this->dataReport($wx_app_id, $wx_app_secret, $data,false);
  71. }
  72. $_code = $_rdata['errcode'];
  73. return $this->huoError($_code, $_rdata['errmsg']);
  74. }
  75. $_code = OfficialAccountStatus::NO_ERROR;
  76. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code));
  77. }
  78. /**
  79. * 数据源报表
  80. *
  81. * @param string $wx_app_id 第三方用户唯一凭证
  82. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  83. * @param array $data 请求参数
  84. *
  85. * @return array
  86. */
  87. public function getDataSourceReport($wx_app_id, $wx_app_secret, $data) {
  88. $_url = 'https://api.weixin.qq.com/marketing/user_action_set_reports/get?version=v1.0';
  89. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  90. $_url = $_url.'&access_token='.$_access_token.'&'.http_build_query($data);
  91. $_ret = Common::curl($_url, '', 'GET');
  92. $_rdata = [];
  93. if (Common::isJson($_ret)) {
  94. $_rdata = json_decode($_ret, true);
  95. }
  96. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  97. $_code = $_rdata['errcode'];
  98. return $this->huoError($_code, $_rdata['errmsg']);
  99. }
  100. $_code = OfficialAccountStatus::NO_ERROR;
  101. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata['data']);
  102. }
  103. /**
  104. * 数据源查询
  105. *
  106. * @param string $wx_app_id 第三方用户唯一凭证
  107. * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret
  108. * @param array $data
  109. *
  110. * @return array
  111. */
  112. public function getDataSourceInfo($wx_app_id, $wx_app_secret, $data) {
  113. $_url = 'https://api.weixin.qq.com/marketing/user_action_sets/get?version=v1.0';
  114. $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret);
  115. $_url = $_url.'&access_token='.$_access_token.'&'.http_build_query($data);
  116. $_ret = Common::curl($_url, '', 'GET');
  117. $_rdata = [];
  118. if (Common::isJson($_ret)) {
  119. $_rdata = json_decode($_ret, true);
  120. }
  121. if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) {
  122. $_code = $_rdata['errcode'];
  123. return $this->huoError($_code, $_rdata['errmsg']);
  124. }
  125. $_code = OfficialAccountStatus::NO_ERROR;
  126. return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata['data']);
  127. }
  128. }