function init() { $url = wxCreateBottommenuUrl() . WxUtil::getWxTokenFromDB(); $menustr = '{ "button": [ { "type": "view", "name": "资讯", "url": "http://bangmaisiw.w107.mc-test.com/wap/News.php" }, { "name": "互动", "sub_button": [ { "type": "view", "name": "来稿说明", "url": "http://bangmaisiw.w107.mc-test.com/wap/Engage.php" }, { "type": "click", "name": "进入聊天室", "key": "enterChatingroom" }, { "type": "click", "name": "退出聊天室", "key": "exitChatingroom" } ] }, { "name": "服务", "sub_button": [ { "type": "view", "name": "学习主题", "url": "http://bangmaisiw.w107.mc-test.com/wap/Theme.php" }, { "type": "view", "name": "公开研讨课", "url": "http://bangmaisiw.w107.mc-test.com/wap/Course.php" }, { "type": "view", "name": "定制服务", "url": "http://bangmaisiw.w107.mc-test.com/wap/Ordering.php" }, { "type": "view", "name": "网页聊天室", "url": "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx6f9dc2bfd436b651&redirect_uri=http%3A%2F%2Fbangmaisiw.w107.mc-test.com%2Fwap%2Fchatingroom%2Fwxoauth.php&response_type=code&scope=snsapi_base&state=callback#wechat_redirect" } ] } ] }'; $respArr = RequestUtil::httpPost($url, $menustr, 'post'); if ($respArr["errcode"] == 0) { return "菜单初始化成功,将在24小时内生效。"; } return $respArr["errcode"] . ":" . $respArr["errmsg"]; }
/** * 根据图片素材的id,拼装多图文素材 * $mediaid * $title 图文消息的标题 不可空 $sourceurl 阅读原文的链接 可空 $content 图文 的 文, 不可空 $digest 图片消息的描述, 可空 $showcoverpic 是否显示封面,1为显示,0为不显示 * */ public static function uploadmultipicmsg($articles) { $url = wxUploadPicMsg() . WxUtil::getWxTokenFromDB(); //$articles[] = $article; $dataarr = array("articles" => $articles); $data = JsonUtil::getJsonStrFromArray($dataarr); //echo $data; $response = RequestUtil::httpPost($url, $data, 'post'); return $response; }
function init() { $url = wxCreateBottommenuUrl() . WxUtil::getWxTokenFromDB(); $menustr = '{ "button": [ { "type": "view", "name": "资讯", "url": "http://demo.alsovalue.com/BONZWISE/News-List.html" }, { "name": "互动", "sub_button": [ { "type": "click", "name": "问答", "key": "askAndAnwser" }, { "type": "view", "name": "来稿说明", "url": "demo.alsovalue.com/BONZWISE/Draft-Description.html" } { "type": "click", "name": "进入聊天室", "key": "enterChatingroom" }, { "type": "click", "name": "退出聊天室", "key": "exitChatingroom" } ] }, { "name": "服务", "sub_button": [ { "type": "view", "name": "学习主题", "url": "demo.alsovalue.com/BONZWISE/LearningTopics.html" }, { "type": "view", "name": "公开研讨课", "url": "demo.alsovalue.com/BONZWISE/PublicationSeminar.html" }, { "type": "view", "name": "定制服务", "url": "demo.alsovalue.com/BONZWISE/CustomizedService.html" } ] } ] }'; $respArr = RequestUtil::httpPost($url, $menustr, 'post'); echo $respArr["errmsg"]; }
function initUser() { $remoteTemp = WxUtil::getUserList(); //echo print_r($remoteTemp); if (!empty($remoteTemp)) { $remoteUsers = $remoteTemp["data"]; if (!empty($remoteUsers)) { global $db; $res = $db->query("SELECT openid FROM wx_user_info "); $rowList = $db->fetch_all($res); $user_info_list = array(); // 每次最多查询100个用户 $itemp = 0; $remoteOpenidArray = $remoteUsers["openid"]; $openidArray = array(); // 先过滤一遍 foreach ($remoteOpenidArray as $openid) { if (!in_array(array("openid" => $openid), $rowList)) { // 只取openid不在数据库中的 $openidArray[] = $openid; } } LogUtil::logs("微信总用户数: ====> " . count($openidArray), getLogFile("/business.log")); $saveTimes = 1; foreach ($openidArray as $openid) { if (!in_array(array("openid" => $openid), $rowList)) { //echo $openid." is not in<br />"; // 组装一个信息 if ($itemp % 100 == 0) { $user_to_query = array(); } $toqueryuser = array("openid" => $openid, "lang" => "zh-CN"); $user_to_query[] = $toqueryuser; //LogUtil::logs("itemp====> ".$itemp, getLogFile("/business.log")); if (count($user_to_query) == 100 || $itemp == count($openidArray) - 1) { // 每满100条,查询一次 LogUtil::logs("====> 第" . $saveTimes . "次发起微信查询", getLogFile("/business.log")); $pdata = JsonUtil::getJsonStrFromArray(array("user_list" => $user_to_query)); $batchUserInfo = WxUtil::getBatchUserInfo($pdata); if (empty($batchUserInfo)) { // 先检查是否存在特殊字符 } if (!empty($batchUserInfo)) { $user_info_list = $batchUserInfo["user_info_list"]; transactionSave($user_info_list); $countres = $db->query("SELECT count(1) as totalcount from wx_user_info a"); $countObj = $db->fetch($countres); $nums = $countObj["totalcount"]; LogUtil::logs("已保存用户数 ====> " . $nums, getLogFile("/business.log")); } else { LogUtil::logs("====> 第" . $saveTimes . "次发起微信查询出错", getLogFile("/business.log")); LogUtil::logs("查询的用户openid请求参数====> " . $pdata, getLogFile("/business.log")); LogUtil::logs("查询的用户openid结果====>" . JsonUtil::getJsonStrFromArray($batchUserInfo), getLogFile("/business.log")); } $saveTimes++; } $itemp++; } } return "同步完成"; } } }
/** * 调用客服接口接口,发送多条图文消息,服务号不限制次数,但是要限制48小时内与微信公众号有互动的用户才能收到 */ function sendmultinewmsgByService() { // 当前php文件所在目录 //define('DIR_ROOT', str_replace('\\', '/', dirname(__FILE__))); // 项目根目录 define('DIR_ROOT', $_SERVER['DOCUMENT_ROOT']); //define('DIR_ROOT', "/wwwroot/"); $courseids = $_POST["courseids"]; /*$inTemp = ""; foreach($courseids as $courseid){ $inTemp = $inTemp.$courseid.","; } $in = substr($inTemp, 0, strlen($inTemp) -1); */ // 根据courseid查询对应的课程(推送)信息 $querysql = "select * from cn_news where id in (" . $courseids . ")"; global $db; global $smarty; $res = $db->query($querysql); $rowlist = $db->fetch_all($res); // 先判断下是否存在不满足条件的数据 $errortitle = ""; foreach ($rowlist as $row) { if (empty($row["simage"])) { $errortitle .= "<font color='red'>\"" . $row["id"] . "\"</font><br />"; } } if (!empty($errortitle)) { echo "编号为:<br />" . $errortitle . "的信息不完善,请重新编辑,添加图片、缩略图信息"; return; } // TODO 先保存图片素材,得到media_id, 页面提示用户,图片素材数量上限 // 图片(image)、语音(voice)、视频(video)和缩略图(thumb) $meidaType = "image"; // // 循环所有的消息,制成成图片消息,拿到media id 数组 $articles = array(); $showcoverpic = "0"; // 是否显示封面,1为显示,0为不显示 foreach ($rowlist as $row) { //获取当前文件物理路径 $tmp_file = DIR_ROOT . "/" . $row["simage"]; //echo $tmp_file; if (file_exists($tmp_file)) { // 先循环制作所有的图片素材, 调素材管理接口上传图片,得到meidaid $response = WxUtil::uploadPicGetPicUrl($meidaType, $tmp_file); //echo print_r($response); if (!empty($response["url"])) { $picurl = $response["url"]; $title = $row["title"]; $sourceurl = "http://bangmaisiw.w107.mc-test.com/wap/Course-Details.php?id=" . $row["id"]; $content = $row["contents"]; $article = array("title" => $title, "description" => "", "url" => $sourceurl, "picurl" => $picurl); $articles[] = $article; } else { LogUtil::logs("批量创建图片素材失败:消息来源id:" . $row["id"] . ",错误信息:" . $response["errmsg"], getLogFile("/business.log")); } } else { LogUtil::logs("文件不存在:" . $row["image"], getLogFile("/business.log")); } } // 拿到mediaid, 就可以发起推送了 // 先获取openid列表 $openidsql = "select openid, nickname from wx_user_info where subscribe = '1' "; $openidres = $db->query($openidsql); // php的json格式:array(""=>"") $openidList = $db->fetch_all($openidres); // 循环下,转成无key的值数组 $openidarr = array(); $lognickname = ""; foreach ($openidList as $openid) { $lognickname .= $openid["nickname"] . ","; $openidarr[] = $openid["openid"]; } //echo print_r($openidarr); $tit = new TypeImageText(); echo $tit->sendMultiImageTextMsgToMultiUser($openidarr, $articles); echo "<br />消息发送到:" . $lognickname; }
/** * 图文消息推送预览接口 */ public function batchSendPicMsgYulan($openidarr, $mediaid) { $yulanurl = "https://api.weixin.qq.com/cgi-bin/message/mass/preview?access_token="; $data = array("touser" => $openidarr, "mpnews" => array("media_id" => $mediaid), "msgtype" => "mpnews"); $datastr = JsonUtil::getJsonStrFromArray($data); //echo print_r($datastr); $url = $yulanurl . WxUtil::getWxTokenFromDB(); $response = RequestUtil::httpPost($url, $datastr, 'post'); if ($response['errcode'] == 0) { return "图文消息群发成功"; } return $respArr["errcode"] . ":" . $respArr["errmsg"]; }