php微信公衆號啓動服務器配置

微信公衆號開發者技術文檔參考資料:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319php

 

第一步:把wxtokenvalid.php文件放到項目根目錄,token值改爲本身的,就是下面第二步填寫的token值html

<?phpjson

header('Content-type:text');
define("TOKEN", "123456"); //填寫本身的token值api

$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);app

if($tmpStr == $signature){
return true;
}else{
return false;
}
}curl

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);post

switch ($RX_TYPE)
{
case "event":
$result = $this->receiveEvent($postObj);
break;
case "image":
$result = $this->receiveText($postObj);
break;
case "text":
$result = $this->receiveText($postObj);
break;
}
$this->logger("T ".$result);
echo $result;
}else {
echo "";
exit;
}
}

private function receiveEvent($object)
{
$content = "";
switch ($object->Event)
{
case "subscribe":
$content = "您好!歡迎關注「彭州氣象」官方微信公衆號!「關注就有禮物,點贊送大獎!」活動正在火熱進行中,點擊:http://dwz.cn/79Rlnw 搶大獎!";
break;
case "unsubscribe":
$content = "取消關注";
break;
}
$result = $this->transmitText($object, $content);
// $token = $this->getAccessToken();
$openid = $object->FromUserName;
// $data = file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token."&openid=".$openid."&lang=zh_CN");
// $data = json_decode($data,true);
//// dump($data["errcode"]);
// if($data["errcode"]==42001){
// $_SESSION['access_token']='';
// $token = $this->getAccessToken();
// $data = file_get_contents("https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$token."&openid=".$openid."&lang=zh_CN");
// $data = json_decode($data,true);
// }
// Header("http://pz.cdtq.gov.cn/Preagr/add/openid/".$openid);ui

file_get_contents("http://pz.cdtq.gov.cn/Preagr/add/openid/".$openid);
return $result;
}

//接收文本消息
private function receiveText($object)
{
$keyword = trim($object->Content);
// $content = date("Y-m-d H:i:s",time())."\n技術支持 方倍工做室";
$content = "對於本次活動有如下提醒:\n一、請仔細閱讀活動規則;\n二、全部紅包將在中獎名單公示後3個工做日內統一發送至用戶手中;\n三、發獎前若是取消「彭州氣象」微信公衆號的關注,視爲棄權;\n四、本次活動最終解釋權歸「彭州氣象」微信公衆號全部。\n 查看詳情:http://dwz.cn/79Rlnw";

if(is_array($content)){
if (isset($content[0]['PicUrl'])){
$result = $this->transmitNews($object, $content);
}else if (isset($content['MusicUrl'])){
$result = $this->transmitMusic($object, $content);
}
}else{
$result = $this->transmitText($object, $content);
}

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 = "log.xml";
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 = "https://api.weixin.qq.com/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裏進行設置

相關文章
相關標籤/搜索