public static function validate_definition_arr($definition_arr) { self::st_reset_error(); if (empty($definition_arr) or !is_array($definition_arr)) { self::st_set_error(self::ERR_DEFINITION, self::s2p_t('Definition is not an array.')); return true; } $default_definition = self::default_method_definition(); $new_definition_arr = array(); foreach ($default_definition as $key => $def_value) { if (!array_key_exists($key, $definition_arr)) { $new_definition_arr[$key] = $def_value; } else { $new_definition_arr[$key] = $definition_arr[$key]; } } if (empty($new_definition_arr['name'])) { self::st_set_error(self::ERR_NAME, self::s2p_t('You should provide a name in method definition.')); return false; } if (empty($new_definition_arr['http_method']) or !S2P_SDK_Rest_API_Request::valid_http_method($new_definition_arr['http_method'])) { self::st_set_error(self::ERR_HTTP_METHOD, self::s2p_t('Invalid HTTP method for API method %s', $new_definition_arr['name'])); return false; } if (!empty($new_definition_arr['get_variables'])) { if (!is_array($new_definition_arr['get_variables'])) { self::st_set_error(self::ERR_GET_VARIABLES, self::s2p_t('Invalid get variables for method %s.', $new_definition_arr['name'])); return false; } $new_var_definition_arr = array(); foreach ($new_definition_arr['get_variables'] as $key => $var_definition) { if (!($new_definition = self::validate_get_variable_definition($var_definition))) { continue; } $new_var_definition_arr[$key] = $new_definition; } if (empty($new_var_definition_arr)) { $new_var_definition_arr = null; } $new_definition_arr['get_variables'] = $new_var_definition_arr; } if (self::st_has_error()) { return false; } /** @var S2P_SDK_Scope_Structure $new_definition_arr['request_structure'] */ if (empty($new_definition_arr['request_structure'])) { $new_definition_arr['request_structure'] = null; } elseif (!$new_definition_arr['request_structure'] instanceof S2P_SDK_Scope_Structure) { self::st_set_error(self::ERR_REQUEST_STRUCTURE, self::s2p_t('Invalid request structure object for method %s.', $new_definition_arr['name'])); return false; } elseif (!$new_definition_arr['request_structure']->get_validated_definition()) { self::st_copy_error($new_definition_arr['request_structure']); return false; } /** @var S2P_SDK_Scope_Structure $new_definition_arr['response_structure'] */ if (empty($new_definition_arr['response_structure'])) { $new_definition_arr['response_structure'] = null; } elseif (!$new_definition_arr['response_structure'] instanceof S2P_SDK_Scope_Structure) { self::st_set_error(self::ERR_RESPONSE_STRUCTURE, self::s2p_t('Invalid response structure object for method %s.', $new_definition_arr['name'])); return false; } elseif (!$new_definition_arr['response_structure']->get_validated_definition()) { self::st_copy_error($new_definition_arr['response_structure']); return false; } /** @var S2P_SDK_Scope_Structure $new_definition_arr['error_structure'] */ if (empty($new_definition_arr['error_structure'])) { $new_definition_arr['error_structure'] = null; } elseif (!$new_definition_arr['error_structure'] instanceof S2P_SDK_Scope_Structure) { self::st_set_error(self::ERR_ERROR_STRUCTURE, self::s2p_t('Invalid error structure object for method %s.', $new_definition_arr['name'])); return false; } elseif (!$new_definition_arr['error_structure']->get_validated_definition()) { self::st_copy_error($new_definition_arr['error_structure']); return false; } return $new_definition_arr; }
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; }