微信

来源:互联网 发布:域名怎么跟服务器绑定 编辑:程序博客网 时间:2024/04/28 05:16

1.

[python] view plain copy 在CODE上查看代码片派生到我的代码片
  1. 在微信公共平台中建立一个账号。  

2.

[python] view plain copy 在CODE上查看代码片派生到我的代码片
  1. 在开发者工具中进入微信公共平台测试账号  

3.

[python] view plain copy 在CODE上查看代码片派生到我的代码片
  1. 进入微信公共平台测试账号之后你可以看到appID和appsecret的信息  

4.

[python] view plain copy 在CODE上查看代码片派生到我的代码片
  1. 然后在体验接口权限表中-----基础支持-----获取access_token------开发者必读------接入指南  

5.复制代码放到你的服务器中

[python] view plain copy
  1. <?php  
  2. /**  
  3.  * wechat php test  
  4.  */  
  5. //define your token  
  6. define("TOKEN","weixin");  
  7. define("appID","wxbf7cb55e****a7a7");  
  8. define("appsecret","f9cdc960*****ed32e7b72d296420a60 ");  
  9. $wechatObj = new wechatCallbackapiTest();  
  10. $wechatObj->valid();  
  11. class wechatCallbackapiTest  
  12. {  
  13.     public function valid()  
  14.     {  
  15.         $echoStr = $_GET["echostr"];  
  16.   
  17.         //valid signature , option  
  18.         if($this->checkSignature()){  
  19.             echo $echoStr;  
  20.             echo $this->createMenu();  
  21.             $this->responseMsg();  
  22.             exit;  
  23.         }  
  24.     }  
  25.     public function responseMsg()  
  26.     {  
  27.         //get post data, May be due to the different environments  
  28.         $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];  
  29.   
  30.         //extract post data  
  31.         if (!empty($postStr)){  
  32.             /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,  
  33.                the best way is to check the validity of xml by yourself */  
  34.             libxml_disable_entity_loader(true);  
  35.             $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);  
  36.             $fromUsername = $postObj->FromUserName;  
  37.             $toUsername = $postObj->ToUserName;  
  38.             $keyword = trim($postObj->Content);  
  39.             $time = time();  
  40.             $textTpl = "<xml>  
  41.                             <ToUserName><![CDATA[%s]]></ToUserName>  
  42.                             <FromUserName><![CDATA[%s]]></FromUserName>  
  43.                             <CreateTime>%s</CreateTime>  
  44.                             <MsgType><![CDATA[%s]]></MsgType>  
  45.                             <Content><![CDATA[%s]]></Content>  
  46.                             <FuncFlag>0</FuncFlag>  
  47.                             </xml>";  
  48.                 if(!empty( $keyword ))  
  49.                 {  
  50.                     $msgType = "text";  
  51.                     //$contentStr = "Welcome to wechat world!";  
  52.                     $contentStr=$this->test($keyword);  
  53.                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  
  54.                     echo $resultStr;  
  55.                 }else{  
  56.                     $msgType = "text";  
  57.                     $contentStr = "欢迎光临";  
  58.                     $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);  
  59.                     echo $resultStr;  
  60.                 }  
  61.         }else {  
  62.             echo "";  
  63.             exit;  
  64.         }  
  65.     }  
  66.     public function test($keyword){  
  67.         $url="http://www.tuling123.com/openapi/api?key=7de9378923d2693b87223a03e7cc400e&info=".$keyword;  
  68.         $html=file_get_contents($url);  
  69.         $arr=json_decode($html,true);  
  70.         return $arr['text'];  
  71.     }  
  72.     private function checkSignature()  
  73.     {  
  74.         // you must define TOKEN by yourself  
  75.         if (!defined("TOKEN")) {  
  76.             throw new Exception('TOKEN is not defined!');  
  77.         }  
  78.         $signature = $_GET["signature"];  
  79.         $timestamp = $_GET["timestamp"];  
  80.         $nonce = $_GET["nonce"];  
  81.         $token = TOKEN;  
  82.         $tmpArr = array($token, $timestamp, $nonce);  
  83.         // use SORT_STRING rule  
  84.         sort($tmpArr, SORT_STRING);  
  85.         $tmpStr = implode( $tmpArr );  
  86.         $tmpStr = sha1( $tmpStr );  
  87.         if( $tmpStr == $signature ){  
  88.             return true;  
  89.         }else{  
  90.             return false;  
  91.         }  
  92.     }  
  93.     public function getAccessToken(){  
  94.         $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".appID."&secret=".appsecret;  
  95.         $json=file_get_contents($url);  
  96.         $arr=json_decode($json,true);  
  97.         $Accesstoken=$arr['access_token'];  
  98.         return $Accesstoken;  
  99.     }  
  100.     public function createMenu(){  
  101.         $accesstoken=$this->getAccessToken();  
  102.         $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$accesstoken;  
  103.         $data='  
  104.                  {  
  105.              "button":[  
  106.              {  
  107.                   "type":"click",  
  108.                   "name":"名字",  
  109.                   "key":"V1001_TODAY_MUSIC"  
  110.               },  
  111.               {  
  112.                    "name":"菜单",  
  113.                    "sub_button":[  
  114.                    {  
  115.                        "type":"view",  
  116.                        "name":"搜索",  
  117.                        "url":"http://www.soso.com/"  
  118.                     },  
  119.                     {  
  120.                        "type":"view",  
  121.                        "name":"视频",  
  122.                        "url":"http://v.qq.com/"  
  123.                     },  
  124.                     {  
  125.                        "type":"click",  
  126.                        "name":"赞一下我们",  
  127.                        "key":"V1001_GOOD"  
  128.                     }]  
  129.                }]  
  130.          }';  
  131.         $this->curlPost($url,$data,'POST');  
  132.     }  
  133.     public function curlPost($url,$data,$method){  
  134.         $ch = curl_init();   //1.初始化  
  135.         curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址  
  136.         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式  
  137.         //4.参数如下  
  138.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https  
  139.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  
  140.         curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器  
  141.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
  142.         curl_setopt($ch, CURLOPT_AUTOREFERER, 1);  
  143.         curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容  
  144.         curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');  
  145.         if($method=="POST"){//5.post方式的时候添加数据  
  146.             curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
  147.         }  
  148.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
  149.         $tmpInfo = curl_exec($ch);//6.执行  
  150.         if (curl_errno($ch)) {//7.如果出错  
  151.             return curl_error($ch);  
  152.         }  
  153.         curl_close($ch);//8.关闭  
  154.         return $tmpInfo;  
  155.     }  
  156. }  
  157. ?> 
0 0
原创粉丝点击