公众号PHP模板修改,PHP 实现发送模板消息(微信公众号版)

```

$ACCESS_TOKEN = "";//ACCESS_TOKEN

//openid数组

$touser = [

'ovMzR1HD6LOcbcWLw-Xn9eKOyG1U'

];

//模板消息请求URL

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

//遍历发送微信消息

foreach ($touser as $value) {

$data = getDataArray($value);

$json_data = json_encode($data);//转化成json数组让微信可以接收

$res = https_request($url, urldecode($json_data));//请求开始

$res = json_decode($res, true);

if ($res['errcode'] == 0 && $res['errcode'] == "ok") {

echo "发送成功!
";

}else {

echo "发送失败!
";

}

}

//获取发送数据数组

function getDataArray($value)

{

$data = array(

'touser' => $value, //要发送给用户的openid

'template_id' => "kKPT5I-wyENb81yHtg8WazchUFjsqtyQ1Rlu62lGaQw",//改成自己的模板id,在微信后台模板消息里查看

'url' => "http://ui-china.cn", //自己网站链接url

'data' => array(

'first' => array(

'value' => "标题名称",

'color' => "#000"

),

'keyword1' => array(

'value' => "员工姓名",

'color' => "#f00"

),

'keyword2' => array(

'value' => "2019-1-6",

'color' => "#173177"

),

'keyword3' => array(

'value' => "地点",

'color' => "#3d3d3d"

),

'keyword4' => array(

'value' => "类型",

'color' => "#3d3d3d"

),

'keyword5' => array(

'value' => "备注",

'color' => "#3d3d3d"

),

'remark' => array(

'value' => "1.备注详情\n2.备注详情>>>",

'color' => "#3d3d3d"

),

)

);

return $data;

}

//curl请求函数,微信都是通过该函数请求

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;

}

?>

```

==========================================================

获取openid

这是微信官方文档

1.用户同意授权,获取code

接口地址:https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE#wechat_redirect

appid:公众号appid基础设置里有(必填)

redirect_uri:重定向地址,用于接收code(必填)

response_type:返回类型,请填写code(必填)

scope:应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )(必填)

#wechat_redirect:无论直接打开还是做页面302重定向时候,必须带此参数(必填)

完成参数填写后直接扔进你的自定义菜单栏里,点击跳转url

2.通过code换取网页授权access_token

接口地址:https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code

$code=$request->get("code"); //接收code,这里我用的laravel框架

$url="https://api.weixin.qq.com/sns/oauth2/access_token?appid=appid&secret=secret&code=".$code."&grant_type=authorization_code";

$res=HttpUtils::curl($url, $params = false, $ispost = 0, $https = 1);//此方法为curl发送请求,可联系我要完整代码

$res = (array)json_decode($res); // 返回结果为json,其中包含openid,access_token

appid:公众号appid基础设置里有(必填)

secret:公众号secret基础配置里生成(必填)

code:第一步获取的code(必填)

grant_type:填写为authorization_code(必填)

正确返回的结果:

{ "access_token":"ACCESS_TOKEN",

"expires_in":7200,

"refresh_token":"REFRESH_TOKEN",

"openid":"OPENID",

"scope":"SCOPE" }

其中openid扔进你的数据库,发送模板消息的时候用

发送模板消息

1.获取access_token

接口地址:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret

appid:公众号appid(必填)

secret:公众号secret(必填)

grant_type:获取access_token填写client_credential(必填)

2.拼接模板消息

```

$data=[

"touser"=>$openid, //对方的openid,前一步获取

"template_id"=>"EVcUo-BP_A59s8sXjmYDZPEXtbaMpOCwVQguN4TUwHY", //模板id

"miniprogram"=>["appid"=>"", //跳转小程序appid

"pagepath"=>"pages/index/index"],//跳转小程序页面

"data"=>[

"first"=>[

"value"=> "你的账户即将到期,请及时缴费", //自定义参数

"color"=> '#173177'//自定义颜色

],

"keyword1"=>[

"value"=> $account, //自定义参数

"color"=> '#173177'//自定义颜色

],

"keyword2"=>[

"value"=> $time, //自定义参数

"color"=> '#173177'//自定义颜色

],

"remark"=>[

"value"=> "如有疑问,请联系当地网点", //自定义参数

"color"=> '#173177'//自定义颜色

],

]

];

```

3.发送模板消息

$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=appid&secret=secret"; //此时再次请求access_token,与获取openid的接口不同!!!

$access_token=json_decode(self::curl($url))->{"access_token"};

$msgurl="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token; // 发送模板消息接口

return json_decode(self::curl($msgurl,$params=json_encode($data),$ispost=1,$https=1));

1.openid获取需要网页获取

2.接口地址严格按照官方所给出的地址填写,参数顺序不能错

3.发送模板消息时获取的access_token具有2小时的时效可丢进缓存中,不必每次发送都获取,每天只有两千次,模板消息发送次数为10万次,当然根据你公众号的关注人数来确定,人数超过10万肯定具有更高的次数

===========================================================

```

$ACCESS_TOKEN="这里写改成自己的ACCESS_TOKEN";//

$touser = ['ouD7BuHpIKRXPIz7pdrwI9IwDRCU','ouD7BuI36wSUZgteyiydmDrldQLU','ouD7BuLejq7R4Vbuyh41bH778cg0'];//openid数组

$data=array(

'touser'=>$touser[0], //要发送给用户的openid

'template_id'=>"mfopDNUlvoBGGsPLB-d_nrfL8Je92xnTq5vk5ZBxL-w",//改成自己的模板id,在微信后台模板消息里查看

'url'=>"http://mp.weixin.qq.com/s/8UWPqHVa8PReWZp-No0ebA", //自己网站链接url

'data'=>array(

'first'=>array(

'value'=>"亲爱的同学,您有考试提醒,请查阅。",

'color'=>"#000"

),

'keyword1'=>array(

'value'=>"2017下半年教师资格证面试",

'color'=>"#f00"

),

'keyword2'=>array(

'value'=>"2018-1-6",

'color'=>"#173177"

),

'keyword3'=>array(

'value'=>"请看您的准考证",

'color'=>"#3d3d3d"

),

'keyword4'=>array(

'value'=>"教师资格证试讲",

'color'=>"#3d3d3d"

),

'keyword5'=>array(

'value'=>"答辩,选题,结构化",

'color'=>"#3d3d3d"

),

'remark'=>array(

'value'=>"\n现在是打印准考证时间,请您在考试前打印准考证,戳进来可以查看详情>>>",

'color'=>"#3d3d3d"

),

)

);

$json_data=json_encode($data);//转化成json数组让微信可以接收

$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$ACCESS_TOKEN;//模板消息请求URL

$res=https_request($url,urldecode($json_data));//请求开始

$res=json_decode($res,true);

if($res['errcode']==0 && $res['errcode']=="ok"){

echo "发送成功!";

}

//curl请求函数,微信都是通过该函数请求

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;

}

?>

```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 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、付费专栏及课程。

余额充值