public function replyCode() { $openId = (string) $this->postObj->FromUserName; $customerInfo = get_customer_info($openId); $customerId = $customerInfo['id']; $url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s'; $accessToken = get_access_token(); $url = sprintf($url, $accessToken); $data['expire_seconds'] = 1800; $data['action_name'] = $this->actionName; $data['action_info'] = array('scene' => array('scene_id' => (int) $customerId)); $data = json_encode($data); // $replyMessage = A('ReplyMessage'); // $replyMessage->setText('传入的JSON数据为' . $data); // $replyMessage->replyTextMessage(); // return; $res = http_post_json($url, $data); $res = json_decode($res); $ticket = $res->ticket; $ticket = urlencode($ticket); $replyMessage = A('ReplyMessage'); $picUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=" . $ticket; $url = $picUrl; $title = '您的专属二维码'; $description = '其它用户通过扫描该二维码关注我们,将自动成为您的朋友。'; $news[] = array('title' => $title, 'description' => $description, 'picurl' => $picUrl, 'url' => $url); $replyMessage->setNews($news); $replyMessage->replyNewsMessage(); }
public function sendMenuAction() { $CustomMenu = D('CustomMenu'); $menuList = $CustomMenu->fetchMenuList(); $wechatMenu = array(); //微信菜单的数组,用于转为 json //进行微信菜单的拼接 foreach ($menuList as $key => $value) { //如果存在子菜单,说明为目录 if (array_key_exists('_child', $value)) { $subButton = array(); foreach ($value['_child'] as $k => $v) { //进行微信菜单格式的转换 $subButton[] = $this->_to_wechat_button($v); } $wechatMenu['button'][] = array("name" => $value['title'], 'sub_button' => $subButton); } else { $wechatMenu['button'][] = $this->_to_wechat_button($value); } } $json = urldecode(json_encode($this->_url_encode($wechatMenu))); $accessToken = get_access_token(); $url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token=' . $accessToken; $res = json_decode(http_post_json($url, $json)); if ($res->errmsg == 'ok') { $this->success('菜单生成成功,新菜单将在12小时后生效,重新关注后可立即查看效果', U('CustomerMenu/index'), 2); } else { $this->error('操作失败,错误代码:' . $res->errcode . $res->errmsg, U('CustomerMenu/index'), 30); } }
$msg = $obj->msg; } if (empty($devid)) { die(json_encode(array("ret" => -1, "msg" => "no devid"))); } $sendurl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . get_access_token(); $jsondata = array("msgtype" => "text", "text" => array("content" => $msg)); $users = QMC::read($devid); if (!$users || empty($users)) { die(json_encode(array("ret" => -2, "msg" => "no user registered"))); } foreach ($users as $user) { # code... $jsondata["touser"] = $user; $jsonStr = json_encode($jsondata); $http = http_post_json($sendurl, $jsonStr); } echo json_encode(array("ret" => "0")); function http_post_json($url, $jsonStr) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($jsonStr))); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return array($httpCode, $response); }
$token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx926454a4965d2b7c&corpsecret=PsPkG1V74sWm6V2mNN3F2NOHKQT696g1wrmJZhB7WT-8xcLmt-3rez3FfuH3SzCf'; $msg_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token='; function http_get_access_token($token_url) { $json = file_get_contents($token_url); $obj = json_decode($json); return $obj->access_token; } function http_post_json($url, $jsonStr) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8'); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($jsonStr))); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return array($httpCode, $response); } function json_text_message($user, $message) { $data = ["touser" => $user, "msgtype" => "text", "agentid" => "0", "text" => ["content" => $message], "safe" => "0"]; return json_encode($data, JSON_UNESCAPED_UNICODE); } $access_token = http_get_access_token($token_url); $json_message = json_text_message("@all", "大家好 ~!"); var_dump(http_post_json($msg_url . $access_token, $json_message));