Collections functions
General functions
Render functions
Theme permission 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 73 to 176

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): '" json_encode($params[$param_name]) . "'");

            
// Check if array;
            
$type $fparam->getType();
            if(
gettype($type) == "object")
                {
                
// type is an object 
                
$type $type->getName();
                }
            if(
$fparam->hasType() && gettype($type) == "string" && $type == "array")
                {
                
// Decode as must be json encoded if array
                
$GLOBALS["use_error_exception"] = true;
                try
                    {
                    
$decoded json_decode($params[$param_name],JSON_OBJECT_AS_ARRAY);
                    }
                catch (
Exception $e)
                    {
                    
$error str_replace(
                        array(
"%arg""%expected-type""%type"),
                        array(
$param_name"array (json encoded)",$lang['unknown']),
                        
$lang["error-type-mismatch"]);
                    return 
json_encode($error);
                    }
                unset(
$GLOBALS["use_error_exception"]);
                
// Check passed data type after decode
                
if(gettype($decoded) != "array")
                    {
                    
$error str_replace(
                        array(
"%arg""%expected-type""%type"),
                        array(
$param_name"array (json encoded)"$lang['unknown']),
                        
$lang["error-type-mismatch"]);
                    return 
json_encode($error);
                    }
                
$params[$param_name] = $decoded;
                }

            
$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 empty value");
            
$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 20th May 2022 09:35 Europe/London time based on the source file dated 14th May 2022 09:10 Europe/London time.