Tencent::api PHP Method

api() public static method

发起一个腾讯API请求
public static api ( $command, $params = [], $method = 'GET', $multi = false ) : string
$command 接口名称 如:t/add
$params 接口参数 array('content'=>'test');
$method 请求方式 POST|GET
$multi 图片信息
return string
    public static function api($command, $params = array(), $method = 'GET', $multi = false)
    {
        if (isset($_SESSION['t_access_token'])) {
            //OAuth 2.0 方式
            //鉴权参数
            $params['access_token'] = $_SESSION['t_access_token'];
            $params['oauth_consumer_key'] = OAuth::$client_id;
            $params['openid'] = $_SESSION['t_openid'];
            $params['oauth_version'] = '2.a';
            $params['clientip'] = Common::getClientIp();
            $params['scope'] = 'all';
            $params['appfrom'] = 'php-sdk2.0beta';
            $params['seqid'] = time();
            $params['serverip'] = $_SERVER['SERVER_ADDR'];
            $url = self::$apiUrlHttps . trim($command, '/');
        } elseif (isset($_SESSION['t_openid']) && isset($_SESSION['t_openkey'])) {
            //openid & openkey方式
            $params['appid'] = OAuth::$client_id;
            $params['openid'] = $_SESSION['t_openid'];
            $params['openkey'] = $_SESSION['t_openkey'];
            $params['clientip'] = Common::getClientIp();
            $params['reqtime'] = time();
            $params['wbversion'] = '1';
            $params['pf'] = 'php-sdk2.0beta';
            $url = self::$apiUrlHttp . trim($command, '/');
            //生成签名
            $urls = @parse_url($url);
            $sig = SnsSign::makeSig($method, $urls['path'], $params, OAuth::$client_secret . '&');
            $params['sig'] = $sig;
        }
        //请求接口
        $r = TencentHttp::request($url, $params, $method, $multi);
        $r = preg_replace('/[^\\x20-\\xff]*/', '', $r);
        //清除不可见字符
        $r = iconv('utf-8', 'utf-8//ignore', $r);
        //UTF-8转码
        //调试信息
        if (self::$debug) {
            echo '<pre>';
            echo '接口:' . $url;
            echo '<br>请求参数:<br>';
            print_r($params);
            echo '返回结果:' . $r;
            echo '</pre>';
        }
        return $r;
    }

Usage Example

示例#1
0
function Tencent()
{
    require_once APP_ROOT_PATH . 'system/api_login/Tencent/Tencent.php';
    OAuth::init($GLOBALS['m_config']['tencent_app_key'], $GLOBALS['m_config']['tencent_app_secret']);
    $openid = trim($GLOBALS['request']['openid']);
    $openkey = trim($GLOBALS['request']['openkey']);
    if ($GLOBALS['m_config']['tencent_bind_url'] == "") {
        $app_url = get_domain() . APP_ROOT . "/api_callback.php?c=Tencent";
    } else {
        $app_url = $GLOBALS['m_config']['tencent_bind_url'];
    }
    $access_token = trim($GLOBALS['request']['access_token']);
    es_session::set("t_access_token", $access_token);
    es_session::set("t_openid", $openid);
    es_session::set("t_openkey", $openkey);
    if (es_session::get("t_access_token") || es_session::get("t_openid") && es_session::get("t_openkey")) {
        $r = Tencent::api('user/info');
        $r = json_decode($r, true);
        $name = $r['data']['name'];
        if ($GLOBALS['db']->getOne("select count(*) from " . DB_PREFIX . "user where tencent_id = '" . $name . "'") == 0) {
            $GLOBALS['db']->query("update " . DB_PREFIX . "user set t_access_token ='" . $access_token . "',t_openkey = '" . $openkey . "',t_openid = '" . $openid . "', tencent_id = '" . $name . "' where id =" . intval($GLOBALS['user_info']['id']));
        } elseif (intval($GLOBALS['db']->getOne("select id from " . DB_PREFIX . "user where tencent_id = '" . $name . "'")) == intval($GLOBALS['user_info']['id'])) {
            $GLOBALS['db']->query("update " . DB_PREFIX . "user set t_access_token ='" . $access_token . "',t_openkey = '" . $openkey . "',t_openid = '" . $openid . "', tencent_id = '" . $name . "' where id =" . intval($GLOBALS['user_info']['id']));
        } else {
            $root['return'] = 0;
            $root['info'] = "该微博帐号已被其他会员绑定";
            output($root);
        }
    }
    $root['return'] = 1;
    $root['info'] = "绑定成功";
    $root['login_type'] = "Tencent";
    output($root);
}
All Usage Examples Of Tencent::api
Tencent