php 微信小程序 統一服務消息 訂閱消息

先看騰訊文檔:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/uniform-message/uniformMessage.send.html

通過文檔可以看出,需要攜帶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;
}

 

發佈了66 篇原創文章 · 獲贊 30 · 訪問量 26萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章