php开发微信手册,PHP技术开发微信公众平台

这篇文章主要介绍了微信公众平台的两种模式(编辑模式和开发模式)顾名思义编辑模式就是写普通的功能,开发模式具有更多的功能,下面主要是针对开发模式做介绍,

下面通过图文并茂的方式介绍微信公众平台开发过程,具体内容如下:

微信公众平台有两种模式:编辑模式 和 开发模式。

普通的功能可以通过编辑模式来搞定。开发模式具有更多的功能。让我们来使用开发模式开发helloword吧

步骤如下:

第一步:先注册一个公众号(https://mp.weixin.qq.com)

第二步:注册sae(),作为你的服务器。

第三步:登录微信公众平台(https://mp.weixin.qq.com)查看开发文档并下载官方提供的demo。做适当修改。

第四步:将代码压缩成zip格式,上传到sae平台。

第五步:登录微信公众平台,进入开发者中心。开启“服务者配置”。

第六步:成功了。

开始吧:

1.先注册一个公众号(https://mp.weixin.qq.com)

2.注册sae()

注册完以后要记得进行实名认证,不然绑定到公众平台的时候,会有永远的“无法获取token”的提示。(实名认证需要3个工作日才能成功)

然后可以点击创建应用。创建后可以在下面看到。

ff67cf838eb2b8cbdba491b706cf72a7.png

进入自己创建的应用。然后点击代码管理。

34838bd387ececc3b02a457e3aeb09b3.png

939c19c77cbeae1983c4cf88d24082ba.png

3.登录微信公众平台(https://mp.weixin.qq.com)

查看开发文档并下载官方提供的demo。

a6f98413e2eb6358232f30b8f83c0b58.png

0a9bc2c3b9c0bfa012bbd9d6042bac3b.png

3e5e8f2d955b2f716b5dbf9b2c6f08de.png

打开后是如下的代码:

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)){

/* 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 = "%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()

{

// 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;

}

}

}

?>

我试过,,如上代码,似乎无法执行到response那一块。所以做了更改

valid();

}else{

$wechatObj->responseMsg();

}

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)){

/* 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 = "%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()

{

// 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;

}

}

}

?>

你可以将Welcome to wechat world!更改为Hello Word!

4.将代码压缩成zip格式,上传到sae平台。

1161181f4654dcc0a5cd096e5ae30fee.png

点击“编辑代码”,可以看到你上传的php文件。然后右击,url查看。复制url()。在这里要记住在此php文件中定义的token。此处为“weixin”,可以在如下图中看到。

1fb712813e3cd959874e6224279baeb1.png

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值