-
PHP 7.0+ (master branch)) -
PHP 5.2+ ( php5 branch ) -
Curl -
Json -
Msgpack (Optional)
-
Fast, Easy, Simple -
Concurrent RPC calls -
Multiple data packager supported (php, json, msgpack built-in) -
Multiple transfer protocols supported (http, https, TCP) -
Detailed debug informations
pecl install yar
$/path/to/phpize $./ configure --with-php-config=/path/to/php-config/ $make && make install
--with-curl=DIR --enable(disable)-msgpack --enable(disable)-epoll (require Yar 2.1.2)
-
Install msgpack for PHP extension:
pecl install msgpack
apt-get install msgpack-php
-
configuration:
$phpize $configure --with-php-config=/path/to/php-config/ --enable-msgpack $make && make install
-
yar.timeout //default 5000 (ms) -
yar.connect_timeout //default 1000 (ms) -
yar.packager //default "php", when built with --enable-msgpack then default "msgpack", it should be one of "php", "json", "msgpack" -
yar.debug //default Off -
yar.expose_info // default On, whether output the API info for GET requests -
yar.content_type // default "application/octet-stream" -
yar.allow_persistent // default Off
-
YAR_VERSION -
YAR_OPT_PACKAGER -
YAR_OPT_PERSISTENT -
YAR_OPT_TIMEOUT -
YAR_OPT_CONNECT_TIMEOUT -
YAR_OPT_HEADER // Since 2.0.4 -
YAR_OPT_PROXY //Since 2.2.0 -
YAR_OPT_PROVIDER //Since 2.3.0 -
YAR_OPT_TOKEN //Since 2.3.0
<? php
class API { /**
* the doc info will be generated automatically into service info page.
* @params
* @return
*/
public function some_method ( $ parameter , $ option = " foo " ) { } protected function client_can_not_see () { } } $ service = new Yar_Server ( new API ()); $ service -> handle (); ?>
<? php
class API { protected function __info ( $ markup ) { return " Hello world " ; } }
<? php
class API { protected function __auth ( $ provider , $ token ) { return verify ( $ provider , $ token ); }
<? php
$ client -> setOpt ( YAR_OPT_PROVIDER , " provider " ); $ client -> setOpt ( YAR_OPT_TOKEN , " token " ); $ client -> call ();
<? php
$ client = new Yar_Client ( " http://host/api/ " ); /* the following setopt is optinal */
$ client -> SetOpt ( YAR_OPT_CONNECT_TIMEOUT , one thousand ); $ client -> SetOpt ( YAR_OPT_HEADER , array ( " hd1: val " , " hd2: val " )); //Custom headers, Since 2.0.4
/* call remote service */
$ result = $ client -> some_method ( " parameter " ); ?>
<? php
function callback ( $ retval , $ callinfo ) { var_dump ( $ retval ); } function error_callback ( $ type , $ error , $ callinfo ) { error_log ( $ error ); } Yar_Concurrent_Client :: call ( " http://host/api/ " , " some_method " , array ( " parameters " ), " callback " ); Yar_Concurrent_Client :: call ( " http://host/api/ " , " some_method " , array ( " parameters " )); // if the callback is not specificed,
// callback in loop will be used
Yar_Concurrent_Client :: call ( " http://host/api/ " , " some_method " , array ( " parameters " ), " callback " , " error_callback " , array ( YAR_OPT_PACKAGER => " json " )); //this server accept json packager
Yar_Concurrent_Client :: call ( " http://host/api/ " , " some_method " , array ( " parameters " ), " callback " , " error_callback " , array ( YAR_OPT_TIMEOUT => one )); //custom timeout
Yar_Concurrent_Client :: loop ( " callback " , " error_callback " ); //send the requests,
//the error_callback is optional
?>
<? php
$ client = new Yar_Client ( " http://host/api/ " ); $ client -> SetOpt ( YAR_OPT_PERSISTENT , one ); $ result = $ client -> some_method ( " parameter " ); /* The following calls will speed up due to keep-alive */
$ result = $ client -> some_other_method1 ( " parameter " ); $ result = $ client -> some_other_method2 ( " parameter " ); $ result = $ client -> some_other_method3 ( " parameter " ); ?>
<? php
$ client = new Yar_Client ( " http://host/api/ " ); $ client -> SetOpt ( YAR_OPT_RESOLVE , array ( " host:80:127.0.0.1 " )); /* call goes to 127.0.0.1 */
$ result = $ client -> some_method ( " parameter " );
<? php
$ client = new Yar_Client ( " http://host/api/ " ); $ client -> SetOpt ( YAR_OPT_PROXY , " 127.0.0.1:8888 " ); //http proxy , Since 2.2.0
/* call goes to 127.0.0.1 */
$ result = $ client -> some_method ( " parameter " );
#ifdef PHP_WIN32
#pragma pack(push) #pragma pack(1) #endif
typedef struct _yar_header { uint32_t id ; // transaction id
uint16_t version ; // protocol version
uint32_t magic_num ; // default is: 0x80DFEC60
uint32_t reserved ; unsigned char provider [ thirty-two ]; // reqeust from who
unsigned char token [ thirty-two ]; // request token, used for authentication
uint32_t body_len ; // request body len } #ifndef PHP_WIN32
__attribute__ ( ( packed )) #endif
yar_header_t ; #ifdef PHP_WIN32
#pragma pack(pop) #endif
<? php
array ( " i " => '' , //transaction id
" m " => '' , //the method which being called
" p " => array (), //parameters )
<? php
array ( " i " => '' , " s " => '' , //status
" r " => '' , //return value
" o " => '' , //output
" e " => '' , //error or exception )