国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 熱點 > 微信 > 正文

Thinkphp5微信小程序獲取用戶信息接口的實例詳解

2024-07-22 01:17:21
字體:
來源:轉載
供稿:網友

Thinkphp5微信小程序獲取用戶信息接口的實例詳解

首先在官網下載示例代碼, 選php的,

這里有個坑

官方的php文件,編碼是UTF-8+的, 所以要把文件改為UTF-8

然后在Thinkphp5 extend文件夾下建立Wxxcx命名空間,把官方的幾個類文件放進去(這里要注意文件夾名, 命名空間名, 類名的, 大小寫,一定要一樣,官方的文件名和類名大小寫不一樣)

然后是自己的thinkphp接口代碼:

<?php /**  * Created by PhpStorm.  * User: leeoo  * Date: 2017/9/14 0014  * Time: 10:43  */  namespace app/api/controller/v1;   use think/Loader; use think/Request; use Workerman/Protocols/Http; use Wxxcx/WXBizDataCrypt; use first/second/Foo;  class Index {   public function index($id)   {      return json(['msg' => $id]);   }    public function dologin()   {     $code = Request::instance()->param('code');     $encryptedData = Request::instance()->param('encryptedData');     $iv = Request::instance()->param('iv');      $appid = "你的小程序appid";     $secret = "你的小程序secret";     //appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code     $param = array(       'appid' => $appid,       'secret' => $secret,       'js_code' => $code,       'grant_type' => 'authorization_code'     );   //http函數為封裝的請求函數     $res = http("https://api.weixin.qq.com/sns/jscode2session", $param, 'post');      $arr = json_decode($res, true);      $result = $this->wxdecode($encryptedData, $iv, $arr['session_key'], $appid);      //return json($result);     if ($result) {       return json(['code' => 1]);     } else {       return json(['code' => -1]);     }    }    public function wxdecode($encryptedData, $iv, $sessionKey, $appid)   {     //Loader::import('Wxxcx/WXBizDataCrypt', EXTEND_PATH);     $pc = new WXBizDataCrypt($appid, $sessionKey);     $data = null;     $errCode = $pc->decryptData($encryptedData, $iv, $data);     //echo $data;     //return json(['data'=>$data]);     $data = json_decode($data);      if ($errCode == 0) {       //print($data . "/n");       //dump($data);       return $data;     } else {       //print($errCode . "/n");       //dump($errCode);       return $errCode;     }    } } 

http封裝函數:

/**  * 發送HTTP請求方法  * @param string $url  請求URL  * @param array $params 請求參數  * @param string $method 請求方法GET/POST  * @return array $data  響應數據  */ function http($url, $params, $method = 'GET', $header = array(), $multi = false){   $opts = array(     CURLOPT_TIMEOUT    => 30,     CURLOPT_RETURNTRANSFER => 1,     CURLOPT_SSL_VERIFYPEER => false,     CURLOPT_SSL_VERIFYHOST => false,     CURLOPT_HTTPHEADER   => $header   );   /* 根據請求類型設置特定參數 */   switch(strtoupper($method)){     case 'GET':       $opts[CURLOPT_URL] = $url . '?' . http_build_query($params);       break;     case 'POST':       //判斷是否傳輸文件       $params = $multi ? $params : http_build_query($params);       $opts[CURLOPT_URL] = $url;       $opts[CURLOPT_POST] = 1;       $opts[CURLOPT_POSTFIELDS] = $params;       break;     default:       throw new Exception('不支持的請求方式!');   }   /* 初始化并執行curl請求 */   $ch = curl_init();   curl_setopt_array($ch, $opts);   $data = curl_exec($ch);   $error = curl_error($ch);   curl_close($ch);   if($error) throw new Exception('請求發生錯誤:' . $error);   return $data; }             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜宾市| 徐汇区| 来宾市| 沈丘县| 林口县| 柯坪县| 惠东县| 贵港市| 清镇市| 阿鲁科尔沁旗| 咸阳市| 绥宁县| 邵阳县| 乌海市| 兴海县| 璧山县| 张家界市| 阿鲁科尔沁旗| 彭水| 大渡口区| 松阳县| 乌苏市| 沂南县| 留坝县| 新绛县| 固始县| 赫章县| 松桃| 江川县| 长子县| 和龙市| 桐柏县| 石楼县| 江山市| 平定县| 郑州市| 万州区| 文水县| 九龙县| 吴旗县| 贡觉县|