Index.php
5.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace app\api\controller;
use app\api\controller\inspection\Task;
use app\common\controller\Api;
use fast\Http;
/**
* 首页接口
*/
class Index extends Api
{
protected $noNeedLogin = ['*'];
protected $noNeedRight = ['*'];
/**
* 首页
*
*/
public function index()
{
$this->success('请求成功');
}
function getAccessToken() {
$appId = "wx58ceff4e93cfc523";
$appSecret = "baf744d21875280a5e98611f66adaf91";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appId}&secret={$appSecret}";
$result = json_decode(file_get_contents($url), true);
return $result["access_token"] ?? null;
}
function createMiniProgramQRCode($accessToken, $path, $width = 430) {
$url = "https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken}";
$data = json_encode([
'path' => $path,
'width' => $width
]);
$options = [
'http' => [
'method' => 'POST',
'header' => 'Content-type:application/json',
'content' => $data,
],
];
//POST参数
$result = $this->httpRequest($url,$data,"POST");
$file_name=md5(rand(1000,9999).time()). '.png';
$filename= 'uploads/' . $file_name ;
$ret = file_put_contents($filename, $result, true);
$task=new Task();
$res=$task->fileUpload($file_name);
return $res;
}
//把请求发送到微信服务器换取二维码
public function httpRequest($url,$data='',$method='GET'){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
if($method=='POST')
{
curl_setopt($curl, CURLOPT_POST, 1);
if ($data !='')
{
curl_setopt($curl, CURLOPT_POSTFIELDS,$data);
}
}
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
public function test(){
$accessToken = $this->getAccessToken();
if ($accessToken) {
$path = 'pages/index/index'; // 小程序内的页面路径
$qrCodeData = $this->createMiniProgramQRCode($accessToken, $path);
print_r($qrCodeData);return;
return $qrCodeData;
}
}
/***
* 公众号推送审核信息给经纪人
*/
public function senWxmsgToAgentUser($titdesc="", $remark="")
{ $appId = 'wxb7dd0c03865a94e0';
$appSecret = '6af75a6fb8211da45631630e34769f82';
$openId = 'o5ABA4yWDTLRTf3LkBMMHoV7XOvQ';
$unionId = 'ojyUX6oNFoQMqoCd1JWtfwQs-CeA';
// 获取token
$myurl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appId . "&secret=" . $appSecret;
$json_token = http_request($myurl);
$access_tokens = json_decode($json_token, true);
$accessToken = $access_tokens['access_token'];
// 发送消息的接口地址
$sendMessageUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$accessToken}";
// 构造要发送的消息数据
$data = [
'touser' => $openId,
'msgtype' => 'text',
'text' => [
'content' => '你好,这是一个公众号消息'
]
];
// 对数据进行JSON编码
$jsonData = json_encode($data, JSON_UNESCAPED_UNICODE);
// 发送HTTP POST请求
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $sendMessageUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// 处理响应结果
$result = json_decode($response, true);
if ($result['errcode'] == 0) {
echo "消息发送成功";
} else {
echo "消息发送失败,错误代码:{$result['errcode']},错误信息:{$result['errmsg']}";
}
}
function http_request($url, $data = array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
// 我们在POST数据哦!
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}