微信公众平台接口开发教程

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 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Ž -----------------------------------------------------ԕлᱟᔰਁ㘵ส⹰ᮉ〻-------------------------------------------------------
相关文档
最新文档