接入微信公眾平臺開發,開發者需要按照如下步驟完成:
1、填寫服務器配置
2、驗證服務器地址的有效性
3、依據接口文檔實現業務邏輯
第一步:填寫服務器配置
其中URL為驗證消息的確來自微信服務器所存的PHP文件的路徑;
Token為自己填寫的任意字符,后面還會用到;
第二步:驗證消息的確來自微信服務器
保存以下php代碼并打包上傳到服務器
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "Token"); //這里的token即為上面填寫的token
$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;
}
}
}
?>
第三步:依據接口文檔實現業務邏輯
1.網頁授權獲取用戶基本信息
(首先需要在權限列表中網頁帳號->[網頁授權獲取用戶基本信息]修改權限,填寫自己的網站網址);
第一步:用戶同意授權,獲取code
(中括號內的為需要填寫的自己的信息)(后面的訪問也是這樣訪問)
https://open.weixin.qq.com/connect/oauth2/authorize?appid=[APPID]&redirect_uri=[REDIRECT_URI]&response_type=code&scope=[SCOPE]&state=STATE#wechat_redirect```
此時PHP代碼則可以獲取code
header("Content-type:text/html;charset=utf-8");
$code=$_GET['code'];
echo $code;
**第二步:通過code換取網頁授權access_token**
獲取code后,請求以下鏈接獲取access_token:
(中括號內的為需要填寫的自己的信息)
php代碼示例
header("Content-type:text/html;charset=utf-8");
$code=$_GET['code'];
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx743540570fe83a69&secret=d23fa1d3e81aeaa0a9b80e1829fd9642&code=".$code."&grant_type=authorization_code";
而此時獲取查詢結果可已通過查詢
<?php
header("Content-type:text/html;charset=utf-8");
$code=$_GET['code'];
$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=wx743540570fe83a69&secret=d23fa1d3e81aeaa0a9b80e1829fd9642&code=".$code."&grant_type=authorization_code";
$res=json_decode(file_get_contents($url));
$access_token=$res->access_token;
$open=$res->openid;
$infourl="https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$open."&lang=zh_CN";
$infores=json_decode(file_get_contents($infourl));
var_dump($infores);
?>
目的:使用jssdk功能
使用流程(接入jssdk流程):
---1、設置安全域名
---2、引入js 文件
---3、注入config接口,進行驗證
---4、驗證成功,通過ready方法 回調jssdk功能。(具體實現什么,看需求)
重點:步驟三---驗證
1、獲取access_token
2、通過 access_token 獲取jsapi_ticket
3、利用jsapi_ticket生成簽名
4、將 簽名、隨機字符串、時間戳、string以數組形式返回。