1、第一个问题,自己的服务器应该上传哪些代码,我在网上找了一个能用的,如下:
(1)index.php
<?php
date_default_timezone_set("Asia/Shanghai");
define("TOKEN", "weixin");
require_once("Utils.php");
Utils::traceHttp();
$wechatObj = new wechatCallBackapiTest();
if (isset($_GET["echostr"])){
$wechatObj->valid();
}else {
$wechatObj->responseMsg();
}
class wechatCallBackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if ($this->checkSignature()){
echo $echoStr;
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;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
Utils::logger($postStr);
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
switch($RX_TYPE){
case "event":
$result = $this->receiveEvent($postObj);
break;
default:
$result = "unknow msg type:".$RX_TYPE;
break;
}
Utils::logger($result, '公众号');
echo $result;
}else{
echo "";
exit;
}
}
private function receiveEvent($object)
{
switch ($object->Event){
case "subscribe":
$content = "欢迎关注微微一笑很倾城";
break;
default:
$content = "";
break;
}
$result = $this->transmitText($object, $content);
return $result;
}
private function transmitText($object, $content)
{
$xmlTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime><![CDATA[%s]]></CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);
return $result;
}
}
(2)Utils.php
<?php
class Utils
{
public static function traceHttp()
{
$content = date('Y-m-d H:i:s')."\n\rremote_ip:".$_SERVER["REMOTE_ADDR"].
"\n\r".$_SERVER["QUERY_STRING"]."\n\r\n\r";
$max_size = 1000;
$log_filename = "./query.xml";
if (file_exists($log_filename) and (abs(filesize($log_filename))) > $max_size){
unlink($log_filename);
}else {
}
file_put_contents($log_filename, $content, FILE_APPEND);
}
public static function logger($log_content, $type = '用户')
{
$max_size = 3000;
$log_filename = "./log.xml";
if (file_exists($log_filename) and (abs(filesize($log_filename)) >
$max_size)) {
unlink($log_filename);
}
file_put_contents($log_filename, "$type ".date('Y-m-d H:i:s')."\n\r".$log_content."\n\r",
FILE_APPEND);
}
}
(3)output_query.php
<?php
@header('Content-type: text/plain;charset=UTF-8');
$filepath = './query.xml';
readfile($filepath);
(4)output_log.php
<?php
@header('Content-type: text/plain;charset=UTF-8');
$filepath = './log.xml';
readfile($filepath);
2、代码上传了,但是还是配置失败
原因:(1)服务器没有安装apache,这个自己去网上百度,一大堆;(2)没有安装php运行环境,附个链接:点击打开链接
(3)路径要填写对,前面要加http://或者https://
改完这些测试,很好用的一个debug:点击打开链接