Exemplo n.º 1
0
 public function do_call($params = false)
 {
     $this->reset_call_result();
     if (empty($params) or !is_array($params)) {
         $params = array();
     }
     if (empty($params['user_agent']) or !is_string($params['user_agent'])) {
         $params['user_agent'] = '';
     }
     if (empty($params['allow_remote_calls'])) {
         $params['allow_remote_calls'] = false;
     }
     if (empty($params['quick_return_request'])) {
         $params['quick_return_request'] = false;
     }
     if (empty($params['custom_validators']) or !is_array($params['custom_validators'])) {
         $params['custom_validators'] = array();
     }
     if (empty($this->_method)) {
         $this->set_error(self::ERR_METHOD, self::s2p_t('Method not set'));
         return false;
     }
     if (!($api_key = $this->get_api_key())) {
         $this->set_error(self::ERR_APIKEY, self::s2p_t('API Key not set.'));
         return false;
     }
     if (!($site_id = $this->get_site_id())) {
         $this->set_error(self::ERR_SITE_ID, self::s2p_t('Site ID not set.'));
         return false;
     }
     if (!$this->validate_base_url() or empty($this->_base_url)) {
         $this->set_error(self::ERR_URL, self::s2p_t('Couldn\'t obtain base URL.'));
         return false;
     }
     if (!($request_data = $this->_method->prepare_for_request($params))) {
         if ($this->_method->has_error()) {
             $this->copy_error($this->_method);
         } else {
             $this->set_error(self::ERR_PREPARE_REQUEST, self::s2p_t('Couldn\'t prepare request data.'));
         }
         return false;
     }
     if ($hook_result = $this->trigger_hooks('rest_api_prepare_request_after', array('api_obj' => $this, 'request_data' => $request_data)) and is_array($hook_result)) {
         if (array_key_exists('request_data', $hook_result)) {
             $request_data = $hook_result['request_data'];
         }
     }
     $final_url = $this->_base_url . $request_data['full_query'];
     if (!empty($params['quick_return_request'])) {
         $return_arr = array();
         $return_arr['final_url'] = $final_url;
         $return_arr['request_data'] = $request_data;
         return $return_arr;
     }
     if (!($this->_request = new S2P_SDK_Rest_API_Request())) {
         $this->set_error(self::ERR_PREPARE_REQUEST, self::s2p_t('Couldn\'t prepare API request.'));
         return false;
     }
     if (empty($request_data['http_method']) or !$this->_request->set_http_method($request_data['http_method'])) {
         $this->set_error(self::ERR_HTTP_METHOD, self::s2p_t('Couldn\'t set HTTP method.'));
         return false;
     }
     if (!$this->_request->set_url($final_url)) {
         $this->set_error(self::ERR_URL, self::s2p_t('Couldn\'t set final URL.'));
         return false;
     }
     if (!empty($request_data['request_body'])) {
         $this->_request->set_body($request_data['request_body']);
     }
     $this->_request->add_header('Content-Type', 'application/json; charset=utf-8');
     $call_params = array();
     if (empty($params['user_agent'])) {
         $call_params['user_agent'] = 'APISDK_' . S2P_SDK_VERSION . '/PHP_' . phpversion() . '/' . php_uname('s') . '_' . php_uname('r');
     } else {
         $call_params['user_agent'] = trim($params['user_agent']);
     }
     $call_params['userpass'] = array('user' => $site_id, 'pass' => $api_key);
     $this->trigger_hooks('rest_api_call_before', array('api_obj' => $this));
     if (!($request_result = $this->_request->do_curl($call_params))) {
         $request_result = null;
     }
     if ($hook_result = $this->trigger_hooks('rest_api_request_result', array('api_obj' => $this, 'request_result' => $request_result)) and is_array($hook_result)) {
         if (array_key_exists('request_result', $hook_result)) {
             $request_result = S2P_SDK_Rest_API_Request::validate_request_array($hook_result['request_result']);
         }
     }
     if (empty($request_result)) {
         if ($this->_request->has_error()) {
             $this->copy_error($this->_request);
         } else {
             $this->set_error(self::ERR_CURL_CALL, self::s2p_t('Error sending API call'));
         }
         return false;
     }
     $return_arr = self::default_call_result();
     $return_arr['final_url'] = $final_url;
     $return_arr['request'] = $request_result;
     $this->_call_result = $return_arr;
     if (!in_array($request_result['http_code'], S2P_SDK_Rest_API_Codes::success_codes())) {
         $code_str = $request_result['http_code'];
         if ($code_details = S2P_SDK_Rest_API_Codes::valid_code($request_result['http_code'])) {
             $code_str .= ' (' . $code_details . ')';
         }
         // Set a generic error as maybe we will get more specific errors later when parsing response. Don't throw this error yet...
         $this->set_error(self::ERR_CURL_CALL, self::s2p_t('Request responded with error code %s', $code_str), '', array('prevent_throwing_errors' => true));
         if (empty($request_result['response_buffer'])) {
             // In case there's nothing to parse, throw generic error...
             if ($this->throw_errors()) {
                 $this->throw_error();
             }
             return false;
         }
     }
     if (!($response_data = $this->_method->parse_response($request_result))) {
         if ($this->_method->has_error()) {
             $this->copy_error($this->_method);
         }
         if ($this->has_error()) {
             if ($this->throw_errors()) {
                 $this->throw_error();
             }
             return false;
         }
         $this->set_error(self::ERR_PARSE_RESPONSE, self::s2p_t('Error parsing server response.'));
         return false;
     }
     if ($hook_result = $this->trigger_hooks('rest_rest_api_response_data', array('api_obj' => $this, 'response_data' => $response_data)) and is_array($hook_result)) {
         if (array_key_exists('response_data', $hook_result)) {
             $response_data = $hook_result['response_data'];
         }
     }
     if (!$this->_method->validate_response($response_data)) {
         if ($this->_method->has_error()) {
             $this->copy_error($this->_method);
         } else {
             $this->set_error(self::ERR_VALIDATE_RESPONSE, self::s2p_t('Error validating server response.'));
         }
         return false;
     }
     $return_arr['response'] = $response_data;
     $this->_call_result = $return_arr;
     // Make sure errors get thrown if any...
     if ($this->has_error() and $this->throw_errors()) {
         $this->throw_error();
         return false;
     }
     return $return_arr;
 }