1、新的需求为:需要实现微信渠道的文章的阅读数的显示。如图1
2、实则最为理想的情况,应该是能够基于文章 ID 获取此文章的阅读数,就像抖音。查看抖音开放平台文档 – 资源中心 – Open Api – 视频管理 – 抖音 – 查询视频 – 查询特定视频的视频数据。请求参数支持文章 ID。如图2
3、查看微信官方文档 – 公众号 – 数据统计 – 图文分析 – 接口调用请求说明。其不支持文章 ID,仅支持数据的日期范围。begin_date,获取数据的起始日期,begin_date和end_date的差值需小于“最大时间跨度”(比如最大时间跨度为1时,begin_date和end_date的差值只能为0,才能小于1),否则会报错。end_date,获取数据的结束日期,end_date允许设置的最大值为昨日。如图3
4、查看微信官方文档 – 公众号 – 数据统计 – 图文分析 – 返回参数说明。请注意:这里的msgid实际上是由msgid(图文消息id,这也就是群发接口调用后返回的msg_data_id)和index(消息次序索引)组成, 例如12003_3, 其中12003是msgid,即一次群发的消息的id; 3为index,假设该次群发的图文消息共5个文章(因为可能为多图文),3表示5个中的第3个。如图4
5、当微信文章群发成功后,将 消息发送任务的ID 与 消息的数据ID,存储至表中。其字段类型为:bigint。因为 int 的最大值为:2147483647。小于 msg_data_id 的值:2247483783。如图5
6、POST https://api.weixin.qq.com/datacube/getarticletotal?access_token={access_token} ,响应如下,如图6
{ "begin_date": "2020-11-13", "end_date": "2020-11-13" }
{ "list": [ { "ref_date": "2020-11-13", "msgid": "2247483782_1", "title": "33333r", "user_source": 0, "details": [ { "stat_date": "2020-11-13", "target_user": 356, "int_page_read_user": 51, "int_page_read_count": 68, "ori_page_read_user": 4, "ori_page_read_count": 6, "share_user": 1, "share_count": 1, "add_to_fav_user": 0, "add_to_fav_count": 0, "int_page_from_session_read_user": 51, "int_page_from_session_read_count": 68, "int_page_from_hist_msg_read_user": 0, "int_page_from_hist_msg_read_count": 0, "int_page_from_feed_read_user": 0, "int_page_from_feed_read_count": 0, "int_page_from_friends_read_user": 0, "int_page_from_friends_read_count": 0, "int_page_from_other_read_user": 0, "int_page_from_other_read_count": 0, "feed_share_from_session_user": 0, "feed_share_from_session_cnt": 0, "feed_share_from_feed_user": 0, "feed_share_from_feed_cnt": 0, "feed_share_from_other_user": 0, "feed_share_from_other_cnt": 0 }, { "stat_date": "2020-11-14", "target_user": 356, "int_page_read_user": 64, "int_page_read_count": 82, "ori_page_read_user": 5, "ori_page_read_count": 7, "share_user": 1, "share_count": 1, "add_to_fav_user": 0, "add_to_fav_count": 0, "int_page_from_session_read_user": 64, "int_page_from_session_read_count": 82, "int_page_from_hist_msg_read_user": 0, "int_page_from_hist_msg_read_count": 0, "int_page_from_feed_read_user": 0, "int_page_from_feed_read_count": 0, "int_page_from_friends_read_user": 0, "int_page_from_friends_read_count": 0, "int_page_from_other_read_user": 0, "int_page_from_other_read_count": 0, "feed_share_from_session_user": 0, "feed_share_from_session_cnt": 0, "feed_share_from_feed_user": 0, "feed_share_from_feed_cnt": 0, "feed_share_from_other_user": 0, "feed_share_from_other_cnt": 0 }, { "stat_date": "2020-11-15", "target_user": 356, "int_page_read_user": 64, "int_page_read_count": 84, "ori_page_read_user": 6, "ori_page_read_count": 8, "share_user": 1, "share_count": 1, "add_to_fav_user": 0, "add_to_fav_count": 0, "int_page_from_session_read_user": 64, "int_page_from_session_read_count": 84, "int_page_from_hist_msg_read_user": 0, "int_page_from_hist_msg_read_count": 0, "int_page_from_feed_read_user": 0, "int_page_from_feed_read_count": 0, "int_page_from_friends_read_user": 0, "int_page_from_friends_read_count": 0, "int_page_from_other_read_user": 0, "int_page_from_other_read_count": 0, "feed_share_from_session_user": 0, "feed_share_from_session_cnt": 0, "feed_share_from_feed_user": 0, "feed_share_from_feed_cnt": 0, "feed_share_from_other_user": 0, "feed_share_from_other_cnt": 0 }, { "stat_date": "2020-11-16", "target_user": 356, "int_page_read_user": 64, "int_page_read_count": 84, "ori_page_read_user": 6, "ori_page_read_count": 8, "share_user": 1, "share_count": 1, "add_to_fav_user": 0, "add_to_fav_count": 0, "int_page_from_session_read_user": 64, "int_page_from_session_read_count": 84, "int_page_from_hist_msg_read_user": 0, "int_page_from_hist_msg_read_count": 0, "int_page_from_feed_read_user": 0, "int_page_from_feed_read_count": 0, "int_page_from_friends_read_user": 0, "int_page_from_friends_read_count": 0, "int_page_from_other_read_user": 0, "int_page_from_other_read_count": 0, "feed_share_from_session_user": 0, "feed_share_from_session_cnt": 0, "feed_share_from_feed_user": 0, "feed_share_from_feed_cnt": 0, "feed_share_from_other_user": 0, "feed_share_from_other_cnt": 0 }, { "stat_date": "2020-11-17", "target_user": 356, "int_page_read_user": 65, "int_page_read_count": 86, "ori_page_read_user": 6, "ori_page_read_count": 8, "share_user": 1, "share_count": 1, "add_to_fav_user": 0, "add_to_fav_count": 0, "int_page_from_session_read_user": 65, "int_page_from_session_read_count": 86, "int_page_from_hist_msg_read_user": 0, "int_page_from_hist_msg_read_count": 0, "int_page_from_feed_read_user": 0, "int_page_from_feed_read_count": 0, "int_page_from_friends_read_user": 0, "int_page_from_friends_read_count": 0, "int_page_from_other_read_user": 0, "int_page_from_other_read_count": 0, "feed_share_from_session_user": 0, "feed_share_from_session_cnt": 0, "feed_share_from_feed_user": 0, "feed_share_from_feed_cnt": 0, "feed_share_from_other_user": 0, "feed_share_from_other_cnt": 0 } ], "url": "https://mp.weixin.qq.com/s?__biz=MzIxNjcyMzkzOQ==&mid=2247483782&idx=1&sn=06aa625b6f4b908a91776bf2e9a8c00e#rd" }, { "ref_date": "2020-11-13", "msgid": "2247483782_2", "title": "22222", "user_source": 0, "details": [ { "stat_date": "2020-11-13", "target_user": 356, "int_page_read_user": 21, "int_page_read_count": 25, "ori_page_read_user": 0, "ori_page_read_count": 0, "share_user": 0, "share_count": 0, "add_to_fav_user": 0, "add_to_fav_count": 0, "int_page_from_session_read_user": 21, "int_page_from_session_read_count": 25, "int_page_from_hist_msg_read_user": 0, "int_page_from_hist_msg_read_count": 0, "int_page_from_feed_read_user": 0, "int_page_from_feed_read_count": 0, "int_page_from_friends_read_user": 0, "int_page_from_friends_read_count": 0, "int_page_from_other_read_user": 0, "int_page_from_other_read_count": 0, "feed_share_from_session_user": 0, "feed_share_from_session_cnt": 0, "feed_share_from_feed_user": 0, "feed_share_from_feed_cnt": 0, "feed_share_from_other_user": 0, "feed_share_from_other_cnt": 0 }, { "stat_date": "2020-11-14", "target_user": 356, "int_page_read_user": 30, "int_page_read_count": 37, "ori_page_read_user": 0, "ori_page_read_count": 0, "share_user": 0, "share_count": 0, "add_to_fav_user": 0, "add_to_fav_count": 0, "int_page_from_session_read_user": 30, "int_page_from_session_read_count": 37, "int_page_from_hist_msg_read_user": 0, "int_page_from_hist_msg_read_count": 0, "int_page_from_feed_read_user": 0, "int_page_from_feed_read_count": 0, "int_page_from_friends_read_user": 0, "int_page_from_friends_read_count": 0, "int_page_from_other_read_user": 0, "int_page_from_other_read_count": 0, "feed_share_from_session_user": 0, "feed_share_from_session_cnt": 0, "feed_share_from_feed_user": 0, "feed_share_from_feed_cnt": 0, "feed_share_from_other_user": 0, "feed_share_from_other_cnt": 0 }, { "stat_date": "2020-11-15", "target_user": 356, "int_page_read_user": 31, "int_page_read_count": 39, "ori_page_read_user": 0, "ori_page_read_count": 0, "share_user": 0, "share_count": 0, "add_to_fav_user": 0, "add_to_fav_count": 0, "int_page_from_session_read_user": 31, "int_page_from_session_read_count": 39, "int_page_from_hist_msg_read_user": 0, "int_page_from_hist_msg_read_count": 0, "int_page_from_feed_read_user": 0, "int_page_from_feed_read_count": 0, "int_page_from_friends_read_user": 0, "int_page_from_friends_read_count": 0, "int_page_from_other_read_user": 0, "int_page_from_other_read_count": 0, "feed_share_from_session_user": 0, "feed_share_from_session_cnt": 0, "feed_share_from_feed_user": 0, "feed_share_from_feed_cnt": 0, "feed_share_from_other_user": 0, "feed_share_from_other_cnt": 0 }, { "stat_date": "2020-11-16", "target_user": 356, "int_page_read_user": 31, "int_page_read_count": 39, "ori_page_read_user": 0, "ori_page_read_count": 0, "share_user": 0, "share_count": 0, "add_to_fav_user": 0, "add_to_fav_count": 0, "int_page_from_session_read_user": 31, "int_page_from_session_read_count": 39, "int_page_from_hist_msg_read_user": 0, "int_page_from_hist_msg_read_count": 0, "int_page_from_feed_read_user": 0, "int_page_from_feed_read_count": 0, "int_page_from_friends_read_user": 0, "int_page_from_friends_read_count": 0, "int_page_from_other_read_user": 0, "int_page_from_other_read_count": 0, "feed_share_from_session_user": 0, "feed_share_from_session_cnt": 0, "feed_share_from_feed_user": 0, "feed_share_from_feed_cnt": 0, "feed_share_from_other_user": 0, "feed_share_from_other_cnt": 0 }, { "stat_date": "2020-11-17", "target_user": 356, "int_page_read_user": 32, "int_page_read_count": 40, "ori_page_read_user": 0, "ori_page_read_count": 0, "share_user": 0, "share_count": 0, "add_to_fav_user": 0, "add_to_fav_count": 0, "int_page_from_session_read_user": 32, "int_page_from_session_read_count": 40, "int_page_from_hist_msg_read_user": 0, "int_page_from_hist_msg_read_count": 0, "int_page_from_feed_read_user": 0, "int_page_from_feed_read_count": 0, "int_page_from_friends_read_user": 0, "int_page_from_friends_read_count": 0, "int_page_from_other_read_user": 0, "int_page_from_other_read_count": 0, "feed_share_from_session_user": 0, "feed_share_from_session_cnt": 0, "feed_share_from_feed_user": 0, "feed_share_from_feed_cnt": 0, "feed_share_from_other_user": 0, "feed_share_from_other_cnt": 0 } ], "url": "https://mp.weixin.qq.com/s?__biz=MzIxNjcyMzkzOQ==&mid=2247483782&idx=2&sn=ad6130affc4ebffff8d7a95d89f8b6c7#rd" }, { "ref_date": "2020-11-13", "msgid": "2247483783_1", "title": "快乐周末三号", "user_source": 0, "details": [ { "stat_date": "2020-11-13", "target_user": 356, "int_page_read_user": 98, "int_page_read_count": 121, "ori_page_read_user": 9, "ori_page_read_count": 11, "share_user": 0, "share_count": 0, "add_to_fav_user": 0, "add_to_fav_count": 0, "int_page_from_session_read_user": 97, "int_page_from_session_read_count": 120, "int_page_from_hist_msg_read_user": 0, "int_page_from_hist_msg_read_count": 0, "int_page_from_feed_read_user": 0, "int_page_from_feed_read_count": 0, "int_page_from_friends_read_user": 0, "int_page_from_friends_read_count": 0, "int_page_from_other_read_user": 1, "int_page_from_other_read_count": 1, "feed_share_from_session_user": 0, "feed_share_from_session_cnt": 0, "feed_share_from_feed_user": 0, "feed_share_from_feed_cnt": 0, "feed_share_from_other_user": 0, "feed_share_from_other_cnt": 0 }, { "stat_date": "2020-11-14", "target_user": 356, "int_page_read_user": 118, "int_page_read_count": 147, "ori_page_read_user": 10, "ori_page_read_count": 12, "share_user": 0, "share_count": 0, "add_to_fav_user": 0, "add_to_fav_count": 0, "int_page_from_session_read_user": 117, "int_page_from_session_read_count": 146, "int_page_from_hist_msg_read_user": 0, "int_page_from_hist_msg_read_count": 0, "int_page_from_feed_read_user": 0, "int_page_from_feed_read_count": 0, "int_page_from_friends_read_user": 0, "int_page_from_friends_read_count": 0, "int_page_from_other_read_user": 1, "int_page_from_other_read_count": 1, "feed_share_from_session_user": 0, "feed_share_from_session_cnt": 0, "feed_share_from_feed_user": 0, "feed_share_from_feed_cnt": 0, "feed_share_from_other_user": 0, "feed_share_from_other_cnt": 0 }, { "stat_date": "2020-11-15", "target_user": 356, "int_page_read_user": 119, "int_page_read_count": 150, "ori_page_read_user": 10, "ori_page_read_count": 12, "share_user": 0, "share_count": 0, "add_to_fav_user": 0, "add_to_fav_count": 0, "int_page_from_session_read_user": 118, "int_page_from_session_read_count": 149, "int_page_from_hist_msg_read_user": 0, "int_page_from_hist_msg_read_count": 0, "int_page_from_feed_read_user": 0, "int_page_from_feed_read_count": 0, "int_page_from_friends_read_user": 0, "int_page_from_friends_read_count": 0, "int_page_from_other_read_user": 1, "int_page_from_other_read_count": 1, "feed_share_from_session_user": 0, "feed_share_from_session_cnt": 0, "feed_share_from_feed_user": 0, "feed_share_from_feed_cnt": 0, "feed_share_from_other_user": 0, "feed_share_from_other_cnt": 0 }, { "stat_date": "2020-11-16", "target_user": 356, "int_page_read_user": 119, "int_page_read_count": 150, "ori_page_read_user": 10, "ori_page_read_count": 12, "share_user": 0, "share_count": 0, "add_to_fav_user": 0, "add_to_fav_count": 0, "int_page_from_session_read_user": 118, "int_page_from_session_read_count": 149, "int_page_from_hist_msg_read_user": 0, "int_page_from_hist_msg_read_count": 0, "int_page_from_feed_read_user": 0, "int_page_from_feed_read_count": 0, "int_page_from_friends_read_user": 0, "int_page_from_friends_read_count": 0, "int_page_from_other_read_user": 1, "int_page_from_other_read_count": 1, "feed_share_from_session_user": 0, "feed_share_from_session_cnt": 0, "feed_share_from_feed_user": 0, "feed_share_from_feed_cnt": 0, "feed_share_from_other_user": 0, "feed_share_from_other_cnt": 0 }, { "stat_date": "2020-11-17", "target_user": 356, "int_page_read_user": 120, "int_page_read_count": 151, "ori_page_read_user": 10, "ori_page_read_count": 12, "share_user": 0, "share_count": 0, "add_to_fav_user": 0, "add_to_fav_count": 0, "int_page_from_session_read_user": 119, "int_page_from_session_read_count": 150, "int_page_from_hist_msg_read_user": 0, "int_page_from_hist_msg_read_count": 0, "int_page_from_feed_read_user": 0, "int_page_from_feed_read_count": 0, "int_page_from_friends_read_user": 0, "int_page_from_friends_read_count": 0, "int_page_from_other_read_user": 1, "int_page_from_other_read_count": 1, "feed_share_from_session_user": 0, "feed_share_from_session_cnt": 0, "feed_share_from_feed_user": 0, "feed_share_from_feed_cnt": 0, "feed_share_from_other_user": 0, "feed_share_from_other_cnt": 0 } ], "url": "https://mp.weixin.qq.com/s?__biz=MzIxNjcyMzkzOQ==&mid=2247483783&idx=1&sn=4946a8fb35caa4c29aa2232eb51f8bb1#rd" } ] }
7、获取 微信公众帐号的应用的任务 的第一篇文章的统计数据。最终能够尽量模拟出类似抖音的效果(虽然不能够获取到最新的阅读数,只能够获取到发布后最多7天后的阅读数)。最大时间跨度是指一次接口调用时最大可获取数据的时间范围,如最大时间跨度为7是指最多一次性获取7天的数据。基于文章 ID 获取其阅读数。存在的问题,可能在于,日期范围不够准确,因为其取值为这条记录的创建时间,应该取值发布至微信平台的时间。后续优化。代码如下
<?php namespace console/services; use Yii; use yii/helpers/ArrayHelper; use yii/web/ServerErrorHttpException; class WxAppTaskService extends /common/services/WxAppTaskService { /** * 获取 微信公众帐号的应用的任务 的第一篇文章的统计数据 * @param object $wxAppTask 微信公众帐号的应用的任务 * @param string $accessToken 访问令牌 * * @return array * 格式如下: * * [ // 最后一个统计日期 * 'stat_date': '2020-05-29', // 统计的日期,在 getarticletotal 接口中,ref_date 指的是文章群发出日期, 而 stat_date 是数据统计日期 * 'target_user': 387, // 送达人数,一般约等于总粉丝数(需排除黑名单或其他异常情况下无法收到消息的粉丝) * 'int_page_read_user': 86, // 图文页(点击群发图文卡片进入的页面)的阅读人数 * 'int_page_read_count': 111, // 图文页的阅读次数 * 'ori_page_read_user': 11, // 原文页(点击图文页“阅读原文”进入的页面)的阅读人数,无原文页时此处数据为0 * 'ori_page_read_count': 12, // 原文页的阅读次数 * 'share_user': 1, // 分享的人数 * 'share_count': 1, // 分享的次数 * 'add_to_fav_user': 0, // 收藏的人数 * 'add_to_fav_count': 0, // 收藏的次数 * 'int_page_from_session_read_user': 85, // 公众号会话阅读人数 * 'int_page_from_session_read_count': 109, // 公众号会话阅读次数 * 'int_page_from_hist_msg_read_user': 0, // 历史消息页阅读人数 * 'int_page_from_hist_msg_read_count': 0, // 历史消息页阅读次数 * 'int_page_from_feed_read_user': 0, // 朋友圈阅读人数 * 'int_page_from_feed_read_count': 0, // 朋友圈阅读次数 * 'int_page_from_friends_read_user': 0, // 好友转发阅读人数 * 'int_page_from_friends_read_count': 0, // 好友转发阅读次数 * 'int_page_from_other_read_user': 1, // 其他场景阅读人数 * 'int_page_from_other_read_count': 1, // 其他场景阅读次数 * 'feed_share_from_session_user': 0, // 公众号会话转发朋友圈人数 * 'feed_share_from_session_cnt': 0, // 公众号会话转发朋友圈次数 * 'feed_share_from_feed_user': 0, // 朋友圈转发朋友圈人数 * 'feed_share_from_feed_cnt': 0, // 朋友圈转发朋友圈次数 * 'feed_share_from_other_user': 0, // 其他场景转发朋友圈人数 * 'feed_share_from_other_cnt': 0, // 其他场景转发朋友圈次数 * ] * * @throws ServerErrorHttpException */ public static function getCounts($wxAppTask, $accessToken) { $date = Date('Y-m-d', $wxAppTask->created_at); // HTTP请求,获取图文群发总数据 $httpGetArticleTotalData = [ 'accessToken' => $accessToken, 'beginDate' => $date, 'endDate' => $date, ]; $articleTotal = WxDatacubeService::httpGetArticleTotal($httpGetArticleTotalData); // 重建数组索引,键名:msgid $list = ArrayHelper::index($articleTotal['list'], 'msgid'); $msgid = $wxAppTask->msg_data_id . '_1'; $counts = []; if (isset($list[$msgid])) { // 获取一个数组的最后一个键值 $lastKey = array_key_last($list[$msgid]['details']); $counts = $list[$msgid]['details'][$lastKey]; } return $counts; } }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/250653.html