1. まず、WeChat 公式アカウント -> 開発者ツール -> パブリック プラットフォーム テスト アカウントにログインし、正常に設定するには、内部の TOKEN がファイルに定義されている TOKEN と一致している必要があります。
2. リクエストが WeChat サーバーから送信されたかどうかを確認します
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;
}
}
3.公式アカウントにメッセージを送信し、返信があるか確認する
コードは以下のように表示されます
if(!empty($keyword))
{
$msgType = "text";
$contentStr = $this->word($keyword);
// $contentStr ="we come";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}else {
echo "";
exit;
}
}
// 关键字
public function word($keyword){
if($keyword === '天气'){
return $contentStr ="今天天气有雨!";
}else if($keyword === '你好'){
return $contentStr ="你好!有什么需要帮助?";
}else{
return $contentStr ="sorry,I can not help you。";
}
4. 重要な役割を果たすコードは次のとおりです
define("TOKEN", "weixin");
$echoStr = $_GET["echostr"];
echo $echoStr;