Posted By

halk on 11/05/12


Tagged

ajax php api


Versions (?)

PHP API Header For Function Libraray (Call Any PHP Functions Via Ajax)


 / Published in: PHP
 

Place your functions below the header and then call them by posting to the script with any kind of ajax : $('#test').load('base/ajax/ajaxapi.php',{method:'sha256',params:"'batman','gargamelhatessmurfs'"}); for example.

  1. /**
  2.  * AJAX API FOR PHP FUNCTION LIBRARY
  3.  */
  4. if(function_exists(stripslashes(trim($_POST['method'])))){ //IF THE FUNCTION EXISTS (IN THIS SCRIPT)
  5. $method = stripslashes(trim($_POST['method']));
  6. $params = str_replace("'", '',stripslashes(trim($_POST['params']))); //strip single quotes if used
  7. $opts= explode(',',$params); //turn the parameters string into an array
  8. $function = new ReflectionFunction($method); //instantiate the function as an object
  9. $function->invokeArgs($opts); //invoke the function with an array of arguments (if given)
  10. }else{ //ELSE THE FUNCTION DOES NOT EXIST
  11. echo "error the function you called : ".$_POST['method']."(".$_POST['params'].")"." does not exist";
  12. }
  13. /////////////////////////////////////////////////////////////////////////////////////////////////
  14. /////////////////////////////////////////////////////////////////////////////////////////////////
  15. // PLACE YOUR FUNCTIONS BELOW HERE. //
  16. /////////////////////////////////////////////////////////////////////////////////////////////////
  17. /////////////////////////////////////////////////////////////////////////////////////////////////

Report this snippet  

You need to login to post a comment.