Hi All,
I'm building an embedded application that uses PHP scripting for internal
data processing. It behaves as follows:
- Provide arbitrary application-specified input parameters.
- Execute a PHP script that manipulates those input parameters.
- Retrieve the resulting return value.
I've attempted three possible approaches for achieving this goal. Please
bear with me as I explain each of these approaches -- it's necessary in
order to understand the question :-)
Approach A:
Parse a script containing a PHP function, and then call that PHP function.
On the PHP side:
// some arbitrary user-defined PHP function
function myfunction( $pArg ) { return strtoupper( $pArg ); }
On the application side:
// do something to parse "myfunction" and cause it to be available via
call_user_function()
php_lint_script(&file_handle TSRMLS_CC);
zval funcname, retval;
zval *args[1];
INIT_ZVAL(retval);
INIT_ZVAL(funcname);
MAKE_STD_ZVAL(args[1]);
ZVAL_STRING(args[1], "the string to make uppercase", 0);
ZVAL_STRING(&funcname, "myfunction", 0);
// call the user function
if(call_user_function(EG(function_table), NULL, &funcname, &retval, 1, args
TSRMLS_CC) != FAILURE)
{
// it worked
}
My problem with Approach A is that "php_lint_script", "php_execute_script",
etc, do not add the PHP function "myfunction" to the EG(function_table)
hash, so call_user_function() always fails.
Approach B:
Execute a PHP script and retrieve the return value without calling a
particular PHP function.
On the PHP side:
return strtoupper( $GLOBAL_VARIABLE );
On the application side:
php_register_variable("GLOBAL_VARIABLE", "the string to make uppercase",
NULL
TSRMLS_CC);
zval *retval;
php_execute_simple_script(&file_handle, &retval TSRMLS_CC);
My problem with Approach B is that the return zval from the
php_execute_simple_script() function always contains a true/false value
instead of whatever was returned using the 'return' construct from the code.
Approach C:
Similar to approach B, except call a module function to record the result.
On the PHP side:
record_return( strtoupper( $GLOBAL_VARIABLE ) );
On the application side, create a module that provides the "record_return"
function to record the return value for later usage. This approach works.
However, if at all possible, I would prefer to use the PHP language 'return'
construct and somehow retrieve the resulting value.
Does anyone have any suggestions on how I could get either approach A or
approach B working? Any input would be greatly appreciated.
I'm currently using PHP 5.2.4 for development -- I can upgrade to a newer
version if that would be beneficial.
Thanks,
Marshall
Hi All,
A correction to my earlier email.
On Tue, Apr 15, 2008 at 11:37 AM, Marshall Greenblatt <
magreenblatt@gmail.com> wrote:
Approach B:
Execute a PHP script and retrieve the return value without calling a
particular PHP function.On the PHP side:
return strtoupper( $GLOBAL_VARIABLE );
On the application side:
php_register_variable("GLOBAL_VARIABLE", "the string to make uppercase",
NULL
TSRMLS_CC);zval *retval;
php_execute_simple_script(&file_handle, &retval TSRMLS_CC);My problem with Approach B is that the return zval from the
php_execute_simple_script() function always contains a true/false value
instead of whatever was returned using the 'return' construct from the code.
This approach does actually work as advertised. I had a bug in my test code
:-).
However, that still leaves the original question -- how to call a function
defined in a PHP script using call_user_function() or similar?
Thanks,
Marshall
Hi All,
Pardon the noise. All functions, including call_user_function(), now work
as expected.
Thanks,
Mashall