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

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

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

步骤如下:

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

第二步:注册sae(http://sae.sina.com.cn/),作为你的服务器。

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

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

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

第六步:成功了。

开始吧:

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

2.注册sae(http://sae.sina.com.cn/)

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

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

PHP技术开发微信公众平台

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

PHP技术开发微信公众平台

PHP技术开发微信公众平台

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

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

PHP技术开发微信公众平台

PHP技术开发微信公众平台

PHP技术开发微信公众平台

打开后是如下的代码:

<"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 (!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 = "<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(!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;
 }
 }
}
"htmlcode">
<"TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//这里做了更改
if($_GET["echostr"]){
 $wechatObj->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 = "<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(!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;
 }
 }
}
"text-align: center">PHP技术开发微信公众平台

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

PHP技术开发微信公众平台

5.登录微信公众平台,进入开发者中心。开启“服务者配置”。url填写上一步复制的url(这里我删除了前面的1.因为我的sae默认第一版。你可以试试,删除1,若是url访问,不报404,那就是没问题)。token填写的是代码中的token(上面是“weixin”)。

PHP技术开发微信公众平台

PHP技术开发微信公众平台

如果启用成功,就可以关注你的微信平台,输入内容,看看提示是不是Welcome to wechat world!或者Hello Word!

以上全部内容就是针对微信公众平台做的讲解,希望可以帮助到大家。

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com

RTX 5090要首发 性能要翻倍!三星展示GDDR7显存

三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。

首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。

据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。