微信公众平台接口回复中文乱码

php中文网
发布: 2016-06-06 20:50:45
原创
2134人浏览过

<?php
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg(); 
function youdaofanyi($keyword)
{
	$url = 'http://fanyi.youdao.com/openapi.do?keyfrom=coolrand&key=545706258&type=data&doctype=json&version=1.1&q='.$keyword;
	$f = new SaeFetchurl();
	$output = $f->fetch($url);
	$message = json_decode($output,true);
	$errorcode = $message['errorCode'];
	$phonetic = $message['basic']['$phonetic'];
	$explains1 = $message['basic']['explains']['0'];
	$explains2 = $message['basic']['explains']['1'];
	$explains3 = $message['basic']['explains']['2'];
	$trans = $message['translation'][0];
	return "哈哈"."\n".$trans."\n".$phonetic."\n".$explains1;
}

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)){
    		$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', 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 ))
    		{	
				if((eregi("a",$keyword))){
    			$msgType = "text";
    			$contentStr = youdaofanyi($keyword);
    			$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
    			echo $resultStr;
				}
				else{
					$msgType = "text";
    				$contentStr = "欢迎";
    				$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
    				echo $resultStr;
				}
    		}else{
    			echo "asd";
    		} 
        }else {
    		echo MESS;
    		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.2.2
极品模板多语言企业网站管理系统1.2.2

【极品模板】出品的一款功能强大、安全性高、调用简单、扩展灵活的响应式多语言企业网站管理系统。 产品主要功能如下: 01、支持多语言扩展(独立内容表,可一键复制中文版数据) 02、支持一键修改后台路径; 03、杜绝常见弱口令,内置多种参数过滤、有效防范常见XSS; 04、支持文件分片上传功能,实现大文件轻松上传; 05、支持一键获取微信公众号文章(保存文章的图片到本地服务器); 06、支持一键

极品模板多语言企业网站管理系统1.2.2 0
查看详情 极品模板多语言企业网站管理系统1.2.2

这个问题已被关闭,原因:

回复内容:

<?php
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg(); 
function youdaofanyi($keyword)
{
	$url = 'http://fanyi.youdao.com/openapi.do?keyfrom=coolrand&key=545706258&type=data&doctype=json&version=1.1&q='.$keyword;
	$f = new SaeFetchurl();
	$output = $f->fetch($url);
	$message = json_decode($output,true);
	$errorcode = $message['errorCode'];
	$phonetic = $message['basic']['$phonetic'];
	$explains1 = $message['basic']['explains']['0'];
	$explains2 = $message['basic']['explains']['1'];
	$explains3 = $message['basic']['explains']['2'];
	$trans = $message['translation'][0];
	return "哈哈"."\n".$trans."\n".$phonetic."\n".$explains1;
}

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)){
    		$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', 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 ))
    		{	
				if((eregi("a",$keyword))){
    			$msgType = "text";
    			$contentStr = youdaofanyi($keyword);
    			$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
    			echo $resultStr;
				}
				else{
					$msgType = "text";
    				$contentStr = "欢迎";
    				$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
    				echo $resultStr;
				}
    		}else{
    			echo "asd";
    		} 
        }else {
    		echo MESS;
    		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;
    	}
    }
}
    ?>
登录后复制

代码如上,可是回复中的中文会显示乱码,到底出什么问题了啊

相关标签:
微信app下载
微信app下载

微信是一款手机通信软件,支持通过手机网络发送语音短信、视频、图片和文字。微信可以单聊及群聊,还能根据地理位置找到附近的人,带给大家全新的移动沟通体验,有需要的小伙伴快来保存下载体验吧!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号