PHP微信開發之模板訊息回覆

weixin_33755649發表於2018-04-04

參考:http://www.jb51.net/article/87269.htm

程式碼:

<?php
//$ac = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx4e45725489bf95a6&secret=ebdb0802ccb233aec7f744646db895a5');
//$wxt = json_decode($ac,true);

$url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=Ka4dA44aDm2P94j4aWaD24PRdD7HADAd';//access_token改成你的有效值
 
$data = array(
    'first' => array(
        'value' => '您報考的龍門杯比賽有最新訊息',
        'color' => '#FF0000'
    ),
    'keyword1' => array(
        'value' => '三年級一班',
        'color' => '#FF0000'
    ),
    'keyword2' => array(
        'value' => '商丘第一高階中學',
        'color' => '#FF0000'
    ),
    'keyword3' => array(
        'value' => '2018/04/04 15:00~14:45',
        'color' => '#FF0000'
    ),
    'keyword4' => array(
        'value' => '首先恭喜您通過了我們龍門杯的初選,通知您於18年4月20日來商丘教育局南院(商丘市神火大道86號)2樓會議室進行考試。如有疑問,請撥打電話13526859585 13596852458。',
        'color' => '#FF0000'
    ),
    'remark' => array(
        'value' => '請您務必準時到場!',
        'color' => '#FF0000'
    )
);
$template_msg=array('touser'=>'oQJHg1MiLXLuk57BPYEds7QNCS28','template_id'=>'EHUHkPfa2_iJrlDCMDn7SNkGcyHu7BwwsdurOnQgy_Y',"url"=>"http://www.sqess.com/",'topcolor'=>'#FF0000','data'=>$data);
 
$curl = curl_init($url);
$header = array();
$header[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
// 不輸出header頭資訊
curl_setopt($curl, CURLOPT_HEADER, 0);
// 偽裝瀏覽器
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
// 儲存到字串而不是輸出
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// post資料
curl_setopt($curl, CURLOPT_POST, 1);
// 請求資料
curl_setopt($curl,CURLOPT_POSTFIELDS,json_encode($template_msg));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
exit;

完畢。

相關文章