当前位置:首页 > PHP教程 > php高级应用 > 列表

php 微信公众平台开发之验证步骤

发布:smiling 来源: PHP粉丝网  添加日期:2015-05-09 09:40:47 浏览: 评论:0 

微信公众平台开发我们现在做得比较多了,下面给各位介绍的是一个入门级别的微信公众平台验证基础知识了,有兴趣的和小编来看看。

开发微信的时候 需要验证一下,在官方开发者中心哪里有可以下源代码,登录到 公众帐号后 看到左边的最下角有一个开发者中心点击,然后填写上你相对应的 Token 和 url 然后就可以验证成功的话就可以开发了.

下载微信php验证源代码在 开发者中心 - 开发者文档 - 接口消息 - 验证消息真实 - 拉到最下面就php演示代码.  

下载好后代码如下:

  1. <?php 
  2. /** 
  3.   * wechat php test 
  4.   * update time: 20141008 
  5.   */ 
  6. //define your token 
  7. define("TOKEN", "weixin"); 
  8. $wechatObj = new wechatCallbackapiTest(); 
  9. $wechatObj->valid(); 
  10. class wechatCallbackapiTest 
  11. { 
  12. public function valid() 
  13.     { 
  14.         $echoStr = $_GET["echostr"]; 
  15.         //valid signature , option 
  16.         if($this->checkSignature()){ 
  17.         echo $echoStr; 
  18.         exit; 
  19.         } 
  20.     } 
  21.     public function responseMsg() 
  22.     { 
  23. //get post data, May be due to the different environments 
  24. $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; 
  25.       //extract post data 
  26. if (!emptyempty($postStr)){ 
  27.                  
  28.               $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); 
  29.                 $fromUsername = $postObj->FromUserName; 
  30.                 $toUsername = $postObj->ToUserName; 
  31.                 $keyword = trim($postObj->Content); 
  32.                 $time = time(); 
  33.                 $textTpl = "<xml> 
  34. <ToUserName><![CDATA[%s]]></ToUserName> 
  35. <FromUserName><![CDATA[%s]]></FromUserName> 
  36. <CreateTime>%s</CreateTime> 
  37. <MsgType><![CDATA[%s]]></MsgType> 
  38. <Content><![CDATA[%s]]></Content> 
  39. <FuncFlag>0</FuncFlag> 
  40. </xml>";              
  41. if(!emptyempty( $keyword )) 
  42.                 { 
  43.               $msgType = "text"; 
  44.                 $contentStr = "Welcome to wechat world!"; 
  45.                 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); 
  46.                 echo $resultStr; 
  47.                 }else{ 
  48.                 echo "Input something..."; 
  49.                 } 
  50.         }else { 
  51.         echo ""; 
  52.         exit; 
  53.         } 
  54.     } 
  55. private function checkSignature() 
  56. { 
  57.         $signature = $_GET["signature"]; 
  58.         $timestamp = $_GET["timestamp"]; 
  59.         $nonce = $_GET["nonce"]; 
  60. $token = TOKEN; 
  61. $tmpArr = array($token, $timestamp, $nonce); 
  62.         sort($tmpArr, SORT_STRING); 
  63. $tmpStr = implode( $tmpArr ); 
  64. $tmpStr = sha1( $tmpStr ); 
  65. if( $tmpStr == $signature ){ 
  66. return true;  //phpfensi.com 
  67. }else{ 
  68. return false; 
  69. } 
  70. } 
  71. } 
  72. ?> 

TOKEN 修改为你自己想要的 然后在 开发者中心也要写一样的,在验证的过程中 $wechatObj->valid(); 这段代码不能去除这个是验证,验证成功后 我们就可以把 这段 $wechatObj->valid(); 这个注释掉了,然后使用 $wechatObj->responseMsg(); 来进行测试

注意:在开发的时候需要把 $wechatObj->valid(); 给注释掉,不然在手机测试的时候 会没有显示什么.

Tags: php微信平台 php公众平台

分享到: