接入微信公眾平臺開發,開發者需要按照如下步驟完成:
1)首先在網頁端打開微信公眾號登錄進去,左側的 開發-->開發者工具-->公眾平臺測試帳 號掃碼登錄進去;2)把代碼傳到新浪云上復制新浪云的鏈接,3)把新浪云上的鏈接復制到在微信公眾平臺上
3)在新浪云編輯代碼上上傳wx_sample.php文件;
4)在新浪云建立數據庫共享型數據庫點擊管理MySQL;
5)在數據庫中建立一個ticket表 2個字段點擊執行
建成之后是這樣的:
在數據庫中再建一個token表,和上面的格式一樣這樣就建好數據庫了;
在jssdk(微信接口端)鏈接數據庫里面的token和ticket;
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:
(中括號內的為需要填寫的自己的信息)
[APPID]為測試號管理的 appID 賬號
[SECRET]為測試號管理的appsecret賬號
[CODE]為第三步第一小步獲取的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";
而此時獲取查詢結果可已通過查詢
<?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);
?>