```
<?php
namespace Uzhu\Api\Controller;
use Uzhu\Core\ULog\Logger;
use Uzhu\Core\Weixin\WxXYX;
class weixinKeFuController extends ApiController
{
private $encodingAesKey = "axm2PwFW9BpQtVBr14Ym1mXKMFpBJ7rzrcMqHnvNtjh";
private $access_token = '';
private $token = '';
private $wx;
private $pc;
public function init()
{
$this->wx = new WxXYX();
$this->access_token =$this->wx->getAccessToken();
}
// 校验服务器地址URL
public function checkAction()
{
$echostr = $this->GET->getString('echostr');
try{
if (!empty($echostr)) {
$this->validAction();
} else {
$this->sendCustomerMessageAction();
}
} catch (\Exception $e) {
$this->jsonFailed($e->getMessage());
}
}
public function validAction()
{
$echoStr = $this->GET->getString('echostr');;
if ($this->checkSignatureAction()) {
$logger = Logger::getInstance("wx_kefu");
$logger->info("2哈哈");
header('content-type:text');
echo $echoStr;
$logger->info("微信发送的echostr".var_export($echoStr, true));
die;
} else {
echo $echoStr . '+++' . $this->token;
die;
}
}
//微信验证规则
private function checkSignatureAction()
{
$signature = $this->GET->getInt("signature");
$timestamp = $this->GET->getInt("timestamp");
$nonce = $this->GET->getInt("nonce");
$token = $this->token;
$tmpArr = array (
$token,
$timestamp,
$nonce
);
sort ( $tmpArr, SORT_STRING );
$tmpStr = implode ( $tmpArr );
$tmpStr = sha1 ( $tmpStr );
if ($tmpStr == $signature) {
return true;
} else {
return false;
}
}
public function customerTypingAction()
{
$url = "POST https://api.weixin.qq.com/cgi-bin/message/custom/typing?access_token={$this->access_token}";
$res = $this->wx->request($url, $data = null, $credit = null);
$this->wx->request($url, $data);
}
//获取客服获取的临时素材,仅支持下载图片文件
public function getTempMediaAction()
{
$media_id = '';
$url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token={$this->access_token}&media_id={$media_id}";
}
//发送客服消息给用户
public function sendCustomerMessageAction()
{
$postStr = file_get_contents("php://input");
if (!empty ($postStr) && is_string($postStr)) {
$postArr = json_decode($postStr, true);
if (!empty ($postArr ['MsgType']) && $postArr ['MsgType'] == 'text') { // 文本消息
$toUserName = $postArr ['ToUserName']; // 小程序id
$fromUsername = $postArr ['FromUserName']; // 发送者openid
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
$content = '您好,有什么能帮助你?';
$data = array(
"touser" => $fromUsername,
"msgtype" => "link",
"link" => array(
"title" => "签到领大奖",
"description" => "免费! 免费! 免费!",
"url" => "http://gcj.xyxsaler.zhangliediaozhatian.com/fllow.html",
"thumb_url" => ""
)
);
$json = json_encode($data, JSON_UNESCAPED_UNICODE); // php5.4+
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$this->access_token}";
$this->wx->request($url, $json);
exit();
}
// elseif (! empty ( $postArr ['MsgType'] ) && $postArr ['MsgType'] == 'image') { // 图文消息
// $fromUsername = $postArr ['FromUserName']; // 发送者openid
// $toUserName = $postArr ['ToUserName']; // 小程序id
// $textTpl = array (
// "ToUserName" => $fromUsername,
// "FromUserName" => $toUserName,
// "CreateTime" => time (),
// "MsgType" => "transfer_customer_service"
// );
// $json = json_encode ( $textTpl, JSON_UNESCAPED_UNICODE);
// $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$this->access_token}";
// $this->wx->request($url, $json);
// exit ();
// } elseif ($postArr ['MsgType'] == 'event' && $postArr ['Event'] == 'user_enter_tempsession') { // 进入客服动作
// $fromUsername = $postArr ['FromUserName']; // 发送者openid
// $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
// $content = '您好,有什么能帮助你?';
// $data = array (
// "touser" => $fromUsername,
// "msgtype" => "link",
// "link" => array (
// "title" => "签到领大奖",
// "description" => "免费! 免费! 免费!",
// "url" => "http://gcj.xyxsaler.zhangliediaozhatian.com/fllow.html",
// "thumb_url" => ""
// )
// );
// $json = json_encode ( $data, JSON_UNESCAPED_UNICODE ); // php5.4+
// $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$this->access_token}";
// $this->wx->request($url, $json);
//
// } else {
// exit ( '' );
// }
// } else {
// echo "";
// exit ();
// }
// $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$this->access_token}";
// $data = urldecode(json_encode($data));
// $this->wx->request($url, $data);
}
}
public function uploadTempMediaAction()
{
$url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$this->access_token}&type=TYPE";
}
}
```
- 空白目录
- 安装
- linux下安装nginx
- linux下安装php
- 调试
- windows 安装wamp
- 常用源
- 类型
- Boolean布尔类型
- Integer整型
- Float浮点型
- String字符串
- Array数组
- Object对象
- Resource资源类型
- NULL
- Callback/Callable类型
- 类型转换
- 变量
- 预定义变量
- 常量
- 魔术常量(预定义常量)
- 语法
- 运算符
- 流程控制
- 条件语句
- if
- else
- else if/elseif
- switch
- 循环语句
- while
- do-while
- for
- foreach
- break
- return
- continue
- require
- include
- require_once
- include_once
- 函数
- 内置函数
- 匿名函数
- 自定义函数
- 类与对象
- 属性
- 声明
- public
- protected
- private
- 类常量
- 方法
- 魔术方法
- __construct()
- __destruct()
- __call()
- __callStatic()
- __get()
- __set()
- __isset()
- __unset()
- __sleep()
- __wakeup()
- __toString()
- __invoke()
- __set_state()
- __clone()
- __debugInfo()
- 方法的调用
- ->(对象运算符)
- ::(范围解析操作符)
- 设计模式
- 依赖注入
- call_user_func
- is_callable (
- 创建型模式
- 工厂方法模式
- 抽象工厂模式
- 单例模式
- 建造者模式
- 原型模式
- 结构性模式
- 适配器模式
- 装饰器模式
- 代理模式
- 外观模式
- 桥接模式
- 组合模式
- 享元模式
- 行为型模式
- 策略模式
- 模板方法模式
- 观察者模式
- 迭代子模式
- 责任链模式
- 命令模式
- 备忘录模式
- 访问者模式
- 中介者模式
- 解释器模式
- 六大原则
- uml语言
- 反射
- 类型约束
- 调试
- lamp
- lnmp
- composer
- 工作常用
- UML图
- 常见问题