Tuesday, June 29, 2010

jAPI Direct - Direct call PHP methods from JavaScript

jAPI Direct contains PHP and JavaScript script that enables you to call PHP methods direct from JavaScript just by typing their names.

For example you can call PHP method that looks like this:

class MySimpleMath {

public function Addition($firstParam, $seccondParam) {
$sum = $firstParam+$seccondParam; echo $sum;
}
}
...from JavaScript just with:

MySimpleMath.Addition(1,2); And you will have as a result: "3"

So simple is in it? :)

--------------------------
jAPI Direct Implementation

To use jAPI in your project you will have to fallow a few steps only.

First download jAPI project source from this address.

Make sure that you included in your html page all scripts properly. You also have to include this two scripts in your html file: jAPI.js, jAPI-Remote.php.
jAPI-Remote.php actually can be any php script from which you want to use their server side methods.

You have to include jAPI-Core.php script in this file (jAPI-Remote.php) by adding this line of code at the beggining of your script:

include("httpHandler/jAPI-CORE.php");

And at the end of your script you will have to create a new instance of jAPI Base Class like this:

//all classes names comma separated as jAPIBaseClass parameter which you want to use

new jAPIBaseClass('YourClass,AnotherClass');

So, that's it! :)

If you have any question, feel free to ask.
vladica.savic@gmail.com

Cheers

7 comments:

Me said...

nice, small, clean, easy to use.
thx for the post!

Me said...

There seems to be a bug in this API. It's easily fixable.

The bug is in jAPI-CORE.PHP, method jAPICallFunction. Regardless of what $methodName is passed, the method information of the first method in the $reflectedData array is used.

This causes the parameters not to work when there are more parameters in one of the methods then there are in the first method. (This does not pop up in the added example, since the first method has 2 parameters, and the 3rd method has 1)

The following version of this method works. (I didn't add checking for non-existant methods.)

private function jAPICallFunction($className, $methodName) {

$reflectedData = json_decode($this->GetReflection($className));
$method_data;
foreach ( $reflectedData as $cur_method ) {
if ( $cur_method->MethodName == $methodName ) {
$method_data = $cur_method;
break;
}
}

$class =$method_data->ClassName;
$method =$method_data->MethodName;
$numberOfRequiredParameters =$method_data->MethodParams;
$scriptLocation =$method_data->ScriptLocation;

$jAPIHelper = new jAPIHelper();
$params = array();
for($counter = 1; $counter <= $numberOfRequiredParameters; $counter++) {
$params[] = $_POST['Arg'.$jAPIHelper->SpellNumber($counter)];
}

//Finaly, here we are calling
return call_user_func_array(array($className, $methodName),$params);
}

Vladica Savić said...

Thanks @Me for contributing, I will change that. ;)

DaRk452 said...

Hi thanks for the post!, has been pretty useful =). I have a question, can I return an array from a function in the remote php ?

DaRk452 said...

Hi again, I already resolve my issue, if someone else have the same problem, my solution is :

//in the remote php

return the array with json_encode

example:
class exp{
function getarray1(){

$array = array();
for($i=0;$i<10;$i++){
$array[$i] = "hi"+$i;
}

return json_encode($array);
}


now in the javascript

//we call the function

var json = exp.getarray1();
json = JSON.parse(json);

//I used for populate a combobox

combo.options[0] = new Option('--Seleccionar--', '');


alert(json);
if(json[0] != "e"){
for(var i=0;i<json.length;i++){
combo.options[combo.length] = new Option(json[i]);
}
}

Biff said...

Thanks for posting this. I found a bug that occurs when your document_root directory is a symlink of another directory. In class jAPIBaseClass, function GetReflection(), you have the following:

$scriptLocation = str_replace("\\", "/", "http://" . $_SERVER["HTTP_HOST"] . "/" . substr($reflection->getFileName(), strlen($_SERVER["DOCUMENT_ROOT"])));

If the docroot is symlinked, $reflection->getFileName() will return the the actual (non linked) file path, eg "/var/html/mydomain" However, $_SERVER['DOCUMENT_ROOT'] returns the symlinked path, eg "/www/mydomain". This causes the substr clause to return a mangled path.

The fix that worked for me was to use $_SERVER['SCRIPT_FILENAME'] instead of $reflection->getFileName(). Also had to add +1 to the substr to prevent a double slash.

$scriptLocation = str_replace("\\", "/", "http://" . $_SERVER["HTTP_HOST"] . "/" . substr($_SERVER['SCRIPT_FILENAME'], strlen($_SERVER["DOCUMENT_ROOT"])+1));

ahmed said...

i get that with the return of the php vars ::
Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method MyStringMixer::ReverseString() should not be called statically in C:\xampp\htdocs\httpHandler\jAPI-CORE.php on line 42