위챗 관심 자동 회신 메시지
<?php
// 2014.09.26
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if(!isset($_GET["echostr"])){
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}
class wechatCallbackapiTest
{
/* */
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
/* */
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
$result = self::receivemsg($postObj, $RX_TYPE);
self::log($postObj);
}else {
echo "";
exit;
}
}
/* */
public function log($postObj, $content=''){
@$fp = fopen("log.html","a");
if(empty($content)){
$fileData = ' :'.date('Y-m-d H:i:s', time()).' :'.$postObj->MsgType.' :'.$postObj->Event.' :'.$postObj->ToUserName.' :'.$postObj->FromUserName.'</br>';
}else{
$fileData = ' :'.date('Y-m-d H:i:s', time()).' :'.$content.'</br>';
}
fwrite($fp,$fileData);
fclose($fp);
}
/*
* :$postObj->MsgType
* :$postObj->Event
* :$postObj->ToUserName
* OpenID:$postObj->FromUserName
*/
private function receivemsg($postObj, $RX_TYPE='text'){
$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";
if($postObj->Event == "subscribe"){
$keyword = 'content_subscribe';
}elseif($postObj->Event == "unsubscribe"){
$keyword = 'content_unsubscribe';
}else{
$keyword = trim($postObj->Content);
if(empty($keyword)){
$keyword = 'content_kong';
}
}
$url = "http://*****/msg.php?keyword=$keyword";
$contentStr = file_get_contents($url);
$contentStr = $contentStr.','.$postObj->MsgType;
$resultStr = sprintf($textTpl, $postObj->FromUserName, $postObj->ToUserName, time(), $msgType, $contentStr);
echo $resultStr;
}
/* */
private function checkSignature()
{
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
위챗의 인터페이스 파일 wxsample.php
<?php
// 2014.09.26
header("Content-type: text/html; charset=utf-8");
$keyword = $_GET["keyword"];
$wechatObj = new msg();
$wechatObj->index($keyword);
class msg
{
public function index($keyword)
{
$contentStr = "Welcome to wechat world!";
if($keyword == '1'){
$contentStr = ' ';
}elseif($keyword == '2'){
$contentStr = ' ';
}elseif($keyword == ' '){
$contentStr = '222222';
}elseif($keyword == 'content_subscribe'){
$contentStr = ' ';
}elseif($keyword == 'content_unsubscribe'){
$contentStr = '';
}
echo $contentStr;
}
}
키워드를 통해 다른 정보 파일 msg를 되돌려줍니다.php
:2014-09-26 23:10:14 :text : : :</br>
로그 파일, log.html 방면에서 인터페이스를 기록하는 문제
코드 주소 및 설명:
http://www.yundahai.com/php/liang_8_50_1.html
http://www.yundahai.com/php/liang_8_49_1.html
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
위챗 애플릿 사진 코드 업로드 실례 분석 실현종이에 구덩이를 담다 내가 이 기능을 실현하기 전에 나는 어떻게 이 구덩이에서 기어올랐는지 말했다. 파일 백엔드의 인터페이스를 업로드하는 매개 변수는 String 형식입니다. 프론트 데스크톱의 매개 변수: 그러나 이...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.