1、生成二維碼:
01、生成accessToken並保存到文件中
/* * 封住curl請求方法 * @param { (string) $url } 請求的url * @param { (array) $post_data } 請求的參數 * @param { (boolean) $ispost } post/get 默認post * @param {(boolean) $isJson} 是否發送json數據 */ function send_request($url, $post_data = array(), $ispost = true,$isJson=false) { if (empty($url) || empty($post_data)) { return false; } if($isJson) { $post_data=json_encode($post_data); }else $o = http_build_query($post_data); if (!$ispost) { $post_data =$o; if(!strpos($url,'?')) $url =trim($url . '?' . $post_data); else { $url =trim($url.$post_data); } } //初始化curl $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url); //設置header curl_setopt($ch, CURLOPT_HEADER, 0); //要求結果為字符串且輸出到屏幕上 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if ($ispost) { //post提交方式 curl_setopt($ch, CURLOPT_POST, 1); // $post_data為array類型時multipart/formdata請求,string類型時application/x-www-form-urlencoded curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); } curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不驗證證書下同 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $data = curl_exec($ch);//運行curl curl_close($ch); return $data; } /*得到微信accessToken並寫入文件*/ function writeAccessToken($appid,$secret){ $url ='https://api.weixin.qq.com/cgi-bin/token'; $res = send_request($url,array('grant_type'=>'client_credential','appid'=>$appid,'secret'=>$secret),false); $res = json_decode($res,1); if(isset($res['errcode'])) throw new Exception($res['errmsg']); file_put_contents('token.php','<?php exit;?>'.$res['access_token'].','.time()); return $res['access_token']; } /*得到微信accessToken*/ function getAccessToken(){ $file=''; if(file_exists('token.php')) $file=file_get_contents('token.php'); if($file!='') { $content=str_replace('<?php exit;?>','',$file); $arr=explode(',',$content); if(time()-$arr[1]>3000) { return writeAccessToken('wx5f0163b88','a003ed7f659e'); } return $arr[0]; }else { return writeAccessToken('wx5f01641a','a003edcc043bb059e'); } }
02、通過accessToken生成二維碼
參考文檔:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1443433542
/*生成二維碼*/ function generrate() { $qrcode_url = 'https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.getAccessToken(); $post_data = array(); $post_data['expire_seconds'] = 3600 * 24; //有效時間 $post_data['action_name'] = 'QR_SCENE'; $post_data['action_info']['scene']['scene_id'] = 123; //可以用戶uid,微信端可獲取,用戶推廣使用 $json =json_decode(send_request($qrcode_url, $post_data,true,true),true); // echo var_dump($json); if (!$json['errcode']) { $ticket = $json['ticket']; echo '<img src=\'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=' . urlencode($ticket).'\' width=\'260\' height=\'260\'/>'; } else { echo '發生錯誤:錯誤代碼 ' . $json['errcode'] . ',微信返回錯誤信息:' . $json['errmsg']; exit; } }
2、微信公眾號配置
參考文檔:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421135319
配置成功后記得點擊啟用配置
3、處理推送事件
參考文檔:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140454
代碼:
/*處理推送事件*/ function reponseMsg() { $content=file_get_contents("php://input"); if(!empty($content)) { $postObj = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA); file_put_contents('log.txt',date('Y-m-d H:i:s').'\n'.$content); $openid = $postObj->FromUserName; //用戶openid $Event=strtolower($postObj->Event); /*當等於subscribe為已關注用戶*/ if ($Event == 'subscribe') {//首次關注 $is_first = 0; } elseif ($Event == 'scan') {//已關注 $is_first = 1; } } else { file_put_contents('log.txt',date('Y-m-d H:i:s').'\n無法獲取到內容'); exit; } } /*處理微信推送過的信息:注意只有當用戶點擊關注公眾號了或者已經關注了才會推送信息*/ $signature=$_GET['signature']; if($signature!='') { $getInfo=getSHA1('NfzAVfnFD1fv63f4xVtVRNFtRmF1RyRT',$_GET['timestamp'],$_GET['nonce']); $echostr=$_GET['echostr']; if($getInfo[1]!= $signature) { echo '簽名失敗'; exit; } if( $echostr){ //第一次接入weixin api接口的時候 echo $echostr; exit; }else{ //echo 'adss'; reponseMsg(); } }