php语言公众号模板怎么发送,PHP发送公众号模板消息

/*

* 模板消息发送,电脑端测试时需要手动填写openid

* 微信端会自动获取当前openid发送无需填写

*/

header("Content-type: text/html; charset=utf-8");

date_default_timezone_set("Asia/Shanghai");

$temp = new template();

if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {

if (!empty($_GET['code'])) {

$openid = $temp->GetOpenid();

if ($openid) {

$result = $temp->sendMsg($openid);

if ($result['errmsg'] == 'ok') {

echo '';

} else {

echo '发送失败!' . $result['errmsg'];

}

} else {

echo '';

}

} else {

$temp->GET_Code();

}

} else {

echo '';

$openid='orpdAwprTZvFaUwPDfW4WSanlZIk';

$result = $temp->sendMsg($openid);

if ($result['errmsg'] == 'ok') {

echo '';

} else {

echo '发送失败!' . $result['errmsg'];

}

}

class template {

public $appid = "xxxxxxxx";

public $secret = "xxxxxxxxxxxx";

public function sendMsg($openid) {

$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->secret;

//获取access_token

$resultData = $this->https_getRequest($url);

$access_token = $resultData["access_token"];

//$openid = 'orpdAwprTZvFaUwPDfW4WSanlZIk';

$templateid = 'hpTqa6GaczDqeIzuvfWqOiVHNm080yXH1yGnJKHC6jQ';

$title = "模板消息发送成功!";

$data1 = "测试模板消息发送";

$data2 = "开发员!";

$data3 = date("Y年m月d日 H:i", time());

$remark = "当前为测试模板可任意指定内容,但实际上正式帐号的模板消息,只能从模板库中获得";

$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $access_token;

/*

* @miniprogram 跳小程序所需数据,不需跳小程序可不用传该数据

* @appid appid必须与发模板消息的公众号是绑定关联关系)

* @pagepath 所需跳转到小程序的具体页面路径,支持带参数,(示例index?foo=bar)

*/

$data = '{

"touser":"' . $openid . '",

"template_id":"' . $templateid . '",

"url":"http://www.wxwytime.com/index.php?g=Wap&m=Index&a=search&token=lypzib1465694885",

"miniprogram":{

"appid":"",

"pagepath":""

},

"data":{

"first": {

"value":"' . $title . '",

"color":"#7A378B"

},

"keyword1":{

"value":"' . $data1 . '",

"color":"#787878"

},

"keyword2": {

"value":"' . $data2 . '",

"color":"#787878"

},

"keyword3": {

"value":"' . $data3 . '",

"color":"#787878"

},

"remark":{

"value":"' . $remark . '",

"color":"#C5C1AA"

}

}

}';

$send = $this->https_request($url, $data);

$send = json_decode($send, true);

return $send;

}

//发起获得code值链接

public function GET_Code() {

$get_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->appid;

$url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

header("location:" . $get_url . "&redirect_uri=" . $url . "&response_type=code&scope=snsapi_base&state=1#wechat_redirect");

}

//获取用户openid

public function GetOpenid() {

$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->appid . '&secret=' . $this->secret . '&code=' . $_GET['code'] . '&grant_type=authorization_code';

$json_obj = $this->https_getRequest($get_token_url);

// $access_token = $json_obj['access_token'];

return $json_obj['openid'];

}

/*

* 获取数据

*/

public function https_getRequest($url) {

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($ch);

curl_close($ch);

$jsonData = json_decode($output, true);

return $jsonData;

}

//通用数据处理方法发送模板消息

public function https_request($url, $data = null) {

$curl = curl_init();

curl_setopt($curl, CURLOPT_URL, $url);

curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

if (!empty($data)) {

curl_setopt($curl, CURLOPT_POST, 1);

curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

}

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($curl);

curl_close($curl);

return $output;

}

}

要在 PHP发送微信公众号模板消息,首先需要在微信公众平台中创建一个模板消息并获取模板 ID。接下来,您需要在 PHP 中使用 cURL 库向微信 API 发送 POST 请求,以便将模板消息发送给用户。以下是示例代码: ```php $access_token = 'YOUR_ACCESS_TOKEN'; $template_id = 'YOUR_TEMPLATE_ID'; $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token; $data = array( 'touser' => 'OPENID', 'template_id' => $template_id, 'data' => array( 'first' => array('value' => 'Hello, world!'), 'keyword1' => array('value' => 'Keyword 1'), 'keyword2' => array('value' => 'Keyword 2'), 'remark' => array('value' => 'This is a remark.') ) ); $data_string = json_encode($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Content-Length: ' . strlen($data_string) )); $result = curl_exec($ch); curl_close($ch); echo $result; ``` 上述代码中,`$access_token` 是您的公众号访问令牌,`$template_id` 是您创建的模板消息的 ID。您需要将 `OPENID` 替换为要接收模板消息的用户的 OpenID。`$data` 数组包含模板消息的详细信息,其中 `first`、`keyword1`、`keyword2` 和 `remark` 分别对应模板消息中的不同部分。最后,使用 cURL 库将 `$data` 数组作为 JSON 字符串发送到微信 API,然后解析响应以查看是否成功。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值