Автор оригинала: David Wong.
В этой статье мы делимся конкретным кодом PHP для ответа на изображение для разработки общедоступного номера Wechat для вашей справки. Подробности заключаются в следующем.
Фотоотчет
Случайная функция:
rand(1,10)
Основной код:
$tyep= $postObj->MsgType; $textTpl = ""; if ($tyep=="image") { $a=rand(1,3); switch ($a) { case "1"; B= "Healthy and good relationships, happy marriage"; break; case "2"; $b= "Good luck lines where noble people help each other out of danger"; break; default; B= "Excellent talent, rich knowledge and successful career"; } $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$b); echo $resultStr; } %s 0
Индекс. php-код выглядит следующим образом:
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, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$tyep= $postObj->MsgType;
$textTpl = "
%s
0
";
if ($tyep=="image")
{$a=rand(1,3);
switch ($a)
{case "1";
B= "Healthy and good relationships, happy marriage";
break;
case "2";
$b= "Good luck lines where noble people help each other out of danger";
break;
default;
B= "Excellent talent, rich knowledge and successful career";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$b);
echo $resultStr;
}
}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;
}
}
}
?>Выше приведено все содержание этой статьи. Я надеюсь, что это будет полезно для изучения каждого, и я надеюсь, что вы будете больше поддерживать разработчика.
Оригинал: “https://developpaper.com/photo-response-to-the-development-of-php-wechat-public-number/”