* @version : HuoMp 1.0 */ namespace huoMpAd; use huo\controller\common\Base; use huoMpMsg\controller\Common; use huoMpMsg\status\OfficialAccountStatus; class MpAdApi extends Base { /** * 创建数据源 * * @param string $wx_app_id 第三方用户唯一凭证 * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret * @param array $data 请求参数 * * @return array */ public function createDataSource($wx_app_id, $wx_app_secret, $data) { $_url = 'https://api.weixin.qq.com/marketing/user_action_sets/add?version=v1.0'; $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret); $_url = $_url.'&access_token='.$_access_token; $_param = json_encode($data, JSON_UNESCAPED_UNICODE); $_header = ['Content-Type: application/json; charset=utf-8']; $_ret = Common::curl($_url, $_param, 'POST', $_header); $_rdata = []; if (Common::isJson($_ret)) { $_rdata = json_decode($_ret, true); } if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) { $_code = $_rdata['errcode']; return $this->huoError($_code, $_rdata['errmsg']); } // $_rdata['data'] = [ // 'user_action_set_id' => '9999999' // ]; $_code = OfficialAccountStatus::NO_ERROR; return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata['data']); } /** * 回传数据 * * @param string $wx_app_id 第三方用户唯一凭证 * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret * @param array $data 请求参数 * @param bool $read_cache * * @return array */ public function dataReport($wx_app_id, $wx_app_secret, $data, $read_cache = true) { $_url = 'https://api.weixin.qq.com/marketing/user_actions/add?version=v1.0'; $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret, $read_cache); $_url = $_url.'&access_token='.$_access_token; $_param = json_encode($data, JSON_UNESCAPED_UNICODE); $_header = ['Content-Type: application/json; charset=utf-8']; $_ret = Common::curl($_url, $_param, 'POST', $_header); $_rdata = []; if (Common::isJson($_ret)) { $_rdata = json_decode($_ret, true); } if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) { if ($_rdata['errcode'] == '40001' && $read_cache) { return $this->dataReport($wx_app_id, $wx_app_secret, $data,false); } $_code = $_rdata['errcode']; return $this->huoError($_code, $_rdata['errmsg']); } $_code = OfficialAccountStatus::NO_ERROR; return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code)); } /** * 数据源报表 * * @param string $wx_app_id 第三方用户唯一凭证 * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret * @param array $data 请求参数 * * @return array */ public function getDataSourceReport($wx_app_id, $wx_app_secret, $data) { $_url = 'https://api.weixin.qq.com/marketing/user_action_set_reports/get?version=v1.0'; $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret); $_url = $_url.'&access_token='.$_access_token.'&'.http_build_query($data); $_ret = Common::curl($_url, '', 'GET'); $_rdata = []; if (Common::isJson($_ret)) { $_rdata = json_decode($_ret, true); } if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) { $_code = $_rdata['errcode']; return $this->huoError($_code, $_rdata['errmsg']); } $_code = OfficialAccountStatus::NO_ERROR; return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata['data']); } /** * 数据源查询 * * @param string $wx_app_id 第三方用户唯一凭证 * @param string $wx_app_secret 第三方用户唯一凭证密钥,即appsecret * @param array $data * * @return array */ public function getDataSourceInfo($wx_app_id, $wx_app_secret, $data) { $_url = 'https://api.weixin.qq.com/marketing/user_action_sets/get?version=v1.0'; $_access_token = Common::getAccessToken($wx_app_id, $wx_app_secret); $_url = $_url.'&access_token='.$_access_token.'&'.http_build_query($data); $_ret = Common::curl($_url, '', 'GET'); $_rdata = []; if (Common::isJson($_ret)) { $_rdata = json_decode($_ret, true); } if (isset($_rdata['errcode']) && '0' != $_rdata['errcode']) { $_code = $_rdata['errcode']; return $this->huoError($_code, $_rdata['errmsg']); } $_code = OfficialAccountStatus::NO_ERROR; return $this->huoSuccess($_code, OfficialAccountStatus::getMsg($_code), $_rdata['data']); } }