foreach循环执行(发送服务号模版消息),但执行出现瑕疵。

hgwyl 2019-08-05 05:52:32
整个.php页面作用是发送服务号的模板消息,代码如下
$sendingtarget='openid1,openid2';

if (isset($sendingtarget)) {
$sendingtarget_array=explode(",",$sendingtarget);
foreach ($sendingtarget_array as $sendingopenid) {



//循环发送模板消息 开始
function http_request($url,$data=array()){
$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);
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
define('OpenID', $sendingopenid);//发送对象
define('TemplateID', 'mB9jDoh-mwZDp-rweUTm0Q_4VDCq3c__jcpBCFc55zw');//消息模板ID
define('Url', 'http://xxoo.php');//下方详情链接
$json_token=http_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret);
$row=json_decode($json_token,true);
$access_token=$row["access_token"];
//模板消息
$template=array(
'touser' => OpenID,
'template_id' => TemplateID,
'url' => Url,
'data'=>array(
'first'=>array('value'=>urlencode("好友注册成功"),'color'=>"#000000"),
'keyword1'=>array('value'=>urlencode('这是第一行'),'color'=>'#000000'),
'keyword2'=>array('value'=>urlencode('这是第二行'),'color'=>'#000000'),
'remark'=>array('value'=>urlencode('请点击进入审核'),'color'=>'#000000'), )
);

$json_template=json_encode($template);
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
$res=http_request($url,urldecode($json_template));
$res=json_decode($res,true);
if ($res["errcode"]===0){
echo "<div>---|"."发送成功"."|---</div>";
}
else {
echo "<div>---|"."发送失败"."|---</div>";
}
//循环发送模板消息 结束



}
}


情况如下
1、单独拆出foreach部分代码,正确的,能循环打印出sendingtarget_array数组。
2、单独使用发送模板消息,正常,可以发送。

出现的问题
执行代码后,发2次模板消息到同一个openid;而不是各发送1次到2个openid。
...全文
406 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hgwyl 2019-08-06
  • 打赏
  • 举报
回复
引用 5 楼 komyluo 的回复:
http_request这个函数也重复定义了。。。


引用 6 楼 董哥007 的回复:
首先你这个循环里面定义函数 写法上就不对


php和公众号都属于菜鸡入门级别。

可以的话,能给代码么……

T_T
董哥007 2019-08-06
  • 打赏
  • 举报
回复
首先你这个循环里面定义函数 写法上就不对
komyluo 2019-08-06
  • 打赏
  • 举报
回复
http_request这个函数也重复定义了。。。
hgwyl 2019-08-06
  • 打赏
  • 举报
回复
自己解决了,还是得看大量翻帖子和一点点啃代码才行。
翻帖子时,找到一个兄弟说:获取TOKEN有个专门的URL,给你个方法(这面这段代码)
        function http_request($url,$data=array()){
$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);
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}


然后调整一下代码的顺序,最终解决了。
(页面也打印2个"发送成功")
上完整代码给后面的兄弟吧。

//获取TOKEN
function http_request($url,$data=array()){
$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);
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}

//循环发送模板消息 开始
$sendingtarget='openid1,openid2';
if (isset($sendingtarget)) {
$sendingtarget_array=explode(",",$sendingtarget);
foreach ($sendingtarget_array as $sendingopenid) {
//define('OpenID', $sendingopenid);//发送对象
//define('TemplateID', 'mB9jDoh-mwZDp-rweUTm0Q_4VDCq3c__jcpBCFc55zw');//消息模板ID
//define('Url', 'http://xxoo.php');//下方详情链接
$json_token=http_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret);
$row=json_decode($json_token,true);
$access_token=$row["access_token"];
//模板消息
$template=array(
'touser' => $sendingopenid,
'template_id' => 'mB9jDoh-mwZDp-rweUTm0Q_4VDCq3c__jcpBCFc55zw',
'url' => 'http://xxoo.php',
'data'=>array(
'first'=>array('value'=>urlencode("好友注册成功"),'color'=>"#000000"),
'keyword1'=>array('value'=>urlencode('这是第一行'),'color'=>'#000000'),
'keyword2'=>array('value'=>urlencode('这是第二行'),'color'=>'#000000'),
'remark'=>array('value'=>urlencode('请点击进入审核'),'color'=>'#000000'), )
);

$json_template=json_encode($template);
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
$res=http_request($url,urldecode($json_template));
$res=json_decode($res,true);
if ($res["errcode"]===0){
echo "<div>---|"."发送成功"."|---</div>";
}
else {
echo "<div>---|"."发送失败"."|---</div>";
}
//循环发送模板消息 结束
}
}


