Write business logic file
use think \ Action; // curl method own package details, see Appendix define ( "TOKEN", "you set the token"); class Customer extends Controller { // validate the server's URL address public function checkServer(){ if (isset($_GET['echostr'])) { $this->valid(); }else{ $this->responseMsg(); } } public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ header('content-type:text'); echo $echoStr; exit; }else{ echo $echoStr.'+++'.TOKEN; exit; } } private function checkSignature() { $signature = $_GET["signature"]; $timestamp = $_GET["timestamp"]; $nonce = $_GET["nonce"]; $token = TOKEN; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode( $tmpArr ); $tmpStr = sha1( $tmpStr ); if( $tmpStr == $signature ){ return true; }else{ return false; } } public function responseMsg() { // Here is recommended to use file_get_contents ( 'php: // input') to obtain background data over the post $postStr = file_get_contents('php://input'); if (!empty($postStr) && is_string($postStr)){ $postArr = json_decode($postStr,true); if(!empty($postArr['MsgType']) && $postArr['MsgType'] == 'text'){ // user send a text message to the customer service if($postArr['Content'] == 7){ // receive a text message to the specified trigger events $ FromUsername = $ postArr [ 'FromUserName']; // 发送 者 openid $ Media_id = 'upload picture id micro-channel server, see Part III'; // Enter the message you want to reply to the picture media_id $this->requestIMAGE($fromUsername,$media_id); } } else if(!empty($postArr['MsgType']) && $postArr['MsgType'] == 'image'){ // users to send picture messages to the customer, according to the needs set } else if($postArr['MsgType'] == 'event' && $postArr['Event']=='user_enter_tempsession'){ // user enters customer event $ FromUsername = $ postArr [ 'FromUserName']; // 发送 者 openid $ Content = 'Hello, welcome to customer service ***, what could it help you'; $this->requestTXT($fromUsername,$content); } else{ exit('error'); } }else{ echo "empty"; exit; } } // text reply public function requestTXT($fromUsername,$content){ $data=array( "touser"=>$fromUsername, "msgtype"=>"text", "text"=>array("content"=>$content) ); $json = json_encode($data,JSON_UNESCAPED_UNICODE); $this->requestAPI($json); } // photo Reply public function requestIMAGE($fromUsername,$media_id){ $data=array( "touser"=>$fromUsername, "msgtype"=>"image", "image"=>array("media_id"=>$media_id) ); $json = json_encode($data,JSON_UNESCAPED_UNICODE); $this->requestAPI($json); } public function requestAPI($json){ $access_token = $this->get_accessToken(); $ Action = new Action (); // curl Method own package, Appendix details $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token; $output = $action->curl_post($url,$json); if($output == 0){ echo 'success'; exit; } } // call micro-channel api, get access_token, validity 7200s public function get_accessToken(){ $ Url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= your appid & secret = your secret key'; // replace their id and secret applet $res = file_get_contents($url); $data = json_decode($res,true); $token = $data['access_token']; return $token; } }
Reply to upload pictures to the micro-channel server
This is a temporary material interfaces, can exist only three days, the current applet does not support a permanent creative upload, only public support number
public function uploadWxMedia(){ $token = $this->get_accessToken(); $type = "image"; $ Filepath = Env :: get ( 'root_path') 'public \\ assets \\ imageName.png';. // absolute file path of the server, modify according to their storage location $ Data = array ( "media" => new \ CURLFile ($ filepath)); //php5.6 above must upload this method $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=".$token."&type=".$type; $ Action = new Action (); curl // encapsulation method, Appendix $result = $action->curl_post($url,$data); print_r($result); } // call micro-channel api, get access_token, validity 7200s public function get_accessToken(){ $ Url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid= your appid & secret = your secret key'; // replace their id and secret applet $res = file_get_contents($url); $data = json_decode($res,true); $token = $data['access_token']; return $token; }
Access uploadWxMedia () method would be to set a good image upload, returns a json data:
{"type":"image","media_id":"LTbNsi***************JqG","created_at":1558062553}
Media_id value which is used to fill in the second step into the picture in a reply
appendix
A method of packaging curl
namespace think; class Action { // get way request interface public function get_json($url) { $data = file_get_contents($url); // into an array $data = json_decode($data,true); // output return $data; } // post way request interface public function curl_post($url,$data,$headers = null) { // $ data is an array () array; uncoded $ Curl = curl_init (); // start a CURL session if(substr($url,0,5)=='https'){ // skip the certificate check curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // Only when CURLOPT_SSL_VERIFYHOST CURL less than 7.28.1 supports the use of only 1 represents true, than this version you need to use 2 shows (true does not work). curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); if($headers != null){ // post request carries header parameters curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); } // Returns the api json objects $response = curl_exec($curl); // Close the URL request curl_close($curl); // returns json objects return $response; } }