Collections functions
Encryption functions
General functions
Render functions
Theme permission functions
Video functions
Resource functions

execute_api_call()

Description

Execute the specified API function.

Parameters

ColumnTypeDefaultDescription
$query string The query string passed to the API
$pretty boolean false Should the JSON encoded result be 'pretty' i.e. formatted for reading?

Return

void

Location

include/api_functions.php lines 69 to 145

Definition

 
function execute_api_call($query,$pretty=false)
    {
    
$params=array();parse_str($query,$params);
    if (!
array_key_exists("function",$params)) {return false;}
    
$function=$params["function"];
    if (!
function_exists("api_" $function)) {return false;}

    global 
$lang;

    
// Construct an array of the real params, setting default values as necessary
    
$setparams = array();
    
$n 0;    
    
$fct = new ReflectionFunction("api_" $function);
    foreach (
$fct->getParameters() as $fparam)
        {
        
$paramkey $n 1;
        
$param_name $fparam->getName();
        
debug ("API Checking for parameter " $param_name " (param" $paramkey ")");
        if (
array_key_exists("param" $paramkey,$params))
            {
            
debug ("API " $param_name " -   value has been passed : '" $params["param" $paramkey] . "'");
            
$setparams[$n] = $params["param" $paramkey];
            }
        else if(
array_key_exists($param_name$params))
            {
            
debug("API: {$param_name} - value has been passed (by name): '{$params[$param_name]}'");

            if(
$fparam->hasType() && $fparam->isArray() && gettype($params[$param_name]) != "array")
                {
                
$error str_replace(
                    array(
"%arg""%expected-type""%type"),
                    array(
$param_name"array"gettype($params[$param_name])),
                    
$lang["error-type-mismatch"]);
                return 
json_encode($error);
                }

            
$setparams[$n] = $params[$param_name];
            }
        elseif (
$fparam->isOptional())
            {
            
// Set default value if nothing passed e.g. from API test tool
            
debug ("API " $param_name " -  setting default value = '" $fparam->getDefaultValue() . "'");
            
$setparams[$n] = $fparam->getDefaultValue();
            }
        else
            {
             
// Set as empty
            
debug ("API " $param_name " -  setting null value = '" $fparam->getDefaultValue() . "'");
            
$setparams[$n] = "";    
            }
        
$n++;
        }
    
    
debug("API - calling api_" $function);
    
$result call_user_func_array("api_" $function$setparams);

    if(
$pretty)
        {
            
debug("API: json_encode() using JSON_PRETTY_PRINT");
            
$json_encoded_result json_encode($result,(defined('JSON_PRETTY_PRINT')?JSON_PRETTY_PRINT:0));
        }
    else
        {
            
debug("API: json_encode()");
            
$json_encoded_result json_encode($result);
        }
    if(
json_last_error() !== JSON_ERROR_NONE)
        {
        
debug("API: JSON error: " json_last_error_msg());
        
debug("API: JSON error when \$result = " print_r($resulttrue));

        
$json_encoded_result json_encode($result,JSON_UNESCAPED_UNICODE);
        }

    return 
$json_encoded_result;

    }

This article was last updated 23rd November 2020 17:05 Europe/London time based on the source file dated 25th June 2020 16:08 Europe/London time.