Example #1
0
 /**
  * 获取用户授权后回调页面根据获取到的code,获取用户信息
  * 注:本函数将获取access_token和拉取用户信息集成在了一起,未对获取到的access_token进行保存
  *
  * Examples:
  * ```
  * $api->get_userinfo_by_authorize('snsapi_base', $_GET['code']);
  * $api->get_userinfo_by_authorize('snsapi_userinfo', $_GET['code']);
  * ```
  *
  * @param $scope `get_authorize_url`时使用的授权类型
  * @param string $lang 可选,返回国家地区语言版本,zh_CN 简体,zh_TW 繁体,en 英语
  *
  * @return array|object
  */
 public function get_userinfo_by_authorize($scope, $lang = 'zh_CN')
 {
     if (isset($_GET['code']) && !empty($_GET['code'])) {
         $code = $_GET['code'];
         // 1. 通过code换取网页授权access_token
         $url = self::API_DOMAIN . 'sns/oauth2/access_token?appid=' . $this->appId . '&secret=' . $this->appSecret . '&code=' . $code . '&grant_type=authorization_code';
         $res = HttpCurl::get($url, 'json');
         // 异常处理: 获取时网络错误
         if ($res === false) {
             return Error::code('ERR_POST');
         }
         // 判断是否调用成功
         if (isset($res->access_token)) {
             if ($scope == 'snsapi_userinfo') {
                 // 2.1 `snsapi_userinfo` 继续通过access_token和openid拉取用户信息
                 $url = self::API_DOMAIN . 'sns/userinfo?access_token=' . $res->access_token . '&openid=' . $res->openid . '&lang=' . $lang;
                 $res = HttpCurl::get($url, 'json');
                 // 异常处理: 获取时网络错误
                 if ($res === false) {
                     return Error::code('ERR_POST');
                 }
                 // 判断是否调用成功
                 if (isset($res->openid)) {
                     return array(null, $res);
                 } else {
                     return array($res, null);
                 }
             } else {
                 // 2.2 `snsapi_base` 不弹出授权页面,直接跳转,只能获取用户openid
                 return array(null, $res);
             }
         } else {
             return array($res, null);
         }
     } else {
         return array('授权失败', null);
     }
 }
Example #2
0
 /**
  * 微信支付 - 生成预订单
  * @param string $openid 用户openid
  * @param array $conf 配置数组
  * @return bool|mixed
  */
 public function wxPayUnifiedOrder($openid, $conf = [])
 {
     $input = ['out_trade_no' => $conf['out_trade_no'], 'body' => $conf['body'], 'total_fee' => $conf['total_fee'], 'trade_type' => isset($conf['trade_type']) ? $conf['trade_type'] : 'JSAPI', 'notify_url' => isset($conf['notify_url']) ? $conf['notify_url'] : '', 'attach' => isset($conf['attach']) ? $conf['attach'] : '', 'time_start' => isset($conf['time_start']) ? $conf['time_start'] : date('YmdHis'), 'time_expire' => isset($conf['time_expire']) ? $conf['time_expire'] : date('YmdHis', time() + 86400), 'goods_tag' => isset($conf['goods_tag']) ? $conf['goods_tag'] : '', 'openid' => $openid, 'appid' => $this->appId, 'mch_id' => $this->mchId, 'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], 'nonce_str' => SHA1::get_random_str(32)];
     // 签名
     $input['sign'] = SHA1::getSign2($input, 'key=' . $this->key);
     // 生成xml
     $xml = Xml::toXml($input);
     // 调用接口
     $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
     try {
         $res = HttpCurl::post($url, $xml);
         libxml_disable_entity_loader(true);
         return json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
     } catch (\Exception $e) {
         return false;
     }
 }
Example #3
0
 /**
  * 长链接转短链接接口
  *
  * @string $long_url 需要转换的长链接,支持http://、https://、weixin://wxpay 格式的url
  *
  * @return array(err, data)
  * - `err`, 调用失败时得到的异常
  * - `res`, 调用正常时得到的对象
  *
  * Examples:
  * ```
  * list($err, $data) = $api->shorturl('http://me.diary8.com/category/web-front-end.html');
  * echo $data->short_url;
  * ```
  * Result:
  * ```
  * http://w.url.cn/s/ABJrkxE
  * ```
  */
 public function shorturl($long_url)
 {
     $url = self::API_DOMAIN . 'cgi-bin/shorturl?access_token=' . $this->get_access_token();
     $xml = '{"action":"long2short","long_url":"' . $long_url . '"}';
     $res = HttpCurl::post($url, $xml, 'json');
     // 异常处理: 获取时网络错误
     if ($res === FALSE) {
         return Error::code('ERR_GET');
     }
     // 判断是否调用成功
     if ($res->errcode == 0) {
         return array(NULL, $res);
     } else {
         return array($res, NULL);
     }
 }
Example #4
0
 /**
  * 获取用户列表
  *
  * Examples:
  * ```
  * $api->get_user_list();
  * $api->get_user_list('ocNtAt_TirhYM6waGeNUbCfhtZoA');
  * ```
  * Result:
  * ```
  * [
  *     null,
  *     {
  *         total: 4,
  *         count: 2,
  *         data: {
  *             openid: [
  *                 "ocNtAt_K8nRlAdmNEo_R0WVg_rRw",
  *                 "ocNtAt9DVhWngpiMyZzPFWr4IXD0"
  *             ]
  *         },
  *         next_openid: "ocNtAt9DVhWngpiMyZzPFWr4IXD0"
  *     }
  * ]
  * ```
  *
  * @param string $next_openid [可选:第一个拉取的OPENID,不填默认从头开始拉取]
  *
  * @return array(err, data)
  * - `err`, 调用失败时得到的异常
  * - `res`, 调用正常时得到的对象
  */
 public function get_user_list($next_openid = '')
 {
     if ($next_openid != '') {
         $next_openid = '&next_openid=' . $next_openid;
     }
     $url = self::API_DOMAIN . 'cgi-bin/user/get?access_token=' . $this->get_access_token() . $next_openid;
     $res = HttpCurl::get($url, 'json');
     // 异常处理: 获取时网络错误
     if ($res === false) {
         return Error::code('ERR_POST');
     }
     // 判断是否调用成功
     if (isset($res->data)) {
         return array(null, $res);
     } else {
         return array($res, null);
     }
 }