開發(fā)文檔:https://open.work.weixin.qq.com/api/doc/90000/90135/90664
一、效果圖
?
?
?二、開發(fā)流程
1、創(chuàng)建應(yīng)用
?
?
?2、配置應(yīng)用
?
?
?
?三、簡單代碼
1、授權(quán)登錄
?
?2、回調(diào)
?
?helper.php
/** * GET 請求 * @param string $url */ function http_get($url){ $oCurl = curl_init(); if(stripos($url,"https://")!==FALSE){ curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($oCurl, CURLOPT_VERBOSE, 1); curl_setopt($oCurl, CURLOPT_HEADER, 1); // $sContent = curl_exec($oCurl); // $aStatus = curl_getinfo($oCurl); $sContent = execCURL($oCurl); curl_close($oCurl); return $sContent; } /** * POST 請求 * @param string $url * @param array $param * @param boolean $post_file 是否文件上傳 * @return string content */ function http_post($url,$param,$post_file=false){ $oCurl = curl_init(); if(stripos($url,"https://")!==FALSE){ curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1 } if(PHP_VERSION_ID >= 50500 && class_exists('CURLFile')){ $is_curlFile = true; }else { $is_curlFile = false; if (defined('CURLOPT_SAFE_UPLOAD')) { curl_setopt($oCurl, CURLOPT_SAFE_UPLOAD, false); } } if($post_file) { if($is_curlFile) { foreach ($param as $key => $val) { if(isset($val["tmp_name"])){ $param[$key] = new CURLFile(realpath($val["tmp_name"]),$val["type"],$val["name"]); }else if(substr($val, 0, 1) == '@'){ $param[$key] = new CURLFile(realpath(substr($val,1))); } } } $strPOST = $param; }else{ $strPOST = json_encode($param); } curl_setopt($oCurl, CURLOPT_URL, $url); curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt($oCurl, CURLOPT_POST,true); curl_setopt($oCurl, CURLOPT_POSTFIELDS,$strPOST); curl_setopt($oCurl, CURLOPT_VERBOSE, 1); curl_setopt($oCurl, CURLOPT_HEADER, 1); // $sContent = curl_exec($oCurl); // $aStatus = curl_getinfo($oCurl); $sContent = execCURL($oCurl); curl_close($oCurl); return $sContent; } /** * 執(zhí)行CURL請求,并封裝返回對象 */ function execCURL($ch){ $response = curl_exec($ch); $error = curl_error($ch); $result = array( 'header' => '', 'content' => '', 'curl_error' => '', 'http_code' => '', 'last_url' => ''); if ($error != ""){ $result['curl_error'] = $error; return $result; } $header_size = curl_getinfo($ch,CURLINFO_HEADER_SIZE); $result['header'] = str_replace(array(" ", " ", " "), "<br/>", substr($response, 0, $header_size)); $result['content'] = substr( $response, $header_size ); $result['http_code'] = curl_getinfo($ch,CURLINFO_HTTP_CODE); $result['last_url'] = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL); $result["base_resp"] = array(); $result["base_resp"]["ret"] = $result['http_code'] == 200 ? 0 : $result['http_code']; $result["base_resp"]["err_msg"] = $result['http_code'] == 200 ? "ok" : $result["curl_error"]; return $result; } //給URL地址追加參數(shù) function appendParamter($url,$key,$value){ return strrpos($url,"?",0) > -1 ? "$url&$key=$value" : "$url?$key=$value"; } //生成指定長度的隨機字符串 function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } //讀取本地文件 function get_php_file($filename) { if(file_exists($filename)){ return trim(substr(file_get_contents($filename), 15)); }else{ return '{"expire_time":0}'; } } //寫入本地文件 function set_php_file($filename, $content) { $fp = fopen($filename, "w"); fwrite($fp, "<?php exit();?>" . $content); fclose($fp); } //加載本地的應(yīng)用配置文件 function loadConfig(){ return json_decode(get_php_file("../config.php")); } //根據(jù)應(yīng)用ID獲取應(yīng)用配置 function getConfigByAgentId($id){ $configs = loadConfig(); foreach ($configs->AppsConfig as $key => $value) { if($value->AgentId == $id){ $config = $value; break; } } return $config; } /** * 日志函數(shù) * @param $msg * @param string $filename */ function write_log($msg, $filename='msg') { $file = fopen("./$filename.log", "a"); $msg = "[" . date("Y-m-d H:i:s") . "] - " . $msg; fwrite($file, $msg . " "); fclose($file); } /** * 設(shè)置緩存,按需重載 * @param string $cachename * @param mixed $value * @param int $expired * @return boolean */ function setCache($cachename, $value, $expired) { //TODO: set cache implementation $content = var_export(array('access_token' => $value, 'expires_in' => $expired), true); file_put_contents($cachename.".php", "<?php return " . $content . ";"); return false; } /** * 獲取緩存,按需重載 * @param string $cachename * @return mixed */ function getCache($cachename, $expired = 7100) { if(!file_exists($cachename.".php")){ return false; } $access_token_array = include($cachename.".php"); if (filemtime($cachename.".php") + $expired > time()) { return $access_token_array['access_token']; } return false; } //=======================================后端函數(shù)===================================================== /** * 獲取第三方應(yīng)用憑證(suite_access_token)。 * @param $suite_id * @param $suite_secret * @param $suite_ticket */ function getSuiteAccessToken($suite_id, $suite_secret, $suite_ticket) { $cachename = './cache/suite_access_token'; $access_token = getCache($cachename); if (!$access_token){ $url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_suite_token"; $data = array( 'suite_id' => trim($suite_id), 'suite_secret' => trim($suite_secret), 'suite_ticket' => trim($suite_ticket) ); $response = http_post($url, $data); if ($response['base_resp']['ret'] == 0) { $data = json_decode($response['content'], true); $access_token = $data['suite_access_token']; setCache($cachename, $access_token, $data['expires_in']); } } return $access_token; } /** * 發(fā)送消息 * @param $corpId * @param $secret * @param $touser * @param $msgtype * @param $agentid * @param $content */ function sendAgentMessage($corpId, $secret,$agentid, $touser, $msgtype, $title, $content, $urls) { $access_token = getAccessToken($corpId, $secret); $url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=".$access_token; $data = array( 'touser' => $touser, 'msgtype' => $msgtype, 'agentid' => $agentid, 'textcard' => array( 'title' => $title, 'description' => $content, 'url' => $urls, 'btntxt' => '查看詳情' ) ); $response = http_post($url, $data); return $response['base_resp']; } /** * 獲取預(yù)授權(quán)碼。預(yù)授權(quán)碼用于企業(yè)授權(quán)時的第三方服務(wù)商安全驗證。 * @param $suite_access_token * @return mixed */ function getPreAuthCode($suite_access_token) { $cachename = './cache/pre_auth_code'; $access_token = getCache($cachename, 3500); if (!$access_token){ $url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_pre_auth_code?suite_access_token=".$suite_access_token; $response = http_get($url); if ($response['base_resp']['ret'] == 0) { write_log($response['content'], 'api'); $data = json_decode($response['content'], true); $access_token = $data['pre_auth_code']; setCache($cachename, $access_token, $data['expires_in']); } } return $access_token; } /** * 獲取企業(yè)永久授權(quán)碼 * @param $auth_code 臨時授權(quán)碼 * @return null */ function getPermanentCode($auth_code){ $cachename = './cache/suite_access_token'; $access_token = getCache($cachename); if ($access_token){ $url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_permanent_code?suite_access_token=".$access_token; $data = array( 'auth_code' => $auth_code ); $response = http_post($url, $data); if ($response['base_resp']['ret'] == 0) { return $response['content']; } } return null; } //=======================================后端函數(shù)===================================================== //=======================================前端函數(shù)===================================================== /** * 獲取企業(yè)自建應(yīng)用用戶信息 * @param $code */ function getUserInfo($corpId, $secret, $code) { $access_token = getAccessToken($corpId, $secret); $url = "https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token=$access_token&code=$code"; $response = http_get($url); if ($response['base_resp']['ret'] == 0) { $data = json_decode($response['content'], true); return $data; } return null; } /** * 獲取企業(yè)自建應(yīng)用用戶詳細信息 * @param $corpId * @param $secret * @param $userid * @return mixed */ function getUserDetail($corpId, $secret, $userid) { $access_token = getAccessToken($corpId, $secret); $url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=$access_token&userid=$userid"; $response = http_get($url); if ($response['base_resp']['ret'] == 0){ //保存access_token return json_decode($response['content'], true); } } /** * jssdk獲取 * @param $corpid * @param $secret * @return array */ function getAgentConfigSignPackage($corpid, $secret) { $jsapiTicket = getAgentConfigJsApiTicket($corpid, $secret); // 注意 URL 一定要動態(tài)獲取,不能 hardcode. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $timestamp = time(); $nonceStr = createNonceStr(); //這里參數(shù)的順序要按照 key 值 ASCII 碼升序排序 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; $signature = sha1($string); $signPackage = array( "appId" => $corpid, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return $signPackage; } /** * jssdk獲取 * @param $corpid * @param $secret * @return array */ function getSignPackage($corpid, $secret) { $jsapiTicket = getJsApiTicket($corpid, $secret); // 注意 URL 一定要動態(tài)獲取,不能 hardcode. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; $timestamp = time(); $nonceStr = createNonceStr(); //這里參數(shù)的順序要按照 key 值 ASCII 碼升序排序 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; $signature = sha1($string); $signPackage = array( "appId" => $corpid, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return $signPackage; } function getJsApiTicket($corpid,$secret) { // jsapi_ticket 應(yīng)該全局存儲與更新,以下代碼以寫入到文件中做示例 $path = "./cache/{$corpid}_jsapi_ticket.php"; $data = json_decode(get_php_file($path)); if($data->expire_time < time()){ $app_access_token = getAccessToken($corpid,$secret); $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$app_access_token"; $res = json_decode(http_get($url)["content"]); $ticket = $res->ticket; if ($ticket) { $data->expire_time = time() + 7000; $data->jsapi_ticket = $ticket; set_php_file($path, json_encode($data)); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } function getAgentConfigJsApiTicket($corpid,$secret) { // jsapi_ticket 應(yīng)該全局存儲與更新,以下代碼以寫入到文件中做示例 $path = "./cache/{$corpid}_ticket.php"; $data = json_decode(get_php_file($path)); if($data->expire_time < time()){ $app_access_token = getAccessToken($corpid,$secret); $url = "https://qyapi.weixin.qq.com/cgi-bin/ticket/get?access_token=$app_access_token&type=agent_config"; $res = json_decode(http_get($url)["content"]); $ticket = $res->ticket; if ($ticket) { $data->expire_time = time() + 7000; $data->jsapi_ticket = $ticket; set_php_file($path, json_encode($data)); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } /** * 獲取企業(yè)自建應(yīng)用access_token * @param $corpId * @param $secret * @return mixed */ function getAccessToken($corpId, $secret){ $cachename = './cache/'.$corpId.'_access_token'; $access_token = getCache($cachename); if (!$access_token) { $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$corpId&corpsecret=$secret"; $response = http_get($url); if ($response['base_resp']['ret'] == 0){ //保存access_token $data = json_decode($response['content'], true); $access_token = $data['access_token']; setCache($cachename, $access_token, $data['expires_in']); } } return $access_token; } /** * 獲取訪問用戶身份 * @param $code */ function getUserInfo3rd($code) { $cachename = './cache/suite_access_token'; $access_token = getCache($cachename); $url = "https://qyapi.weixin.qq.com/cgi-bin/service/getuserinfo3rd?suite_access_token=$access_token&code=$code"; $response = http_get($url); if ($response['base_resp']['ret'] == 0) { $data = json_decode($response['content'], true); return $data; } return null; } /** * 獲取授權(quán)企業(yè)的access_token * @param $auth_corpid * @return mixed */ function getCorpToken($auth_corpid) { //判斷是否過期 $access_token_filename = "./cache/{$auth_corpid}_access_token"; $access_token = getCache($access_token_filename); if (!$access_token) { $permanent_code = getCache("./cache/{$auth_corpid}_permanent_code", 3 * 365 * 24 * 3600); $suite_access_token = getCache("./cache/suite_access_token"); $url = "https://qyapi.weixin.qq.com/cgi-bin/service/get_corp_token?suite_access_token=".$suite_access_token; $data = array( 'auth_corpid' => $auth_corpid, 'permanent_code' => $permanent_code ); $response = http_post($url, $data); if ($response['base_resp']['ret'] == 0){ //保存access_token $data = json_decode($response['content'], true); $access_token = $data['access_token']; setCache($access_token_filename, $access_token, $data['expires_in']); } } return $access_token; } /** * 獲取用戶信息 * @param $corpId * @param $userId */ function getUserById($corpId, $userId){ //獲取授權(quán)企業(yè)access_token $access_token = getCorpToken($corpId); $url = "https://qyapi.weixin.qq.com/cgi-bin/user/get?access_token=$access_token&userid=$userId"; $response = http_get($url); if ($response['base_resp']['ret'] == 0){ $data = json_decode($response['content'], true); return $data; } return null; } /** * 獲取訪問用戶敏感信息 * @param $corpId * @param $user_ticket * @return mixed|null */ function getUserDetail3rd($user_ticket){ $cachename = './cache/suite_access_token'; $suite_access_token = getCache($cachename); $url = "https://qyapi.weixin.qq.com/cgi-bin/service/getuserdetail3rd?suite_access_token=$suite_access_token"; $response = http_post($url, array('user_ticket' => $user_ticket)); if ($response['base_resp']['ret'] == 0){ $data = json_decode($response['content'], true); return $data; } return null; }
?
?
?
?
?
本文摘自 :https://blog.51cto.com/u