微信
来源:互联网 发布:域名怎么跟服务器绑定 编辑:程序博客网 时间:2024/04/28 05:16
1.
- 在微信公共平台中建立一个账号。
2.
- 在开发者工具中进入微信公共平台测试账号
3.
- 进入微信公共平台测试账号之后你可以看到appID和appsecret的信息
4.
- 然后在体验接口权限表中-----基础支持-----获取access_token------开发者必读------接入指南
5.复制代码放到你的服务器中
- <?php
- /**
- * wechat php test
- */
- //define your token
- define("TOKEN","weixin");
- define("appID","wxbf7cb55e****a7a7");
- define("appsecret","f9cdc960*****ed32e7b72d296420a60 ");
- $wechatObj = new wechatCallbackapiTest();
- $wechatObj->valid();
- class wechatCallbackapiTest
- {
- public function valid()
- {
- $echoStr = $_GET["echostr"];
- //valid signature , option
- if($this->checkSignature()){
- echo $echoStr;
- echo $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(!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){
- $url="http://www.tuling123.com/openapi/api?key=7de9378923d2693b87223a03e7cc400e&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 );
- if( $tmpStr == $signature ){
- return true;
- }else{
- return false;
- }
- }
- public function getAccessToken(){
- $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.soso.com/"
- },
- {
- "type":"view",
- "name":"视频",
- "url":"http://v.qq.com/"
- },
- {
- "type":"click",
- "name":"赞一下我们",
- "key":"V1001_GOOD"
- }]
- }]
- }';
- $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;
- }
- }
- ?>
0 0