kefucontroller base

This commit is contained in:
aozhiwei 2019-01-11 14:49:06 +08:00
parent 1f94592e72
commit 784fa45415
3 changed files with 100 additions and 55 deletions

View File

@ -0,0 +1,18 @@
<?php
return array(
array(
'instance_id' => 1,
'host' => '127.0.0.1',
'port' => 3306,
'user' => 'root',
'passwd' => 'keji178',
),
array(
'instance_id' => 2,
'host' => '127.0.0.1',
'port' => 3306,
'user' => 'root',
'passwd' => 'keji178',
),
);

View File

@ -0,0 +1,16 @@
<?php
return array(
array(
'instance_id' => 1,
'host' => '127.0.0.1',
'port' => 6379,
'passwd' => ''
),
array(
'instance_id' => 2,
'host' => '127.0.0.1',
'port' => 6379,
'passwd' => ''
),
);

View File

@ -1,29 +1,28 @@
<?php <?php
define("TOKEN", ""); //填写自己的token define("TOKEN", "kingsome");
define("MSG_KEY", 'MzebhFcmcIFT85xOr5TnrDVeLx5HMbpgAl5gw2PVvhX');
define("SHAOWEI_APPID", 'wxfa9c8ea6a55f00d0');
define("PRIVATE_KEY", '999712592d00ad2f75b04421e030ba04');
class KefuController { class KefuController {
public function index(){
} public function checkServer() // 校验服务器地址URL
{
public function checkServer(){ // 校验服务器地址URL
echo('ssss');
if ( isset($_GET['echostr'])) { if ( isset($_GET['echostr'])) {
$this->valid(); $this->valid();
} else { } else {
$this->responseMsg(); $this->responseMsg();
} }
} }
public function valid() public function valid()
{ {
$echoStr = $_GET ["echostr" ]; $echoStr = $_GET['echostr'];
if ( $this->checkSignature()){ if ( $this->checkSignature()){
header ('content-type:text' );
echo $echoStr; echo $echoStr;
exit; exit;
} else { } else {
@ -31,17 +30,22 @@ class KefuController {
exit; exit;
} }
} }
private function checkSignature() private function checkSignature()
{ {
$signature = $_GET["signature"]; $signature = $_GET["signature"];
$timestamp = $_GET["timestamp"]; $timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"]; $nonce = $_GET["nonce"];
$token = TOKEN; $token = TOKEN;
error_log('token:' . $token);
$tmpArr = array ( $token , $timestamp , $nonce ); $tmpArr = array ( $token , $timestamp , $nonce );
sort( $tmpArr , SORT_STRING); sort( $tmpArr , SORT_STRING);
$tmpStr = implode( $tmpArr ); $tmpStr = implode( $tmpArr );
$tmpStr = sha1($tmpStr); $tmpStr = sha1($tmpStr);
error_log('tmpStr:' . $tmpStr);
error_log('signature:' . $signature);
if ( $tmpStr == $signature ){ if ( $tmpStr == $signature ){
return true ; return true ;
@ -49,15 +53,13 @@ class KefuController {
return false ; return false ;
} }
} }
public function responseMsg() public function responseMsg()
{ {
$postStr = $GLOBALS ["HTTP_RAW_POST_DATA" ]; $postStr = $GLOBALS ["HTTP_RAW_POST_DATA" ];
if (! empty ( $postStr ) && is_string ( $postStr )){ if (! empty ( $postStr ) && is_string ( $postStr )){
// 禁止引用外部xml实体
//libxml_disable_entity_loader(true);
//$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$postArr = json_decode( $postStr , true ); $postArr = json_decode( $postStr , true );
if (! empty ( $postArr ['MsgType']) && $postArr ['MsgType'] == 'text'){ // 文本消息 if (! empty ( $postArr ['MsgType']) && $postArr ['MsgType'] == 'text'){ // 文本消息
$fromUsername = $postArr ['FromUserName']; // 发送者openid $fromUsername = $postArr ['FromUserName']; // 发送者openid
@ -89,11 +91,13 @@ class KefuController {
); );
$json = json_encode( $data ,JSON_UNESCAPED_UNICODE); // php5.4+ $json = json_encode( $data ,JSON_UNESCAPED_UNICODE); // php5.4+
$access_token = $this -> get_accessToken(); $access_token = $this->getAccessToken();
/* /*
* POST发送https请求客服接口api * POST发送https请求客服接口api
*/ */
$url = " https :// api . weixin . qq . com / cgi - bin / message / custom / send ? access _ token = ". $access_token ; $url = " https :// api . weixin . qq . com / cgi - bin / message / custom / send ? access _ token = ". $access_token ;
// 以'json'格式发送post的https请求 // 以'json'格式发送post的https请求
$curl = curl_init(); $curl = curl_init();
curl_setopt( $curl , CURLOPT_URL, $url ); curl_setopt( $curl , CURLOPT_URL, $url );
@ -122,18 +126,25 @@ class KefuController {
exit ; exit ;
} }
} }
/* 调用微信api获取access_token有效期7200s -xzz0704 */ /* 调用微信api获取access_token有效期7200s -xzz0704 */
public function get_accessToken(){ public function getAccessToken(){
/* 在有效期直接返回access_token */ /* 在有效期直接返回access_token */
if (S('access_token' )){ if (S('access_token' )){
return S('access_token' ); return S('access_token' );
} }
/* 不在有效期重新发送请求获取access_token */ /* 不在有效期重新发送请求获取access_token */
else { else {
$url = ' https :// api . weixin . qq . com / cgi - bin / token ? grant _ type = client _ credential & appid = wx6056 ** **& secret = 30e46f3ef07b **** ' ; $appid = SHAOWEI_APPID;
$result = curl_get_https( $url ); $appkey = PRIVATE_KEY;
$res = json_decode($result , true ); // json字符串转数组 $url = "https://api.weixing.qq.com/cgi-bin/token?;grant_type=client_credential&appid=$appid&secret=$appkey";
$response = '';
if (!phpcommon\HttpClient::get($url, $params, $response)) {
phpcommon\sendError(ERR_RETRY, '系统繁忙');
return;
}
$res = json_decode($response, true );
if ( $res ){ if ( $res ){
S( 'access_token', $res ['access_token'],7100 ); S( 'access_token', $res ['access_token'],7100 );
return S('access_token' ); return S('access_token' );