- 小程序与公众号同一主体
- 微信小程序用户已关注公众号
public function send_temp($openid, $arr)
{
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx741d096c7ee28dcb&secret=560de08ad2c6c023063d23f099ff3dca';
$res = $this->curl_get($url);
$data = json_decode($res, true);
if (isset($data['access_token'])) {
$send_url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token=' . $data['access_token'];
$send_data = [
'touser' => $openid,
'mp_template_msg' => [
'appid' => '',
'template_id' => '',
'url' => '',
'miniprogram' => [
'appid' => '',
'path' => 'pages/index/index',
],
'data' => $arr
],
];
$send_data_decode = json_encode($send_data, true);
return $res_send = $this->sendCmd($send_url, $send_data_decode);
}
}
public function sendCmd($url, $data)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$tmpInfo = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Errno' . curl_error($curl);
}
curl_close($curl);
return $tmpInfo;
}
public function curl_get($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
return $data;
}