調用高級羣發接口:php
1. 調用根據分組進行羣發,返回錯誤代碼:errcode:40008,errmsg:invalid message typejson
錯誤緣由:HTTP請求提交的數據未進過JSON編碼,注意下面代碼中註釋標明「正確和錯誤方式」的部分。api
調用代碼以下:curl
<?php class ScheduleMessage{ private $access_token; public function __construct($access_token) { $this->access_token = $access_token; } public function sentMsgToGroup() { // 根據分組進行羣發【訂閱號與服務號認證後都可用】 // http請求方式: POST $url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=$this->access_token"; $data = array( 'filter' => array( 'is_to_all' => false, 'group_id' => 0 ), 'msgtype' => 'text', 'text' => array( 'content' => 'this is test message with 中文!' ) ); // JSON參數錯誤體檢方式 // $res = json_decode($this -> httpPost($url, $data)); // JSON參數正確提交方式 $res = json_decode($this -> httpPost($url, json_encode($data, JSON_UNESCAPED_UNICODE))); return $res; } private function httpPost($url, $data) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } } ?> |
2. 調用根據OpenID列表羣發,返回錯誤代碼:errcode:40003,errmsg:invalid openid this
錯誤緣由:HTTP請求提交的數據未進過JSON編碼,注意下面代碼中註釋標明「正確和錯誤方式」的部分。編碼
調用代碼以下:url
<?php class ScheduleMessage{ private $access_token; public function __construct($access_token) { $this->access_token = $access_token; } public function sentMsgToOpenId() { // 根據分組進行羣發【訂閱號與服務號認證後都可用】 // http請求方式: POST $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$this->access_token"; $data = array( 'touser' => 'oCECzv7gYSf4SCUrqYNPGL5JJI4M', 'msgtype' => 'text', 'text' => array( 'content' => 'this is test message with 中文!' ) ); // JSON參數錯誤體檢方式 // $res = json_decode($this -> httpPost($url, $data)); // JSON參數正確提交方式 $res = json_decode($this -> httpPost($url, json_encode($data, JSON_UNESCAPED_UNICODE))); return $res; } private function httpPost($url, $data) { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_TIMEOUT, 500); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_URL, $url); $res = curl_exec($curl); curl_close($curl); return $res; } } ?>