Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
ylc.llewan.com
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
llewan
ylc.llewan.com
Commits
ca0fc3f0
Commit
ca0fc3f0
authored
Sep 19, 2018
by
734642908@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加微信调用接口
parent
ba669288
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
138 additions
and
2 deletions
+138
-2
.gitignore
.gitignore
+3
-2
WechatController.class.php
Application/Home/Controller/WechatController.class.php
+135
-0
No files found.
.gitignore
View file @
ca0fc3f0
...
@@ -4,4 +4,5 @@
...
@@ -4,4 +4,5 @@
.project
.project
Application/Runtime
Application/Runtime
%SystemDrive%/
%SystemDrive%/
dump.rdb
dump.rdb
\ No newline at end of file
uploadfile
\ No newline at end of file
Application/Home/Controller/WechatController.class.php
0 → 100644
View file @
ca0fc3f0
<?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
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment