微信公众平台接口开发教程
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
⛩ࠫþᡀѪᔰਁ㘵’
z URLǃTOKEN 䇔䇱
ᡀѪᔰਁ㘵Ⲵㅜа↕ቡᱟປ߉ URLǃTOKEN ؑˈᶕሩᴽ࣑ಘ䘋㹼傼䇱
1˅ ᢺл䶒ԓ⸱༽ࡦᒦ؍ᆈѪањ php ᮷Ԧ˄ྲ weixin.php˅
<?php /**
* wechat php test */
//define your token define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); $wechatObj->valid();
ᗞؑޜՇᒣਠ᧕ਓᔰਁᮉ〻
൘ᵜ᮷Ⲵ㔃ቮ㔉བྷᇦӻ㓽ањ࡙⭘ᗞؑޜՇᒣਠ᧕ਓᔰਁⲴᗞؑՊઈ㇑⨶䖟Ԧᒣਠ аǃӰѸᱟᗞؑՊઈ˛
ᡰ䉃ᗞؑՊઈˈᱟՊઈ㇑⨶䖟Ԧሶ୶ᇦਁ㹼ⲴՐ㔏Պઈˈ㓿䗷⭥ᆀॆ䖜ᦒਾˈᆈۘ൘ᗞؑѝDŽ Պઈᢃᔰᵪᗞؑˈᢛ᧿୶ᇦⲴᗞؑҼ㔤⸱˄ᡆ࣐ᗞؑޜՇᑀਧѪྭ৻˅ˈቡ㜭㧧ᗇаᕐ⭥ᆀՊઈ ˈᒦᆈۘҾᗞؑѝˈণਟ䙊䗷ࡧᗞؑˈӛਇ୶ᇦᨀⲴՊઈᣈᢓ઼ՈᜐDŽ
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)){
class wechatCallbackapiTest {
public function valid() {
$echoStr = $_GET["echostr"];
//valid signature , option if($this->checkSignature()){
echo $echoStr; exit; } }
z ᗞؑՊઈ㇑⨶㌫㔏˄࡙⭘ᗞؑᔰਁᴰᡀ࣏Ⲵ୶ъབྷ⢋˅
л䶒㔉བྷᇦӻ㓽ањᗞؑޜՇᒣਠᔰਁᴰᡀ࣏Ⲵ“བྷ⢋血”—--␡ൣа᱃ޜਨ
ⲴᗞؑՊઈ㇑⨶㌫㔏ˈ䇙Ӫ㙣ⴞаᯠˈⴞࡽᱟᡁⴻࡠᗞؑޜՇᒣਠ࡙⭘ᴰᡀ࣏Ⲵ 䖟ԦҶDŽл䶒㔉བྷᇦӻ㓽алԆⲴ࣏㜭DŽ䇙བྷᇦԕਾڊᔰਁⲴᰦى㜭ᴹњᖸྭⲴ ᙍ䐟DŽ
⭘ᵪᗞؑᐖ䗩ⲴҼ㔤⸱䇶 ࡛ḷ䇶DŽ ˄䈕ḷ䇶ᱟᒯᐎ、ؑ http://www.020pos.com Ⲵᗞؑ ޜՇ䍖ਧ˅
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml>
}else { echo ""; exit;
} }
private function checkSignature() {
$signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"];
zHale Waihona Puke Baidu⌘ᡀѪᔰਁ㘵
⌘ޜՇ䍖ਧˈ䴰㾱ⲫ䇠㠚ᐡⲴⵏᇎ䓛ԭؑ ⌘˖䇮㖞ޜՇਧؑᰦаᇊ㾱䉘ˈ‘ޜՇਧ〠ÿо‘ᗞؑਧÿປ߉ਾᱟн㜭؞᭩Ⲵˈ 䴰һݸ䇔ⵏᜣྭ ⌘ᆼᡀਾˈ䘋ޕ㌫㔏ˈ䘹ᤙሬ㡚кⲴþ儈㓗࣏㜭’
ަѝᴹєњ⁑ᔿ˖þ㕆䗁⁑ᔿÿоþᔰਁ⁑ᔿ’ 唈䇔ᔰⲴѪþ㕆䗁⁑ᔿ’
䘋ޕþ㕆䗁⁑ᔿÿˈ⛩ࠫਣк䀂Ⲵþޣ䰝ÿ䬮᧕ 䘋ޕþᔰਁ⁑ᔿÿˈ⛩ࠫਣк䀂Ⲵþᔰÿ᤹䫞 ᔰਾਟԕⴻࡠ˖
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml>
$token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
}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;
z ᗞؑӔӂ皮ֻ
ڊањㆰঅⲴ皮ֻ˖⭘ᡧਁ䘱ӰѸ᮷ᆇˈᡁԜቡഎ༽ӰѸ᮷ᆇ ⌘˖༽ࡦԓ⸱ਾ㾱ᢺ TOKEN ٬؞᭩Ѫ㠚ᐡⲴ <?php /**
* wechat php test */
//define your token define("TOKEN", "xxx");
$str = ''; $wechatObj = new wechatCallbackapiTest(); $wechatObj->valid();
<ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> <FuncFlag>1</FuncFlag> </xml>"; if(!empty( $keyword )) { $msgType = "text"; $contentStr = $keyword; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; }
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)){
}else{ return false;
} } }
?>
z ⭘ᡧ⌘ޣਾਁ䘱⅒䗾ؑ
൘ᇎ䱵ᓄ⭘ѝˈ⭘ᡧㅜа⅑⌘ޣᱟˈᡁԜ䴰㾱ਁ䘱⅒䗾઼ᨀ皮ؑˈቡ䴰㾱л䶒Ⲵࡔᯝԓ⸱
if($keyword === "Hello2BizUser"){ $contentStr = "⅒䗾ؑ";
} ᔰਁ᮷ẓѝ߉᰾ˈ⭘ᡧ⌘ޣਾᴽ࣑ಘ㠚ࣘਁ䘱䗷ᶕⲴᆇㅖѢѪ"Hello2BizUser"ˈᡁԜቡ ⅑ᶕࡔᯝҶ ⌘˖ؑ⁑ᶯѝ↔亩<FuncFlag>1</FuncFlag>Ⲵ٬㾱䇮Ѫ1ˈ䘉ṧ⭘ᡧᗞؑѝՊᱮ皮ᴹ ᯠ⎸Ⲵḷᘇ
class wechatCallbackapiTest {
public function valid() {
$echoStr = $_GET["echostr"];
//valid signature , option if($this->checkSignature()){
echo $echoStr; $this->responseMsg(); exit; } }
ਾ࣐ԆѪᗞؑྭ৻ˈᗞؑՊ㠚ࣘഎ༽ˈ䈧䗃ޕᵪਧ⸱ˈᛘഎ༽㔉ᆳᵪਧ⸱ ᆳ㠚ࣘሶਁ䘱аᶑᇶ⸱傼䇱⸝ؑ㔉ᛘˈᛘഎ༽傼䇱⸝ؑਾˈᛘሶ㠚ࣘᡀѪᒯᐎ 、ؑⲴሺ䍥VIPՊઈˈӛਇԆԜⲴᡰᴹӗ૱9ᣈՈᜐDŽᒯᐎ、ؑˈቡᱟ 䈕ᗞؑՊઈᒣਠⲴᇈᯩᦸᵳԓ⨶ˈᡁ৫䗷ԆԜ␈ᇍᓇ䬪ൠ൰ http://tgfweng.taobao.comˈ䈕ᗞؑՊઈᒣਠ࣏㜭䎵ᕪ˖ᵜᗞؑᒣਠ䘲ਸᴽ࣑㹼ъ ⲴՊઈ䘎䬱䖟ԦDŽާᴹ㔉Պઈਁ⸝࣏ؑ㜭ˈ㜭䘎䬱DŽՊઈ䘌〻൘ᇦҏ㜭㇑⨶ᛘⲴ ᓇ䬪ᛵߥˈՊઈҏਟԕ䙊䗷Ც㜭ᵪ㓸ㄟᡆ㘵ᗞؑˈḕⴻ㠚ᐡⲴՊઈ〟࠶ᛵߥˈ ۘ٬ᛵߥˈ䘈ਟԕḕⴻᓇ䬪ᢃᣈؑˈ୶૱ؑˈ׳䬰ؑㅹˈ䘈㜭䘌〻лঅDŽ л䶒ᱟ䖟Ԧᒣਠⲫ䱶ൠ൰৺փ傼䍖ਧ˄৫ⴻⴻᱟྲօᡀѪԆԜⲴՊઈⲴˈㄏ ❦㜭㠚࣐ࣘޕᡀѪᗞؑՊઈʽʽʽཚ血ཷҶ˅˖
<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..."; }
3˅ ປ߉ URL TOKEN ؑ
എࡠޜՇᒣਠ亥䶒ˈᢺ URL TOKEN ؑປ߉ਾᨀӔˈ䴰㾱؍䇱 URL окՐⲴ php ᮷Ԧൠ ൰а㠤ˈᒦф TOKEN ٬о php ѝᇊѹⲴа㠤 ؑປ߉↓⺞ਾᨀӔˈ↓ᑨՊᨀ皮ᆼᡀؑ
㠣↔ˈᐢ㓿↓ᔿᡀѪҶᔰਁ㘵ˈ᧕лᶕቡਟԕᆹ㻵㠚ᐡⲴᙍ䐟ᔰਁ〻ᒿҶ
return true; }else{
return false; } } }
?>
2˅ ؞᭩ TOKEN
TOKEN ᱟ⭘ᶕ䘋㹼Ӕӂᆹޘ䇔䇱Ⲵˈ㠚ᐡ䲿ᇊѹˈ⌘؍䇱ᆹޘ ᇊѹਾ؞᭩ԓ⸱ˈ൘ԓ⸱亦䜘ࡠ define("TOKEN", "weixin"); ᢺ٬᭩Ѫ㠚ᐡⲴ TOKEN ٬˄ྲ˖mytoken˅ˈ؍ᆈ᮷Ԧˈ❦ਾкՐࡠᴽ࣑ಘˈ㾱⺞؍ਟԕ 䇯䰞
ҼǃᗞؑՊઈᴹӰѸՈ࣯˛ ᤃኅᯠᇒᡧ˖ᆹ㻵ᗞؑᇒᡧㄟⲴ⎸䍩㘵ᐢᴹ 3 ӯѻཊ(ⴞࡽ䘈൘໎䮯ˈᒣ൷⇿ཙᯠ໎ 100 з⭘ᡧ)ˈབྷ 䟿⎸䍩㘵ਟԕ䙊䗷ᢛ᧿Ҽ㔤⸱Ⲵᯩᔿˈ䗵䙏ᑞᤃኅՊઈ䟿˗ 㔤㌫㘱ᇒᡧ˖ሶՐ㔏Պઈ˄⻱ˈᝏᓄˈᶑ⸱ㅹ˅䐏ᗞؑ㔁ᇊˈࠪ䰘н⭘ᩪᑖˈᯩ、ׯᢰˈ 䟺᭮亮ᇒⲴ䫡व˗ Պઈӂࣘ˖Պઈਟԕ䙊䗷ᗞؑㄟˈḕ䈒Պઈ〟࠶ˈ։仍ˈ⎸䍩䇠ᖅˈ䘈ਟԕᇎ⧠ᴽ࣑亴ᇊˈӗ૱䇒 䍝࣏㜭ˈ㔉ᛘᑖᶕ䇒অ˗ ׳䬰㗔ਁ˖䙊䗷ᗞؑᒣਠˈ䘈ਟԕݽ䍩㔉ᗞؑ⭘ᡧਁ䘱Ոᜐؑˈྲ᮷ᆇˈമ⡷ˈ༠丣ˈ㿶仁ㅹˈሶᛘⲴ ᒯݽ䍩ਁ㔉亮ᇒDŽ -----------------------------------------------------ԕлᱟᔰਁ㘵สᮉ〻-------------------------------------------------------