php启动服务器配置
第⼀步:把wxtokenvalid.php⽂件放到项⽬根⽬录,token值改成⾃⼰的,就是下⾯第⼆步填写的token值<?php
header('Content-type:text');
define("TOKEN", "123456"); //填写⾃⼰的token值
$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;
}
}
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"];
if (!empty($postStr)){
$this->logger("R ".$postStr);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
return $result;
}
private function transmitText($object, $content)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($textTpl, $object->FromUserName,$object->ToUserName, time(), $content);
return $result;
}
private function transmitNews($object, $arr_item)
{
if(!is_array($arr_item))
return;
$itemTpl = " <item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
";
$item_str = "";
foreach ($arr_item as $item)
$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
$newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>服务器
</xml>";
$result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item));
return $result;
}
private function transmitMusic($object, $musicArray)
{
$itemTpl = "<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>";
$item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']); $textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
$item_str
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
return $result;
}
private function logger($log_content)
{
if(isset($_SERVER['HTTP_APPNAME'])){ //SAE
sae_set_display_errors(false);
sae_debug($log_content);
sae_set_display_errors(true);
}else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL
$max_size = 10000;
$log_filename = "l";
if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND);
}
}
/*
* 分享配置
*/
//curl获取请求⽂本内容
private function get_curl_contents($url, $method = 'GET', $data = array())
{
if ($method == 'POST') {
//使⽤crul模拟
$ch = curl_init();
//禁⽤htt<a href="/fw/photo.html" target="_blank">ps</a>
'<a href="/tags.php/curl_setopt/" target="_blank">curl_setopt</a>';
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//允许请求以⽂件流的形式返回
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_DNS_CACHE_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch); //执⾏发送
curl_close($ch);
} else {
if (ini_get('allow_<a href="/tags.php/fopen/" target="_blank">fopen</a>_url') == '1') {
$result = file_get_contents($url);
} else {
//使⽤crul模拟
$ch = curl_init();
//允许请求以⽂件流的形式返回
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//禁⽤https
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch); //执⾏发送
curl_close($ch);
}
}
return $result;
}
/*
* 获取access_token
*/
private function getAccessToken()
{
if ($_SESSION['access_token']) {
return $_SESSION['access_token'];
} else {
$AppID = 'wxc7ea3c2f37093ca6'; //AppID(应⽤ID)
$AppSecret = 'a3b01e59b989455ec3d396c57b4e6c80'; //应⽤内秘钥
$url = "api.weixin.qq/cgi-bin/token?grant_type=client_credential&appid=$AppID&secret=$AppSecret"; // dump($url);
$res = $this->get_curl_contents($url);
$res = json_decode($res, true);
//把获取到的access_token存起来,有效期是7200s;
$_SESSION['access_token']= $res['access_token'];
return $res['access_token'];
}
}
}
>
第⼆步:登录官⽹,启动服务器配置
url填访问到wxtokenvalid.php ⽂件的路径
第三步:启动了服务器配置,那么后台就不能进⾏⾃动回复操作,可以在wxtokenvalid.php⾥进⾏设置

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。