overblog/wsclient-bundle

此包的最新版本(v0.6)没有可用的许可证信息。

Web Service 客户端

v0.6 2016-02-05 16:54 UTC

This package is auto-updated.

Last update: 2024-08-27 13:18:25 UTC


README

这个仓库是什么?

这是Web Service客户端库的存放地。主要用于与外部Rest服务和JSON-RPC(内部API)配合使用。

安装和配置

只需在您的deps文件中添加以下行

[OverblogWsClientBundle]
    git=git@github.com:ebuzzing/OverblogWsClientBundle.git
    target=/bundles/Overblog/WsClientBundle
    version=v0.4

现在您需要告诉Symfony2自动加载器在哪里找到API和将要生成的文件。打开您的文本编辑器,并在app/autoload.php文件中添加以下行

#app/autoload.php

    'Overblog'         => __DIR__.'/../vendor/bundles',

现在我们在应用内核中注册WsClientBundle

#app/AppKernel.php
    // register your bundles
    new Overblog\WsClientBundle\OverblogWsClientBundle(),

您现在可以在主配置文件中定义服务设置。以下示例使用yaml格式

# app/config/config.yml
overblog_ws_client:
    urls:
        *cnct_name*:
          url: http://api.domain.tld/
          type: rest
          timeout: 2000
        *cnct_name_2*:
          url: http://api.domain.tld/json-rpc/user
          type: json

这里的cnct_name是您服务的名称。您可以使用不同的名称在多个Web服务上定义多个服务...

用法

使用ws客户端非常简单。一个简单的Rest调用看起来像这样

$results = $this->get('ws_client')
            ->getConnection('*cnct_name*')
                ->get('/ws/uri')
            ->exec();

您可以通过链式调用一起调用多个服务,如下所示

$results = $this->get('ws_client')
            ->getConnection('*cnct_name_2*')
                ->get('getMethod', array('param' => 'value'))
                ->get('setMethod', array('param' => 'value'))
            ->getConnection('*cnct_name*')
                ->get('/ws/uri')
            ->exec();

调用执行时间将是耗时最长的调用。