```
<?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圖
- 常見問題