通過文檔可以看出,需要攜帶ACCESS_TOKEN,相關參考在https://blog.csdn.net/mushui0633/article/details/89469209
<?php
$appid='wx你的appid';
$appsecret='bd3你的小程序secret';
$r = file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret");
$data = json_decode($r,true);
$url = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token='.$data['access_token'];
$data = array('touser' => 'ox客戶的openid', //發給誰
'template_id' => 'UU你的小程序統一服務消息ID——hBE',
'page' => 'pages/my/li你的小程序前端頁面地址和參數?id=ggf&shareId=ghf',
'data' => array(
'thing1' => array(
'value' => '$goodTitle',
),
'thing2' => array(
'value' => '$orderPrice'.'元',
),
'thing5' => array(
'value' => '$orderTime',
),
'time3' => array(
'value' => '2019-12-11 18:36',
),
// 這裏如果日期時間格式不對就報錯,比如:{"errcode":47003,"errmsg":"argument invalid! hint: [I1D5la06238630] data.time3.value invalid"}
)
);
$data = json_encode($data);
$result = curl_post($url, $data);
$result = json_decode($result);
if ($result->errcode == '0' && $result->errmsg == 'ok') {
echo $result;
return 'success';
} else {
return '發送失敗';
}
function curl_post($url, $data)
{
$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);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
return $response;
}