测试号配置好了后,就可以开始实现发送微信消息接口了;
/** * @return mixed * 获取微信Access_token */ public function get_wx_access_token() { //将access_token存在session/cookie中 //如果access_token在session中并没有过期 if (isset($_SESSION['access_token']) && $_SESSION['expire_time'] > time()) { return $_SESSION['access_token']; } //如果access_token不存在或者已经过期,重新获取access_token else { //protected $appid="wx99e125***efaebe9"; //公众号APPID //protected $appsecret="14b01a0afd0ea****7c4a1a4882385c6"; //公众号秘钥 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $this->appid . "&secret=" . $this->appsecret; $arr = $this->my_curl_wx_api($url); //存到session中 $access_token = $arr['access_token']; $_SESSION['access_token'] = $arr['access_token']; $_SESSION['expire_time'] = time() + 7200; return $access_token; } } //发送 public function service_send(){ unset($_SESSION); //获取Access_token $Access_token=$this->get_wx_access_token(); //请求地址 $url="https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$Access_token; //组装接口Array $array=[ "touser"=>"o0roI1lyAa_8WgMNUlUOjpMlap_Y", //用户的openid "text"=>["content"=>"China is very NB!!! I love China!!!"], "msgtype"=>"text" ]; //将array->json() $postJson=json_encode($array,JSON_UNESCAPED_UNICODE); //调用url $send=$this->my_curl_wx_api($url,'POST',$postJson); var_dump($send); } /** *请求接口 */ protected function my_curl_wx_api($url, $type = 'POST', $data = array()){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false); curl_setopt($ch, CURLOPT_HEADER,0); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); curl_setopt($ch, CURLOPT_AUTOREFERER, 1); if($data){ curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } if(isset($data['file']) && $data['file']){ curl_setopt ( $ch, CURLOPT_SAFE_UPLOAD, false); } $result = curl_exec($ch); curl_close($ch); return json_decode($result, true); }上面就是全部代码,喜欢给个赞鼓励鼓励~~~谢谢