PHPackages                             uniondrug/service - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. uniondrug/service

ActiveProject

uniondrug/service
=================

Uniondrug Micro Service Component for uniondrug/framework

2.7.10(6y ago)06.6k12proprietaryPHP

Since Nov 14Pushed 6y ago2 watchersCompare

[ Source](https://github.com/uniondrug/service)[ Packagist](https://packagist.org/packages/uniondrug/service)[ RSS](/packages/uniondrug-service/feed)WikiDiscussions 2.x Synced 3d ago

READMEChangelog (10)Dependencies (2)Versions (25)Used By (2)

药联微服务协议封装
=========

[](#药联微服务协议封装)

Uniondrug微服务服务端和客户端的封装。基于HTTP的微服务报文格式形如：

```
{
    "errno": "0",
    "error": "",
    "dataType": "OBJECT",
    "data": {
        "do2": {
            "from": "do1"
        },
        "do3": {
            "from": "do1"
        }
    }
}
```

通过封装后，在服务端，通过Server的系列方法直接产生上述格式的响应报文。在客户端，通过Client的方法，发起请求，对上述报文自动解析封装，方便使用。

安装
--

[](#安装)

```
$ cd project-home
$ composer require uniondrug/service
```

修改 `app.php` 配置文件，注入服务。服务名称：服务端 `serviceServer`，客户端 `serviceClient`。

```
return [
    'default' => [
        ......
        'providers'           => [
            ......
            \Uniondrug\Service\ServiceServiceProvider::class,
        ],
    ],
];
```

客户端（Client）
-----------

[](#客户端client)

### 使用方法

[](#使用方法)

> 发起请求

方法：

- `delete`(`serviceName`, `route`, `query[]`, `body[]`, `extra[]`)
- `get`(`serviceName`, `route`, `query[]`, `body[]`, `extra[]`)
- `head`(`serviceName`, `route`, `query[]`, `body[]`, `extra[]`)
- `options`(`serviceName`, `route`, `query[]`, `body[]`, `extra[]`)
- `patch`(`serviceName`, `route`, `query[]`, `body[]`, `extra[]`)
- `post`(`serviceName`, `route`, `query[]`, `body[]`, `extra[]`)
- `put`(`serviceName`, `route`, `query[]`, `body[]`, `extra[]`)

说明：

- `serviceName` 服务名称，在注册中心注册的服务名称，发起请求时，会自动从注册中心查找该服务的节点。
- `route` 是服务路由，通常就是应用服务的控制器路由。比如`/member/info`。
- `query[]` 是数组格式的参数，如果有，则以QueryString的方式发送。
- `body[]` 是数组格式的参数，如果有，默认以JSON的格式发送。如果想改变，则使用`extra[]`设定。
- `extra[]` 是数组格式的参数，包括： -- `type`: `body`的发送方式，模式是`json`，可选的有：`form`: 以form-url-encoded的方式发送；`multipart`：以form-data的方式发送。 -- `timeout`: 发送请求的超时，单位秒 -- `connect_timeout`: 连接节点的超时时间，单位秒 -- `headers`: 数组，可选的头信息。

> 返回值

请求成功则返回一个`Uniondrug\Service\RequestReader`对象：

方法：

- `response()` 获得原始的Response对象。
- `data()` 获得服务返回的结构化结果。也就是`data`里面的内容。
- `headers()` 获得返回结果的头信息。
- `getContents()` 获得返回的Body内容。
- `getDuration()` 获得请求用的时间。
- `hasError()` 返回结果是否有错（应用错误，而非Http请求的错误。Http请求出错，会抛出异常）。
- `getErrno()` 获得错误代码
- `getError()` 获得错误信息

举例：

```
public function postAction()
{
    $serviceName = 'serviceName';
    $route = '/route/action';
    $query = ["page" => 1];
    $body = ["userId" => 1, "options" => ["key" => "value"]];
    $return = $this->serviceClient->post($serviceName, $route, $query, $body);

    print_r($return->data());
}
```

服务端
---

[](#服务端)

在服务端的控制器，使用Server封装产生指定格式的响应报文体。

### 使用方法

[](#使用方法-1)

> 方法

- `Phalcon\Http\Response` - **withError**(`string` **$error**, `int` **$errno** = 1)
- `Phalcon\Http\Response` - **withStruct**(`Uniondrug\Structs\StructInterface` **$struct**)

> 举例

```
public function postAction()
{
    // codes ignored
    $this->serviceServer->withError("错误原因", 1);
}
```

###  Health Score

35

—

LowBetter than 79% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 65% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~39 days

Recently: every ~180 days

Total

24

Last Release

2198d ago

Major Versions

1.7 → 2.02018-02-27

### Community

Maintainers

![](https://www.gravatar.com/avatar/865ceb7d8226574735eab8fc82de9cfbc4a708c291b3c816e75d53944348b686?d=identicon)[wsfuyibing](/maintainers/wsfuyibing)

---

Top Contributors

[![fuyibing](https://avatars.githubusercontent.com/u/8390620?v=4)](https://github.com/fuyibing "fuyibing (26 commits)")[![xueron](https://avatars.githubusercontent.com/u/2560873?v=4)](https://github.com/xueron "xueron (14 commits)")

---

Tags

phalcon

### Embed Badge

![Health badge](/badges/uniondrug-service/health.svg)

```
[![Health](https://phpackages.com/badges/uniondrug-service/health.svg)](https://phpackages.com/packages/uniondrug-service)
```

###  Alternatives

[phalcon/incubator

Adapters, prototypes or functionality that can be potentially incorporated to the C-framework.

7222.9M81](/packages/phalcon-incubator)[phalcon/ide-stubs

The most complete Phalcon Framework IDE stubs library which enables autocompletion in modern IDEs.

1623.1M121](/packages/phalcon-ide-stubs)[mattiasgeniar/phpunit-query-count-assertions

A custom assertion for phpunit that allows you to count the amount of SQL queries used in a test. Can be used to enforce certain performance characteristics (ie: limit queries to X for a certain action).

160730.9k2](/packages/mattiasgeniar-phpunit-query-count-assertions)[oleksandr-torosh/yona-cms

Yona CMS - open source content management system (CMS). Written in Phalcon PHP Framework (v 1.3.x). Has a convenient modular structure. Has simple configuration and architecture. Can be easily modified for any task with any loads.

3652.1k](/packages/oleksandr-torosh-yona-cms)[gte451f/phalcon-json-api-package

A set of tools designed for use in a Phalcon application to make a RESTish API

372.7k](/packages/gte451f-phalcon-json-api-package)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
