微信公众平台接口开发指南
注册成为开发者
注册公众账号,需要登记自己的真实身份信息
注意:设置公众号信息时一定要谨慎,‘公众号名称’与‘微信号’填写后是不能修改的,需事先认真想好
注册完成后,进入系统,选择导航上的‘高级功能’
其中有两个模式:‘编辑模式’与‘开发模式’
默认开启的为‘编辑模式’
进入‘编辑模式’,点击右上角的‘关闭’链接
进入‘开发模式’,点击右上角的‘开启’按钮
开启后可以看到:
点击‘成为开发者’
· URL、TOKEN认证
成为开发者的第一步就是填写URL、TOKEN信息,来对你服务器进行验证
1) 把下面代码复制并保存为一个php文件(如weixin.php)
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 (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "
%s
0
";
if(!empty( $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);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
2) 修改TOKEN
TOKEN是用来进行交互安全认证的,你自己随意定义,注意保证安全
定义后修改代码,在代码顶部找到
define("TOKEN", "weixin");
把值改为你自己的TOKEN值(如:mytoken),保存文件,然后上传到服务器,要确保可以访问
3) 填写URL TOKEN信息
回到公众平台页面,把URL TOKEN信息填写后提交,需要保证URL与上传的php文件地址一致,并且TOKEN值与php中定义的一致
信息填写正确后提交,正常会提示完成信息
至此,已经正式成为了开发者,接下来就可以安装自己的思路开发程序了
· 微信交互示例
做一个简单的示例:用户发送什么文字,我们就回复什么文字
注意:复制代码后要把TOKEN值修改为自己的
valid();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
$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)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "
%s
1
";
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = $keyword;
$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);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
· 用户关注后发送欢迎信息
在实际应用中,用户第一次关注是,我们需要发送欢迎和提示信息,就需要下面的判断代码
if($keyword === "Hello2BizUser"){
$contentStr = "欢迎信息";
}
开发文档中写明,用户关注后服务器自动发送过来的字符串为"Hello2BizUser",我们就依次来判断了
注意:信息
中此项
1的值要设为1,这样用户微信中才会显示有新消息的标志
新浪微博:@Hubert_陈凡辉