လက်ရှိတည်နေရာ: ပင်မစာမျက်နှာ> နောက်ဆုံးရဆောင်းပါးများစာရင်း> WeChat Public Account Template ၏ function ကိုပေးပို့ခြင်းဆိုင်ရာမက်ဆေ့ခ်ျကိုအကောင်အထည်ဖော်ရန်သင်ခန်းစာ

WeChat Public Account Template ၏ function ကိုပေးပို့ခြင်းဆိုင်ရာမက်ဆေ့ခ်ျကိုအကောင်အထည်ဖော်ရန်သင်ခန်းစာ

M66 2025-07-09

WeChat Public Account Template ၏ function ကိုပေးပို့ခြင်းဆိုင်ရာမက်ဆေ့ခ်ျကိုအကောင်အထည်ဖော်ရန်သင်ခန်းစာ

WeChat Public Accounts ၏လူကြိုက်များမှုတိုးပွားလာခြင်းနှင့်အတူကုမ္ပဏီများနှင့် developer များပိုမိုများပြားလာခြင်းနှင့်အတူ Worchat Platform ၏လျှောက်လွှာဖွံ့ဖြိုးတိုးတက်မှုအတွက်စတင်အာရုံစိုက်လာကြသည်။ ၎င်းတို့အနက် template messages များသည်နိုင်ရန်သတိပေးချက်များ, status updates များ, system notifications နှင့်အခြားအခြေအနေများတွင်ကျယ်ပြန့်စွာအသုံးပြုသောထိရောက်သောအသိပေးချက်နည်းလမ်းဖြစ်သည်။ ဤဆောင်းပါးသည် PHP မှတစ်ဆင့် WeChat Public Accountate Messages ၏ပို့ခြင်းဆိုင်ရာမက်ဆေ့ခ်ျများပေးပို့ခြင်းကိုမည်သို့အကောင်အထည်ဖော်ရမည်ကိုမိတ်ဆက်ပေးပြီးလျင်မြန်စွာစတင်ရန် developer များအားလွယ်ကူချောမွေ့စေရန်အပြည့်အဝကုဒ်များကိုဖြည့်ဆည်းပေးသည်။

ပို့ခြင်း template ကိုမက်ဆေ့ခ်ျကိုအကောင်အထည်ဖော်ရန်အတွက်လိုအပ်ချက်

ဖွံ့ဖြိုးရေးလုပ်ငန်းစဉ်အတွင်း template message ပေးပို့ခြင်းကိုသိရှိရန်အောက်ပါအခြေအနေများကိုဖြည့်ဆည်းရမည်။

  • အသိအမှတ်ပြု WeChat တရားဝင်အကောင့် (စာရင်းပေးသွင်းနံပါတ်, 0 န်ဆောင်မှုနံပါတ်သို့မဟုတ်စီးပွားရေးနံပါတ်များကိုရရှိနိုင်ပါသည်);
  • တရားဝင်အကောင့်၏ appid နှင့် appsecret ကို ရယူပါ။
  • WeChat Public Platform တွင် template မက်ဆေ့ခ်ျများကိုလျှောက်ထားပြီးသက်ဆိုင်ရာ template ID ကိုရယူပါ။

ဝင်ရောက်ခွင့်ကိုရယူပါ

access token သည် WeChat API ကိုခေါ်ဆိုသည့်အထောက်အထားတစ်ခုဖြစ်ပြီးတရားဝင်မှုကာလရှိသည်။ developer များသည် API မှတဆင့်တိုကင်ကိုရယူရန်နှင့်၎င်းကိုသက်တမ်းကုန်ဆုံးပြီးနောက်ပြန်လည်ရယူရန်လိုအပ်သည်။

 function getAccessToken($appId, $appSecret) {
    $apiUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appId . "&secret=" . $appSecret;
    $response = file_get_contents($apiUrl);
    $result = json_decode($response, true);

    // စစ်ဆေးကြည့်ရှုAccess Tokenကခိုင်လုံသောသို့မဟုတ်မဖြစ်ပါတယ်
    if (isset($result['access_token'])) {
        return $result['access_token'];
    } else {
        // အမှားများကိုကိုင်တွယ်
        return false;
    }
}

$accessToken = getAccessToken($appId, $appSecret);

Template Message ကိုပို့ပါ

Access Token ကိုအောင်မြင်စွာရယူပြီးနောက်တွင် developer သည် WeChat interface မှတစ်ဆင့် template message များကိုပို့နိုင်သည်။ အောက်ပါတို့သည်တိကျသောအကောင်အထည်ဖော်မှုဖြစ်သည်။

 function sendTemplateMessage($accessToken, $openId, $templateId, $data) {
    $apiUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $accessToken;

    $postData = array(
        'touser' => $openId,
        'template_id' => $templateId,
        'data' => $data
    );

    $jsonData = json_encode($postData);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiUrl);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
    $response = curl_exec($ch);
    curl_close($ch);

    // ပြန်လာသော interface တုံ့ပြန်မှု
    return $response;
}

// နမူနာ template မက်ဆေ့ခ်ျကိုဒေတာ
$templateData = array(
    'first' => array('value' => 'မင်္ဂလာပါ', 'color' => '#173177'),
    'keyword1' => array('value' => 'Template Message', 'color' => '#173177'),
    'keyword2' => array('value' => '2020-01-01', 'color' => '#173177'),
    'remark' => array('value' => 'သင့်ရဲ့အသုံးပြုမှုအတွက်ကျေးဇူးတင်ပါတယ်', 'color' => '#173177')
);

$response = sendTemplateMessage($accessToken, $openId, $templateId, $templateData);

// ပို့ခြင်းရလဒ်ဖြစ်စဉ်ကို
$result = json_decode($response, true);
if ($result['errcode'] == 0) {
    echo "Template Message发送成功!";
} else {
    echo "Template Message发送失败,ကျေးဇူးပြု. နောက်မှထပ်ကြိုးစားပါ。အမှားသတင်းစကား:" . $result['errmsg'];
}

အထက်ပါကုဒ်တွင် sendtemplattemessemessemessage function သည် parameterscing လေးခုကိုလက်ခံသည်။ Token, User OpenID, Template ID နှင့် template ပါ 0 င်သည့်အကြောင်းအရာများကိုရယူပါ။ ၎င်းတို့အနက် $ ဒေတာများသည် timplate ရှိ variable များနှင့်သက်ဆိုင်သောတန်ဖိုးများရေးထားသည့်စာရင်းတစ်ခုဖြစ်သည်။

အကျဉ်းချုပ်

ဤဆောင်းပါးတွင်ဖော်ပြထားသောနည်းလမ်းများမှတစ်ဆင့် developer များက PPP ကို ​​အသုံးပြု. PHP ကို ​​အသုံးပြု. PHP ကိုသုံးနိုင်သည်။ Template Message များသည်အသုံးပြုသူအတွေ့အကြုံများကိုတိုးတက်စေရုံသာမက 0 န်ဆောင်မှုဆိုင်ရာအသိပေးချက်များနှင့်သတင်းအချက်အလက်တွန်းအားပေးခြင်းစသည့်လုပ်ငန်းများကိုထိရောက်စွာပြီးပြည့်စုံစွာပြည့်စုံစေရန်ကူညီပေးနေသည်။

ဤဆောင်းပါးရှိနိဒါန်းနှင့်နမူနာကုဒ်သည်အမှန်တကယ်ဖွံ့ဖြိုးတိုးတက်မှုအတွက်သင့်အတွက်အထောက်အကူဖြစ်လိမ့်မည်ဟုကျွန်ုပ်မျှော်လင့်ပါသည်။