Piwik\API\ResponseBuilder::getResponse PHP Method

getResponse() public method

- If the data resulted from the API call is a DataTable then - we apply the standard filters if the parameters have been found in the URL. For example to offset,limit the Table you can add the following parameters to any API call that returns a DataTable: filter_limit=10&filter_offset=20 - we apply the filters that have been previously queued on the DataTable
See also: DataTable::queueFilter() - we apply the renderer that generate the DataTable in a given format (XML, PHP, HTML, JSON, etc.) the format can be changed using the 'format' parameter in the request. Example: format=xml - If there is nothing returned (void) we display a standard success message - If there is a PHP array returned, we try to convert it to a dataTable It is then possible to convert this datatable to any requested format (xml/etc) - If a bool is returned we convert to a string (true is displayed as 'true' false as 'false') - If an integer / float is returned, we simply return it
public getResponse ( mixed $value = null, boolean | string $apiModule = false, boolean | string $apiMethod = false ) : mixed
$value mixed The initial returned value, before post process. If set to null, success response is returned.
$apiModule boolean | string The API module that was called
$apiMethod boolean | string The API method that was called
return mixed Usually a string, but can still be a PHP data structure if the format requested is 'original'
    public function getResponse($value = null, $apiModule = false, $apiMethod = false)
    {
        $this->apiModule = $apiModule;
        $this->apiMethod = $apiMethod;
        $this->sendHeaderIfEnabled();
        // when null or void is returned from the api call, we handle it as a successful operation
        if (!isset($value)) {
            if (ob_get_contents()) {
                return null;
            }
            return $this->apiRenderer->renderSuccess('ok');
        }
        // If the returned value is an object DataTable we
        // apply the set of generic filters if asked in the URL
        // and we render the DataTable according to the format specified in the URL
        if ($value instanceof DataTableInterface) {
            return $this->handleDataTable($value);
        }
        // Case an array is returned from the API call, we convert it to the requested format
        // - if calling from inside the application (format = original)
        //    => the data stays unchanged (ie. a standard php array or whatever data structure)
        // - if any other format is requested, we have to convert this data structure (which we assume
        //   to be an array) to a DataTable in order to apply the requested DataTable_Renderer (for example XML)
        if (is_array($value)) {
            return $this->handleArray($value);
        }
        if (is_object($value)) {
            return $this->apiRenderer->renderObject($value);
        }
        if (is_resource($value)) {
            return $this->apiRenderer->renderResource($value);
        }
        return $this->apiRenderer->renderScalar($value);
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Records Global settings when user submit changes
  */
 public function setGlobalSettings()
 {
     $response = new ResponseBuilder(Common::getRequestVar('format'));
     try {
         $this->checkTokenInUrl();
         $timezone = Common::getRequestVar('timezone', false);
         $excludedIps = Common::getRequestVar('excludedIps', false);
         $excludedQueryParameters = Common::getRequestVar('excludedQueryParameters', false);
         $excludedUserAgents = Common::getRequestVar('excludedUserAgents', false);
         $currency = Common::getRequestVar('currency', false);
         $searchKeywordParameters = Common::getRequestVar('searchKeywordParameters', $default = "");
         $searchCategoryParameters = Common::getRequestVar('searchCategoryParameters', $default = "");
         $enableSiteUserAgentExclude = Common::getRequestVar('enableSiteUserAgentExclude', $default = 0);
         $keepURLFragments = Common::getRequestVar('keepURLFragments', $default = 0);
         $api = API::getInstance();
         $api->setDefaultTimezone($timezone);
         $api->setDefaultCurrency($currency);
         $api->setGlobalExcludedQueryParameters($excludedQueryParameters);
         $api->setGlobalExcludedIps($excludedIps);
         $api->setGlobalExcludedUserAgents($excludedUserAgents);
         $api->setGlobalSearchParameters($searchKeywordParameters, $searchCategoryParameters);
         $api->setSiteSpecificUserAgentExcludeEnabled($enableSiteUserAgentExclude == 1);
         $api->setKeepURLFragmentsGlobal($keepURLFragments);
         $toReturn = $response->getResponse();
     } catch (Exception $e) {
         $toReturn = $response->getResponseException($e);
     }
     return $toReturn;
 }
All Usage Examples Of Piwik\API\ResponseBuilder::getResponse