| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Model_wechat extends Lin_Model
- {
- function __construct(){
- parent::__construct();
- $this->load->database();
- }
- public function getopenid($code){
- $query1 = $this->db->get_where('setting',array('skey' => 'lywx_id'));
- $app_id = $query1->row_array();
- $query2 = $this->db->get_where('setting',array('skey' => 'lywx_secret'));
- $app_secret = $query2->row_array();
-
- $url = 'https://api.weixin.qq.com/sns/jscode2session';
- $params = [
- 'appid' => $app_id['svalue'],
- 'secret' => $app_secret['svalue'],
- 'js_code' => $code,
- 'grant_type' => 'authorization_code'
- ];
- $query = http_build_query($params);
- $requestUrl = $url . '?' . $query;
-
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $requestUrl);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, 10);
-
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
-
- $response = curl_exec($ch);
- $errorNo = curl_errno($ch);
- $errorMsg = curl_error($ch);
- curl_close($ch);
-
- if ($errorNo !== 0) {
- $str = "cURL Error ($errorNo): $errorMsg";
- return [
- "code"=>-1,
- "msg"=>$str
- ];
- }
-
- $result = json_decode($response, true);
-
- // 检查微信返回的错误码
- if (isset($result['errcode'])) {
- $str ="WeChat API Error: " . $result['errmsg'] . " (Code: " . $result['errcode'] . ")";
- return [
- "code"=>-1,
- "msg"=>$str
- ];
- }
-
- return [
- "code"=>1,
- "msg"=>"success",
- "data"=>$result
- ];
- }
-
- }
|