鞭尸几天再来结贴~
hgwyl 2019-08-05
  • 打赏
  • 举报
回复
引用 楼主 hgwyl 的回复:
整个.php页面作用是发送服务号的模板消息,代码如下
$sendingtarget='openid1,openid2';

if (isset($sendingtarget)) {
$sendingtarget_array=explode(",",$sendingtarget);
foreach ($sendingtarget_array as $sendingopenid) {



//循环发送模板消息 开始
function http_request($url,$data=array()){
$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);
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
define('OpenID', $sendingopenid);//发送对象
define('TemplateID', 'mB9jDoh-mwZDp-rweUTm0Q_4VDCq3c__jcpBCFc55zw');//消息模板ID
define('Url', 'http://xxoo.php');//下方详情链接
$json_token=http_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret);
$row=json_decode($json_token,true);
$access_token=$row["access_token"];
//模板消息
$template=array(
'touser' => OpenID,
'template_id' => TemplateID,
'url' => Url,
'data'=>array(
'first'=>array('value'=>urlencode("好友注册成功"),'color'=>"#000000"),
'keyword1'=>array('value'=>urlencode('这是第一行'),'color'=>'#000000'),
'keyword2'=>array('value'=>urlencode('这是第二行'),'color'=>'#000000'),
'remark'=>array('value'=>urlencode('请点击进入审核'),'color'=>'#000000'), )
);

$json_template=json_encode($template);
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
$res=http_request($url,urldecode($json_template));
$res=json_decode($res,true);
if ($res["errcode"]===0){
echo "<div>---|"."发送成功"."|---</div>";
}
else {
echo "<div>---|"."发送失败"."|---</div>";
}
//循环发送模板消息 结束



}
}


情况如下
1、单独拆出foreach部分代码,正确的,能循环打印出sendingtarget_array数组。
2、单独使用发送模板消息,正常,可以发送。

出现的问题
执行代码后,发2次模板消息到同一个openid;而不是各发送1次到2个openid。


引用 2 楼 PHP是宇宙中最好的语言 的回复:
define('OpenID', $sendingopenid);意义何在?为什么不直接写$sendingopenid


注释了define,还是不行
hgwyl 2019-08-05
  • 打赏
  • 举报
回复

我改了下面这样,还是不行……
$sendingtarget='openid1,openid2';

if (isset($sendingtarget)) {
$sendingtarget_array=explode(",",$sendingtarget);
foreach ($sendingtarget_array as $sendingopenid) {



//循环发送模板消息 开始
function http_request($url,$data=array()){
$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);
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output=curl_exec($ch);
curl_close($ch);
return $output;
}
//define('OpenID', $sendingopenid);//发送对象
//define('TemplateID', 'mB9jDoh-mwZDp-rweUTm0Q_4VDCq3c__jcpBCFc55zw');//消息模板ID
//define('Url', 'http://xxoo.php');//下方详情链接
$json_token=http_request("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret);
$row=json_decode($json_token,true);
$access_token=$row["access_token"];
//模板消息
$template=array(
'touser' => $sendingopenid,
'template_id' => 'mB9jDoh-mwZDp-rweUTm0Q_4VDCq3c__jcpBCFc55zw',
'url' => 'http://xxoo.php',
'data'=>array(
'first'=>array('value'=>urlencode("好友注册成功"),'color'=>"#000000"),
'keyword1'=>array('value'=>urlencode('这是第一行'),'color'=>'#000000'),
'keyword2'=>array('value'=>urlencode('这是第二行'),'color'=>'#000000'),
'remark'=>array('value'=>urlencode('请点击进入审核'),'color'=>'#000000'), )
);

$json_template=json_encode($template);
$url="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=".$access_token;
$res=http_request($url,urldecode($json_template));
$res=json_decode($res,true);
if ($res["errcode"]===0){
echo "<div>---|"."发送成功"."|---</div>";
}
else {
echo "<div>---|"."发送失败"."|---</div>";
}
//循环发送模板消息 结束



}
}
  • 打赏
  • 举报
回复
define('OpenID', $sendingopenid);意义何在?为什么不直接写$sendingopenid
komyluo 2019-08-05
  • 打赏
  • 举报
回复
define('OpenID', $sendingopenid); 重复执行,仅第一次有效

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