搜尋
首頁微信小程式微信開發微信開發系列教程(1)

微信開發系列教程(1)

Mar 31, 2017 pm 02:18 PM
微信開發

首先我們需要申請一個微信公眾帳號,因為這個公眾帳號是公司的,所以不方便透露給大家! 相信簡單的申請工作都是沒有問題的!申請成功之後,選單列會出現“高級功能”,如下圖:

微信開發系列教程(1)

#裡面有2中模式可以選擇,“編輯模式”和“開發模式”,上面都詳細的描述就不再想大家作解釋了! 

這裡我們要說的是開發模式。好了,到現在為止就可以進入開發模式的解說了!

然後我們在設定URL和Token值,如下圖:

微信開發系列教程(1)

URL:填入我們放demo的存取網址,例如:http://www .123.com/wx_sample.php

Token:這個值可以隨便寫。

開啟檔案wx_sample.php,修改下面的內容

define("TOKEN", "weixin"); //修改成自己填写的token

填寫之後,就可以提交了!

驗證成功之後,我們可以寫一個小範例測試一下,如下程式碼:

<?php
/**
  * wechat php test
  */
// define
// your
// token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$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)) {
                                      
            $postObj = simplexml_load_string($postStr, &#39;SimpleXMLElement&#39;, 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() {
        $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;
        } else {
            return false;
        }
    }
}
?>

最簡單的回覆訊息完成了!

以上是微信開發系列教程(1)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。