大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
如何使用php实现微信公众号图文消息回复与access_token?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
创新互联建站-云计算及IDC服务提供商,涵盖公有云、IDC机房租用、成都多线机房、等保安全、私有云建设等企业级互联网基础服务,联系热线:13518219792
//代码如下 reponseMsg(); } } // 接收事件推送并回复 public function reponseMsg(){ //1.获取到微信推送过来post数据(xml格式) $postArr = $GLOBALS['HTTP_RAW_POST_DATA']; //2.处理消息类型,并设置回复类型和内容 /**/ $postObj = simplexml_load_string( $postArr ); //$postObj->ToUserName = ''; //$postObj->FromUserName = ''; //$postObj->CreateTime = ''; //$postObj->MsgType = ''; //$postObj->Event = ''; // gh_e79a177814ed //判断该数据包是否是订阅的事件推送 if( strtolower( $postObj->MsgType) == 'event'){ //如果是关注 subscribe 事件 if( strtolower($postObj->Event == 'subscribe') ){ //回复用户消息(纯文本格式) $toUser = $postObj->FromUserName; $fromUser = $postObj->ToUserName; $time = time(); $msgType = 'text'; $content = '欢迎关注我们的微信公众账号'.$postObj->FromUserName.'-'.$postObj->ToUserName; $template = " 123456789 "; $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content); echo $info; /* %s */ } } //当微信用户发送imooc,公众账号回复‘imooc is very good' /* 12345678 */ /*if(strtolower($postObj->MsgType) == 'text'){ switch( trim($postObj->Content) ){ case 1: $content = '您输入的数字是1'; break; case 2: $content = '您输入的数字是2'; break; case 3: $content = '您输入的数字是3'; break; case 4: $content = "慕课"; break; case '英文': $content = 'imooc is ok'; break; } $template = " 12345678 "; //注意模板中的中括号 不能少 也不能多 $fromUser = $postObj->ToUserName; $toUser = $postObj->FromUserName; $time = time(); // $content = '18723180099'; $msgType = 'text'; echo sprintf($template, $toUser, $fromUser, $time, $msgType, $content); } } */ //用户发送tuwen1关键字的时候,回复一个单图文 if( strtolower($postObj->MsgType) == 'text' && trim($postObj->Content)=='tuwen2' ){ $toUser = $postObj->FromUserName; $fromUser = $postObj->ToUserName; $arr = array( array( 'title'=>'imooc', 'description'=>"imooc is very cool", 'picUrl'=>'/upload/otherpic66/37831.jpg', 'url'=>'http://www.imooc.com', ), array( 'title'=>'hao123', 'description'=>"hao123 is very cool", 'picUrl'=>'/upload/otherpic66/37832.jpg', 'url'=>'http://www.hao123.com', ), array( 'title'=>'qq', 'description'=>"qq is very cool", 'picUrl'=>'/upload/otherpic66/37831.jpg', 'url'=>'http://www.qq.com', ), ); $template = " %s "; echo sprintf($template, $toUser, $fromUser, time(), 'news'); //注意:进行多图文发送时,子图文个数不能超过10个 }else{ switch( trim($postObj->Content) ){ case 1: $content = '您输入的数字是1'; break; case 2: $content = '您输入的数字是2'; break; case 3: $content = '您输入的数字是3'; break; case 4: $content = "慕课"; break; case '英文': $content = 'imooc is ok'; break; } $template = " %s ".count($arr)." "; foreach($arr as $k=>$v){ $template .=" - "; } $template .="
"; //注意模板中的中括号 不能少 也不能多 $fromUser = $postObj->ToUserName; $toUser = $postObj->FromUserName; $time = time(); // $content = '18723180099'; $msgType = 'text'; echo sprintf($template, $toUser, $fromUser, $time, $msgType, $content); }//if end }//reponseMsg end function http_curl(){ //获取imooc //1.初始化curl $ch = curl_init(); $url = 'http://www.baidu.com'; //2.设置curl的参数 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //3.采集 $output = curl_exec($ch); //4.关闭 curl_close($ch); var_dump($output); } function getWxAccessToken(){ //1.请求url地址 $appid = 'wx08d5c2cb632bb5e4'; $appsecret = '06d3444fb9abd8d00314eb4a38ad61a8'; $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret; //2初始化 $ch = curl_init(); //3.设置参数 curl_setopt($ch , CURLOPT_URL, $url); curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1); //4.调用接口 $res = curl_exec($ch); //5.关闭curl curl_close( $ch ); if( curl_errno($ch) ){ var_dump( curl_error($ch) ); } $arr = json_decode($res, true); var_dump( $arr ); } function getWxServerIp(){ $accessToken = "6vOlKOh7r5uWk_ZPCl3DS36NEK93VIH9Q9tacreuxJ5WzcVc235w_9zONy75NoO11gC9P0o4FBVxwvDiEtsdX6ZRFR0Lfs_ymkb8Bf6kRfo"; $url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$accessToken; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); $res = curl_exec($ch); curl_close($ch); if(curl_errno($ch)){ var_dump(curl_error($ch)); } $arr = json_decode($res,true); echo " %s "; var_dump( $arr ); echo ""; } }//class end
看完上述内容,你们掌握如何使用php实现微信公众号图文消息回复与access_token的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!