# Yar - Yet Another RPC framework for PHP
[![Build Status](https://secure.travis-ci.org/laruence/yar.png)](http://travis-ci.org/laruence/yar) [![Build status](https://ci.appveyor.com/api/projects/status/syhw33wlt2nad64i/branch/master?svg=true)](https://ci.appveyor.com/project/laruence/yar/branch/master) [![Build Status](https://github.com/laruence/yar/workflows/integrate/badge.svg?branch=master)](https://github.com/laruence/yar/actions?query=workflow%3Aintegrate)
Light, concurrent RPC framework for PHP(see also: [Yar C framework](https://github.com/laruence/yar-c), [Yar Java framework](https://github.com/weibocom/yar-java))
## Requirement
- PHP 7.0+ (master branch))
- PHP 5.2+ ([php5 branch](https://github.com/laruence/yar/tree/php5))
- Curl
- Json
- Msgpack (Optional)
## Introduction
Yar is a RPC framework which provides a simple and easy way to do communication between PHP applications, it also offers an ability of doing multiple calls to remote services concurrently.
## Features
- 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
## Install
### Install Yar
Yar is an PECL extension, could be installed simply by:
```
pecl install yar
```
### Compile Yar in Linux
```
$/path/to/phpize
$./configure --with-php-config=/path/to/php-config/
$make && make install
```
Available instructions to configure are
```
--with-curl=DIR
--enable(disable)-msgpack
--enable(disable)-epoll (require Yar 2.1.2)
```
### Install Yar with msgpack
1. Install msgpack for PHP extension:
```
pecl install msgpack
```
or for ubuntu user
```
apt-get install msgpack-php
```
or , you can get the github source here: https://github.com/msgpack/msgpack-php
2. configuration:
```
$phpize
$configure --with-php-config=/path/to/php-config/ --enable-msgpack
$make && make install
```
## Runtime Configure
- 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
*NOTE* yar.connect_time is a value in milliseconds, which was measured in seconds before 1.2.1.
## Constants
- 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
## Server
It's very easy to setup a Yar HTTP RPC Server
```php
<?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();
?>
```
Usual RPC calls are issued as HTTP POST requests.
If a HTTP GET request is issued to the uri(access the api address directly via a browser), the service information (commented section above) will be returned, like:
![yar service info page](https://github.com/laruence/laruence.github.com/raw/master/yar_server.png)
### Custom server info
Since 2.3.0, Yar allows you to custom the output in above example by defining "__info" magic method:
```php
<?php
class API {
protected function __info($markup) {
return "Hello world";
}
}
```
then If a HTTP GET request is issued, "hello world" will be sent instead.
### Authentication
Since 2.3.0, Yar allows server to authentic client request by Provider/Token fileds in header, for achieve this, you should define a protected magic method named "__auth" in server side:
```php
<?php
class API {
protected function __auth($provider, $token) {
return verify($provider, $token);
}
```
*NOTE* __auth method should always be defined as protected
if a Yar server has __auth defined, then __auth will be called at the very first time for any request,
if __auth method return true, the request will be processed further, otherwise the request will be terminated by an error of "authentication failed"
in clent side, you can specific the provider/token by:
```php
<?php
$client->setOpt(YAR_OPT_PROVIDER, "provider");
$client->setOpt(YAR_OPT_TOKEN, "token");
$client->call();
```
## Client
It's very simple for a PHP client to call remote RPC:
### Synchronous call
```php
<?php
$client = new Yar_Client("http://host/api/");
/* the following setopt is optinal */
$client->SetOpt(YAR_OPT_CONNECT_TIMEOUT, 1000);
$client->SetOpt(YAR_OPT_HEADER, array("hd1: val", "hd2: val")); //Custom headers, Since 2.0.4
/* call remote service */
$result = $client->some_method("parameter");
?>
```
### Concurrent call
```php
<?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=>1));
//custom timeout
Yar_Concurrent_Client::loop("callback", "error_callback"); //send the requests,
//the error_callback is optional
?>
```
### Persistent call
After Yar 2.1.0, if YAR_OPT_PERSISTENT is set to true, then Yar is able to use HTTP keep-alive to speedup repeated calls to a same address, the link will be released at the end of the PHP request lifecycle.
```php
<?php
$client = new Yar_Client("http://host/api/");
$client->SetOpt(YAR_OPT_PERSISTENT, 1);
$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");
?>
```
### Custom hostname resolving
After Yar 2.1.0, if Yar runs on HTTP protocol, YAR_OPT_RESOLVE could be used to define custom hostname resolving.
```php
<?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");
```
### Use http proxy
After Yar 2.2.1, if Yar runs on HTTP protocol, YAR_OPT_PROXY could be used to define http proxy , such as fidder or charles.
```php
<?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");
```
## Protocols
### Yar Header
Yar is no only designed for PHP only, all RPC request and response are transferred by binary data stream.
Key messages are exchanged by a struct called "Yar Header":
```C
#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[32]; // reqeust
没有合适的资源?快使用搜索试试~ 我知道了~
Light, concurrent RPC framework for PHP & C.zip
共98个文件
phpt:55个
c:14个
h:12个
需积分: 5 0 下载量 153 浏览量
2023-12-31
11:00:46
上传
评论
收藏 99KB ZIP 举报
温馨提示
Light, concurrent RPC framework for PHP & C
资源推荐
资源详情
资源评论
收起资源包目录
Light, concurrent RPC framework for PHP & C.zip (98个子文件)
sss
yar_server.h 2KB
transports
curl.c 28KB
socket.c 12KB
yar_transports.h 2KB
tools
yar_debug.php 1KB
yar_debug.inc 5KB
yar_response.c 5KB
yar.c 7KB
yar_client.c 30KB
config.m4 3KB
.travis.yml 303B
.github
workflows
integrate.yml 1KB
yar.php 493B
yar_legacy_arginfo.h 2KB
yar_response.h 2KB
yar_client.h 2KB
LICENSE 3KB
tests
003.phpt 438B
issue172.phpt 1KB
042.phpt 849B
034.phpt 1KB
048.phpt 746B
010.phpt 721B
041.phpt 590B
045.phpt 918B
017.phpt 930B
019.phpt 875B
030.phpt 1KB
012.phpt 3KB
011.phpt 1KB
046.phpt 593B
015.phpt 797B
040.phpt 819B
027.phpt 819B
025.phpt 1KB
009.phpt 531B
049.phpt 724B
036.phpt 1KB
029.phpt 453B
006.phpt 1KB
051.phpt 593B
bug74867.phpt 530B
yar.inc 9KB
023.phpt 671B
028.phpt 811B
020.phpt 824B
037.phpt 449B
053.phpt 922B
007.phpt 401B
018.phpt 711B
043.phpt 886B
008.phpt 509B
026.phpt 823B
054.phpt 825B
033.phpt 1KB
016.phpt 1KB
022.phpt 891B
014.phpt 900B
004.phpt 810B
021.phpt 463B
031.phpt 684B
002.phpt 709B
035.phpt 2KB
052.phpt 929B
013.phpt 1KB
047.phpt 1KB
050.phpt 680B
039.phpt 757B
001.phpt 538B
032.phpt 675B
038.phpt 658B
024.phpt 945B
005.phpt 488B
package.xml 16KB
yar_request.h 2KB
yar_request.c 4KB
yar_server.c 25KB
yar_exception.c 6KB
yar_packager.c 5KB
.appveyor.yml 8KB
yar_transport.h 4KB
yar_protocol.h 2KB
yar.stub.php 2KB
yar_protocol.c 2KB
config.w32 1KB
yar_exception.h 3KB
CREDITS 60B
.gitignore 118B
php_yar.h 4KB
EXPERIMENTAL 0B
travis
compile.sh 41B
README.md 9KB
yar_arginfo.h 2KB
packagers
json.c 2KB
msgpack.c 2KB
php.c 2KB
yar_packager.h 3KB
yar_transport.c 3KB
共 98 条
- 1
资源评论
暮苍梧~
- 粉丝: 41
- 资源: 258
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功