Remote::get PHP Method

get() public static method

Static method to send a GET request
public static get ( string $url, array $params = [] ) : object
$url string
$params array
return object Response
    public static function get($url, $params = array())
    {
        $defaults = array('method' => 'GET', 'data' => array());
        $options = array_merge($defaults, $params);
        $query = http_build_query($options['data']);
        if (!empty($query)) {
            $url = url::hasQuery($url) ? $url . '&' . $query : $url . '?' . $query;
        }
        // remove the data array from the options
        unset($options['data']);
        $request = new self($url, $options);
        return $request->response();
    }

Usage Example

示例#1
0
 public function action_do()
 {
     $params = Oauth2::parse_query();
     try {
         if (empty($params['code']) or isset($params['error'])) {
             throw new Oauth2_Exception($params['error']);
         }
         $token = Remote::get($this->_configs['token_uri'], array(CURLOPT_POST => TRUE, CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded;charset=utf-8'), CURLOPT_POSTFIELDS => Oauth2::build_query(array('grant_type' => $this->_configs['grant_type'], 'code' => $params['code'], 'client_id' => $this->_configs['client_id'], 'redirect_uri' => $this->_configs['redirect_uri'], 'client_secret' => $this->_configs['client_secret']))));
         $token = json_decode($token);
         if (isset($token->error)) {
             throw new Oauth2_Exception($token->error);
         }
         // Resource in json format
         $resource = Remote::get($this->_configs['access_uri'], array(CURLOPT_POST => TRUE, CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded;charset=utf-8'), CURLOPT_POSTFIELDS => Oauth2::build_query(array('oauth_token' => $token->access_token, 'timestamp' => $_SERVER['REQUEST_TIME'], 'refresh_token' => $token->refresh_token, 'expires_in' => $token->expires_in, 'client_id' => $this->_configs['client_id']))));
         $this->request->response = $resource;
     } catch (Exception $e) {
         $error = $e->getMessage();
     }
     if (isset($error)) {
         switch ($error) {
             case 'access_denied':
                 $this->request->response = 'You have denied this request.';
                 break;
             default:
                 $this->request->response = 'There must be some errors happen in this connection, please contact our web master.' . "[{$error}]";
                 break;
         }
     }
 }
All Usage Examples Of Remote::get