©
Dokumen ini menggunakan Manual laman web PHP Cina Lepaskan
Example #1 Yar Server示例
<?php
class Operator {
public function add ( $a , $b ) {
return $this -> _add ( $a , $b );
}
public function sub ( $a , $b ) {
return $a - $b ;
}
public function mul ( $a , $b ) {
return $a * $b ;
}
protected function _add ( $a , $b ) {
return $a + $b ;
}
}
$server = new Yar_Server (new Operator ());
$server -> handle ();
?>
Example #2 通过浏览器访问(GET请求)
以上例程的输出类似于:
Example #3 Yar Client示例
<?php
$client = new yar_client ( "http://example.com/operator.php" );
var_dump ( $client -> add ( 1 , 2 ));
var_dump ( $client -> call ( "add" , array( 3 , 2 )));
var_dump ( $client -> _add ( 1 , 2 ));
?>
以上例程的输出类似于:
int(3) int(5) PHP Fatal error: Uncaught exception 'Yar_Server_Exception' with message 'call to api Operator::_add() failed' in *
Example #4 Yar Concurrent Client示例
<?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_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" );
Yar_Concurrent_Client :: loop ();
?>
以上例程的输出类似于:
mul result: 4 sub result: 1 add result: 3
[#1] 124960772 at qq dot com [2015-11-10 11:03:55]
<?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_Concurrent_Client::loop();
Yar_Concurrent_Client::reset();
Yar_Concurrent_Client::call("http://example.com/operator.php", "sub", array(2, 1), "callback");
Yar_Concurrent_Client::loop();
?>