创建一个属于自己的微信订阅号
2016-04-28 20:07
543 查看
首先我们需要在微信公众平台注册一个属于自己的开发账号,需要注意的是,这个账号和我们自己用的聊天的微信号是不一样的,然后,新浪云我们也是要通过实名认证服务的。
接下来,我们就可以开发属于自己的微信订阅号了。
登陆之后,我们在左侧菜单栏中选择“----开发者工具------公众平台测试账号”。
接口配置信息中的 “ 消息接口使用指南”给我们提供了一个开发文档。我们可以参考一下。
可以将下面的代码放进你的服务器中了
在上面的过程中我们需要不断的去修改接口的配置信息看能否保存成功来验证我们的代码
接下来,我们就可以开发属于自己的微信订阅号了。
登陆之后,我们在左侧菜单栏中选择“----开发者工具------公众平台测试账号”。
接口配置信息中的 “ 消息接口使用指南”给我们提供了一个开发文档。我们可以参考一下。
可以将下面的代码放进你的服务器中了
<?php /** * wechat php test */ //define your token define("TOKEN","weixin"); define("appID","你自己的appID"); define("appsecret","你自己的appsecret"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->valid(); class wechatCallbackapiTest { public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; //echo $this->getAccessToken(); $this->createMenu(); $this->responseMsg(); exit; } } public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)){ /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection, the best way is to check the validity of xml by yourself */ libxml_disable_entity_loader(true); $postObj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>0</FuncFlag> </xml>"; if($MsgType=='voice'){ $msgType="text"; $contentStr=$this->test($postObj->Recognition); $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ if(!empty( $keyword )) { $msgType = "text"; //$contentStr = "Welcome to wechat world!"; $contentStr=$this->test($keyword); $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ $msgType = "text"; $contentStr = "我给你点个赞"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } } }else { echo ""; exit; } } public function test($keyword){ //需要设置自己的tulingkey $url="http://www.tuling123.com/openapi/api?key=********************&info=".$keyword; $html=file_get_contents($url); $arr=json_decode($html,true); return $arr['text']; } private function checkSignature() { // you must define TOKEN by yourself if (!defined("TOKEN")) { throw new Exception('TOKEN is not defined!'); } $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); return true; if( $tmpStr == $signature ){ return true; }else{ return false; } } public function getAccessToken(){ //设置自己的appid和appsecret $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".appID."&secret=".appsecret; $json=file_get_contents($url); $arr=json_decode($json,true); $Accesstoken=$arr['access_token']; return $Accesstoken; } public function createMenu(){ $accesstoken=$this->getAccessToken(); $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$accesstoken; $data='{ "button":[ { "type":"click", "name":"张三", "key":"V1001_TODAY_MUSIC" }, { "name":"应用软件", "sub_button":[ { "type":"view", "name":"搜索", "url":"http://www.baidu.com/" }, { "type":"view", "name":"我的博客", "url":"http://blog.csdn.net/myhuashengmi" }, { "type":"view", "name":"视频", "url":"http://v.qq.com/" }, { "type":"click", "name":"赞一下我们", "key":"V1001_GOOD" }] }, { "name": "发图", "sub_button": [ { "type": "pic_sysphoto", "name": "系统拍照发图", "key": "rselfmenu_1_0", "sub_button": [ ] }, { "type": "pic_photo_or_album", "name": "拍照或者相册发图", "key": "rselfmenu_1_1", "sub_button": [ ] }, { "type": "pic_weixin", "name": "微信相册发图", "key": "rselfmenu_1_2", "sub_button": [ ] } ] }] }'; return $this->curlPost($url,$data,'POST'); } public function curlPost($url,$data,$method){ $ch = curl_init(); //1.初始化 curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式 //4.参数如下 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容 curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate'); if($method=="POST"){//5.post方式的时候添加数据 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $tmpInfo = curl_exec($ch);//6.执行 if (curl_errno($ch)) {//7.如果出错 return curl_error($ch); } curl_close($ch);//8.关闭 return $tmpInfo; } } ?>
在上面的过程中我们需要不断的去修改接口的配置信息看能否保存成功来验证我们的代码
相关文章推荐
- 仿微信联系人列表字母侧滑控件
- 除微信顶部的“防欺诈盗号,请勿支付或输入qq密码”
- 小木的智慧屋--微信公众号的推广案例分析(1)
- 微信公众平台
- Android录制微信小视频
- 玩玩微信雷达扫描布局
- 不用集成微信sdk,直接客户端调起微信支付
- HTML5 调用手机相册和摄像头的方法并上传微信下测试通过
- # 微信公众号内支付(.Net版本)开发详尽流程分享
- 微信公众平台java开发教程之验证服务器有效性
- 自定义View——类似微信裁剪控件
- 微信 支付支付授权目录 修改
- 微信支付(20140923更新)商户支付密钥key的生成与设置
- Java中的微信公众号开发(一)
- PHP处理微信昵称emoji方法
- 微信支付 php 公众平台及移动端服务器
- 微信公众平台——获取用户信息
- 高大上的微信公众号养成记
- 填平友盟社会化分享之---微信分享的坑
- 微信公众号线上和线下增粉秘籍: