Commit ca0fc3f0 authored by 734642908@qq.com's avatar 734642908@qq.com

添加微信调用接口

parent ba669288
......@@ -5,3 +5,4 @@
Application/Runtime
%SystemDrive%/
dump.rdb
uploadfile
\ No newline at end of file
<?php
namespace Home\Controller;
use Think\Controller;
/*
* 微信处理控制器
*
*/
class WechatController extends BaseController
{
public $appid = 'wx27a7a5c8d80d2cd8';
public $secret = 'a9dc1534c5985c9ad34d8b6fd3f7b2d5';
//小程序消息回复
public function respondMsg()
{
if (isset($_GET['echostr'])) {
$this->valid();
}else{
if($this->checkSignature()) {
$postStr = file_get_contents("php://input");
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$MsgType = $postObj->MsgType;
$keyword = trim($postObj->Content);
$time = time();
$token = $this->getToekn();
$token = json_decode($token,true)['access_token'];
$posturl = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$token;
/*$data = '{
"touser":"'.$fromUsername.'",
"msgtype":"text",
"text":
{
"content":"再见"
}
}';*/
$data = '{
"touser":"'.$fromUsername.'",
"msgtype":"image",
"image":
{
"media_id":"88DSz9hHd-7fc_PEZg9NH-O2bl7Jyp0fN-CRuCE0hxbWA3xzLuyUPNb2YxWrSF_2"
}
}';
//$data = json_encode($data);
$re = $this->curlPost($posturl,$data,true);
}
} else {
echo 'bad request';
}
}
}
public function getToekn() {
$filepath = 'uploadfile/wechatToken.txt';
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->secret;
//检查access_token是否过期
if(file_exists($filepath)) {
$fileTime = filemtime($filepath);
if(($fileTime+7000) < time()) {
$token = file_get_contents($url);
file_put_contents($filepath,$token);
} else {
$token = file_get_contents($filepath);
}
} else {
$token = file_get_contents($url);
file_put_contents($filepath,$token);
}
return $token;
}
//o1SKVdH2mEU_7FpCo3xdQ-kjVp94eesikR3lU9rkUoqCTvTHZhSxDsw9PtSB38Jf
protected function curlPost($url,$data='',$is_ssl = false) {
$ch = curl_init();
// 配置
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
if ($is_ssl) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_SSLVERSION, 1); // 设定SSL版本,微信时候使用1,否则报错
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
// 执行
$re = curl_exec($ch);
// 关闭
curl_close($ch);
return $re;
}
protected function valid() {
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
header('content-type:text');
echo $echoStr;
exit;
}
}
protected function checkSignature() {
$signature = isset($_GET["signature"]) ? $_GET["signature"] : '';
$timestamp = isset($_GET["timestamp"]) ? $_GET["timestamp"] : '';
$nonce = isset($_GET["nonce"]) ? $_GET["nonce"] : '';
$token = 'lewan';
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment