CakeFest 2024: The Official CakePHP Conference

Örnekler

Örnek 1 Yar Server Example

<?php

/* assume this page can be accessed by http://example.com/operator.php */

class Operator {

/**
* Add two operands
* @param interge
* @return interge
*/
public function add($a, $b) {
return
$this->_add($a, $b);
}

/**
* Sub
*/
public function sub($a, $b) {
return
$a - $b;
}

/**
* Mul
*/
public function mul($a, $b) {
return
$a * $b;
}

/**
* Protected methods will not be exposed
* @param interge
* @return interge
*/
protected function _add($a, $b) {
return
$a + $b;
}
}

$server = new Yar_Server(new Operator());
$server->handle();
?>

Örnek 2 Access the server in borwser(GET request)

Yukarıdaki örnek şuna benzer bir çıktı üretir:

Yar Server Info

Örnek 3 Yar Client Example

<?php
$client
= new yar_client("http://example.com/operator.php");

/* call directly */
var_dump($client->add(1, 2));

/* call via call */
var_dump($client->call("add", array(3, 2)));


/* __add can not be called */
var_dump($client->_add(1, 2));
?>

Yukarıdaki örnek şuna benzer bir çıktı üretir:

int(3)
int(5)
PHP Fatal error:  Uncaught exception 'Yar_Server_Exception' with message 'call to api Operator::_add() failed' in *

Örnek 4 Yar Concurrent Client Example

<?php
function callback($ret, $callinfo) {
echo
$callinfo['method'] , " result: ", $ret , "\n";
}

/* register async call to remote services */
Yar_Concurrent_Client::call("http://example.com/operator.php", "add", array(1, 2), "callback");
Yar_Concurrent_Client::call("http://example.com/operator.php", "sub", array(2, 1), "callback");
Yar_Concurrent_Client::call("http://example.com/operator.php", "mul", array(2, 2), "callback");

/* sent all request and wait for response */
Yar_Concurrent_Client::loop();
?>

Yukarıdaki örnek şuna benzer bir çıktı üretir:

mul result: 4
sub result: 1
add result: 3
add a note

User Contributed Notes 1 note

up
-2
124960772 at qq dot com
8 years ago
<?php
function callback($ret, $callinfo) {
echo
$callinfo['method'] , " result: ", $ret , "\n";
}

/* 注册一个异步调用 */
Yar_Concurrent_Client::call("http://example.com/operator.php", "add", array(1, 2), "callback");
/* 发送所有注册的调用, 等待返回, 返回后Yar会调用callback回掉函数 */
Yar_Concurrent_Client::loop();
/* 重置call ,否则上面的call会调用*/
Yar_Concurrent_Client::reset();
Yar_Concurrent_Client::call("http://example.com/operator.php", "sub", array(2, 1), "callback");
Yar_Concurrent_Client::loop();

?>
To Top