BaiduApiClient::api PHP Méthode

api() public méthode

Call an api which is opened by Baidu, file upload apis should not be called by this interface.
public api ( string $uri, array $params = [], string $httpMethod = 'GET', string $type = 'rest' ) : array | false
$uri string Uri for the api, it could be the whole url, like 'https://openapi.baidu.com/rest/2.0/passport/user/info/get', or url path only, like '/rest/2.0/passport/user/info/get', or just api method only, like 'passport/user/info/get'.
$params array Api specific parameters.
$httpMethod string Http method, could be 'GET' or 'POST'.
$type string Type name of the openapi, could be 'rest', or 'public'.
Résultat array | false Returns an array if success, or false if failed.
    public function &api($uri, $params = array(), $httpMethod = 'GET', $type = 'rest')
    {
        if (substr($uri, 0, 8) === 'https://') {
            //apis using https + access_token
            $params = array_merge(array('access_token' => $this->getAccessToken()), $params);
        } elseif (substr($uri, 0, 7) === 'http://') {
            //apis using http + client_id
            $params = array_merge(array('client_id' => $this->getClientId()), $params);
        } else {
            if (substr($uri, 0, 6) === '/rest/') {
                //apis using https + access_token and default domain
                $uri = self::$BD_OPENAPI_DEFAULT_DOMAINS['rest'] . $uri;
                $params = array_merge(array('access_token' => $this->getAccessToken()), $params);
            } elseif (substr($uri, 0, 8) === '/public/') {
                //apis using http + client and default domain
                $uri = self::$BD_OPENAPI_DEFAULT_DOMAINS['public'] . $uri;
                $params = array_merge(array('client_id' => $this->getClientId()), $params);
            } elseif ($type === 'rest') {
                $uri = self::$BD_OPENAPI_DEFAULT_PREFIXS['rest'] . $uri;
                $params = array_merge(array('access_token' => $this->getAccessToken()), $params);
            } elseif ($type === 'public') {
                $uri = self::$BD_OPENAPI_DEFAULT_PREFIXS['public'] . $uri;
                $params = array_merge(array('client_id' => $this->getClientId()), $params);
            } else {
                BaiduUtils::setError(-1, 'Invalid params for ' . __METHOD__ . ": uri[{$uri}] type[{$type}]");
                return false;
            }
        }
        if ($this->batchQueue === null) {
            $result = BaiduUtils::request($uri, $params, $httpMethod);
            if ($result !== false) {
                $result = $this->converJson2Array($result);
                if (is_array($result) && isset($result['error_code'])) {
                    BaiduUtils::setError(-1, 'failed to call baidu openapi: error_code[' . $result['error_code'] . '] error_msg[' . $result['error_msg'] . ']');
                    return false;
                }
            }
        } else {
            // batch run
            $result = null;
            unset($params['access_token']);
            unset($params['client_id']);
            $query = http_build_query($params, '', '&');
            $parts = parse_url($uri);
            $item = array('domain' => $parts['host'], 'path' => $parts['path'], 'params' => $parts['query'] ? $parts['query'] . '&' . $query : $query, 'http_method' => $httpMethod);
            if ($parts['scheme'] === 'https') {
                $this->batchQueue[0][] = array('i' => $item, 'r' => &$result);
            } else {
                $this->batchQueue[1][] = array('i' => $item, 'r' => &$result);
            }
        }
        return $result;
    }

Usage Example

require_once './inc/lightapp_login_api.inc.php';
//回调页地址
$redirectUri = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
$code = $_GET['code'];
//echo $code;
//echo $redirectUri;
$oauth = new BaiduOAuth2($lightapp_api_key, $ligthapp_secret_key);
$oauth->setRedirectUri($redirectUri);
$tokenArr = $oauth->getAccessTokenByAuthorizationCode($code);
if (is_array($tokenArr)) {
    // 换取token成功
    $accessToken = $tokenArr['access_token'];
    $expires_in = $tokenArr['expires_in'];
    // 获取用户信息
    $client = new BaiduApiClient($lightapp_api_key, $accessToken);
    $infoArr = $client->api('/rest/2.0/passport/users/getInfo', array('fields' => 'userid,username,portrait'));
    if (is_array($infoArr)) {
        // 获取用户信息成功
        // 在这里将百度账号与应用自身的账号系统做联合登录处理,建议采取将百度账号暗绑到自身账号体系上
        // 然后将联合登录后生成的用户session的相关信息通过cookie返回到前端页面上
        // 为方便处理,这里将access_token和百度用户uid直接当session信息塞入cookie
        setcookie('bd_access_token', $accessToken, strtotime('2030-1-1 12:00:00'), '/');
        setcookie('bd_username', $infoArr['username'], strtotime('2030-1-1 12:00:00'), '/');
        setcookie('bd_uid', $infoArr['userid'], strtotime('2030-1-1 12:00:00'), '/');
        setcookie('bd_portrait', $infoArr['portrait'], strtotime('2030-1-1 12:00:00'), '/');
    }
}
header("Location:" . 'http://' . $_SERVER['HTTP_HOST'] . '/index.php');
?>

All Usage Examples Of BaiduApiClient::api