開發(fā)文檔
http://mp.weixin.qq.com/wiki/home/

Paste_Image.png
如果你有公眾號的話,那么完全可以使用已經(jīng)注冊的微信號,如果沒有,那么你可以申請一個測試號(目前公眾號申請有點難,可能等上四五天)。
按開發(fā)文檔進(jìn)行接入
如果你按步驟申請了,那么最終你會進(jìn)入這里:

Paste_Image.png
這是需要配置URL、Token等。在驗證URL需要下載一個東西放到服務(wù)器上,如下所示:

Paste_Image.png

Paste_Image.png

Paste_Image.png
最后點擊配置,就能成功。
再驗證完畢后,更改這個文件。

Paste_Image.png
介紹
公眾號一般是接受消息,然后回復(fù)消息,那么問題來了,接受的消息是啥樣的?該怎么回復(fù)消息?如截圖所示:

Paste_Image.png

Paste_Image.png

Paste_Image.png

Paste_Image.png
那么我們基本步驟就是這樣的,接收到一個消息,然后進(jìn)行邏輯判斷,判斷消息類型,然后給予回復(fù),就像下面這樣:
public function responseMsg()
{
//get post data, May be due to the different environments
// $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
$postStr = file_get_contents("php://input");
//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;
$time = time();
$textTpl = null; // xml拼接
$type = $postObj->MsgType; // 信息類型
// 文本消息
if ( $type == "text" ) {
$keyword = trim($postObj->Content); // 發(fā)送過來的消息
$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>";
$msgType = "text";
$contentStr = "你好!";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
}
示例

Paste_Image.png
闡述
微信服務(wù)只負(fù)責(zé)轉(zhuǎn)發(fā)消息,將用戶消息轉(zhuǎn)發(fā)給我們的服務(wù)器,將我們的服務(wù)器的返回的消息轉(zhuǎn)發(fā)給用戶。
信息的處理邏輯還在我們的自己服務(wù)器上,所以說,最大的發(fā)揮空間還是在我們這里。你可以調(diào)用任何接口,比如人臉識別接口face++、百度API、天氣API等等。