從Zend是真的不平安,不幸的是,你最好的選擇是JSON-RPC。
你可以做一個控制器,或者你可以讓另外一個ajax.php你的index.php減少這樣的傢伙開銷確實here
基本上,所有你需要做的是這樣的:
$server = new Zend_Json_Server();
$server->setClass('My_Class_With_Public_Methods');
// I've found that a lot of clients only support 2.0
$server->getRequest()->setVersion("2.0");
if ('GET' == $_SERVER['REQUEST_METHOD']) {
// Indicate the URL endpoint, and the JSON-RPC version used:
$server->setTarget('/ajax.php')
->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
// Grab the SMD
$smd = $server->getServiceMap();
// Return the SMD to the client
header('Content-Type: application/json');
echo $smd;
return;
}
$server->handle();
然後在某個地方佈局:
$server = new Zend_Json_Server();
$server->setClass('My_Class_With_Public_Methods');
$smd = $server->getServiceMap();
?>
<script>
$(document).ready(function() {
rpc = jQuery.Zend.jsonrpc({
url : <?=json_encode($this->baseUrl('/ajax'))?>
, smd : <?=$smd?>
, async : true
});
});
</script>
例如起見,下面是該類:
class My_Class_With_Public_Methods {
/**
* Be sure to properly phpdoc your methods,
* the rpc clients like it when you do
*
* @param float $param1
* @param float $param2
* @return float
*/
public function someMethodInThatClass ($param1, $param2) {
return $param1 + $param2;
}
}
,那麼你可以簡單的調用方法,像這樣在javascript:
rpc.someMethodInThatClass(first_param, second_param, {
// if async = true when you setup rpc,
// then the last param is an object w/ callbacks
'success' : function(data) {
}
'error' : function(data) {
}
});
不會有很多衆所周知的JSON-RPC庫爲Android/iPhone - 但我發現這一點也適用Zend_Json_Server爲Android:
http://software.dzhuvinov.com/json-rpc-2.0-base.html
,這適用於iPhone:
http://www.dizzey.com/development/ios/calling-json-rpc-webservice-in-ios/
從這裏,很明顯,你可以以同樣的方式,使用Javascript /您的移動應用程序確實使用My_Class_With_Public_Methods。
那你最終會使用嗎?我現在處於類似的情況。 – 2012-10-12 18:04:07