本文实例讲述了php版微信公众平台开发之验证步骤。分享给大家供大家参考,具体如下:
微信公众平台开发我们现在做得比较多了,这里给各位介绍的是一个入门级别的微信公众平台验证基础知识了,有兴趣的和小编来看看。
开发微信的时候 需要验证一下,在官方开发者中心哪里有可以下源代码,登录到 公众帐号后 看到左边的最下角有一个开发者中心点击,然后填写上你相对应的 token 和 url 然后就可以验证成功的话就可以开发了.
下载微信php验证源代码在 开发者中心 - 开发者文档 - 接口消息 - 验证消息真实 - 拉到最下面就php演示代码.
下载好后代码如下:
<?php
/**
* wechat php test
* update time: 20141008
*/
//define your token
define("token", "weixin");
$wechatobj = new wechatcallbackapitest();
$wechatobj->valid();
class wechatcallbackapitest
{
public function valid()
{
$echostr = $_get["echostr"];
//valid signature , option
if($this->checksignature()){
echo $echostr;
exit;
}
}
public function responsemsg()
{
//get post data, may be due to the different environments
$poststr = $globals["http_raw_post_data"];
//extract post data
if (!emptyempty($poststr)){
$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(!emptyempty( $keyword ))
{
$msgtype = "text";
$contentstr = "welcome to wechat world!";
$resultstr = sprintf($texttpl, $fromusername, $tousername, $time, $msgtype, $contentstr);
echo $resultstr;
}else{
echo "input something...";
}
}else {
echo "";
exit;
}
}
private function checksignature()
{
$signature = $_get["signature"];
$timestamp = $_get["timestamp"];
$nonce = $_get["nonce"];
$token = token;
$tmparr = array($token, $timestamp, $nonce);
sort($tmparr, sort_string);
$tmpstr = implode( $tmparr );
$tmpstr = sha1( $tmpstr );
if( $tmpstr == $signature ){
return true;
}else{
return false;
}
}
}
?>
其中:token 修改为你自己想要的 然后在 开发者中心也要写一样的,在验证的过程中 $wechatobj->valid(); 这段代码不能去除这个是验证,验证成功后 我们就可以把 这段 $wechatobj->valid(); 这个注释掉了,然后使用 $wechatobj->responsemsg(); 来进行测试
注意:在开发的时候需要把 $wechatobj->valid(); 给注释掉,不然在手机测试的时候 会没有显示什么.
更多关于php相关内容感兴趣的读者可查看本站专题:《php微信开发技巧汇总》、《php编码与转码操作技巧汇总》、《php网络编程技巧总结》、《php基本语法入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!