teknasyon/guzzle-async-pool

此包最新版本(2.0.0)没有提供许可证信息。

2.0.0 2022-09-27 12:29 UTC

This package is auto-updated.

Last update: 2024-09-27 16:58:35 UTC


README

此库旨在简化GuzzleHttp\Pool类的使用,特别是用于SOAP。

演示

要运行example目录中的示例代码,请依次执行以下命令:

$ docker run -it --rm -v $(pwd):/app composer update
$ cd example
$ docker build -t testserver .
$ docker run -d -p 8080:80 -v $(pwd):/var/www/html testserver
$ php test.php
$ php test_guzzle_pool.php

安装

要将库添加到项目中,请在composer.json文件中添加以下行,并运行composer update命令:

"require": {
    "teknasyon/guzzle-async-pool": "1.0"
}

使用

可以使用库中的类创建对SOAP服务的请求。此类的factory方法用于生成所需的GuzzleHttp\Psr7\Request类型对象。示例用法:

// Soap servisinin wsdl dosyası
$wsdl = 'http://127.0.0.1:8080/soap_server.php?wsdl';
// Soap servis adresi
$endpoint = 'http://127.0.0.1:8080/soap_server.php';
// İstekte bulunulacak SoapAction bilgisi
$soapAction = 'http://tempuri.org/Multiply';
// İstekte bulunulacak fonksiyon adı.
$functionName = 'Multiply';
// İstekte bulunulacak fonksiyon için parametreler.
$functionParams = ['intA' => 10, 'intB' => 3];
$request = SoapRequestFactory::factory(
    $wsdl,
    $endpoint,
    $soapAction,
    $functionName,
    $functionParams
);

生成的请求对象通过类发送。通过此类的方法可以监听成功的请求响应,通过方法可以监听失败的请求响应。

$requests = [
    SoapRequestFactory::factory(
        'http://127.0.0.1:8080/soap_server.php?wsdl',
        'http://127.0.0.1:8080/soap_server.php',
        'http://tempuri.org/Add',
        'Add',
        ['intA' => 10, 'intB' => 3]
    ),
    SoapRequestFactory::factory(
        'http://127.0.0.1:8080/soap_server.php?wsdl',
        'http://127.0.0.1:8080/soap_server.php',
        'http://tempuri.org/Subtract',
        'Subtract',
        ['intA' => 10, 'intB' => 3]
    ),
    SoapRequestFactory::factory(
        'http://127.0.0.1:8080/soap_server.php?wsdl',
        'http://127.0.0.1:8080/soap_server.php',
        'http://tempuri.org/Multiply',
        'Multiply',
        ['intA' => 10, 'intB' => 3]
    )
];

$guzzlePoolSettings = ['concurrency' => 5];
$guzzleClient = new Client();
$pool = new Teknasyon\GuzzleAsyncPool\Pool($requests, $guzzlePoolSettings, $guzzleClient);
$pool->onCompletedRequest(function ($index, RequestInterface $request, ResponseInterface $response) {
});
$pool->onFailedRequest(function ($index, RequestInterface $request, \Exception $exception) {
});
$pool->wait();

方法定义的函数将依次接收以下参数:

  • <$index>:请求对象在$requests数组中的索引值。
  • <$request>:请求对象。
  • <$response>:响应对象。

方法定义的函数将依次接收以下参数:

  • <$index>:请求对象在$requests数组中的索引值。
  • <$request>:请求对象。
  • <$exception>:错误对象。如果错误对象为GuzzleHttp\Exception\RequestException类型,则可以通过$exception->getResponse()访问Response对象。

SOAP请求和响应的转换

类将根据指定的参数自动准备XML内容,并使用该内容创建Request对象。无需进行特殊操作。但是,对于SOAP响应,您必须使用类。该类可以将接收到的XML响应转换为PHP数组。您可以通过类接收到的Psr\Http\Message\ResponseInterface类型对象来转换响应。

$pool->onCompletedRequest(function ($index, RequestInterface $request, ResponseInterface $response) use ($startTime) {
    $soapResponse = Decoder::decode($response->getBody()->getContents());
    ...
});
$pool->onFailedRequest(function ($index, RequestInterface $request, \Exception $exception) use ($startTime) {
    $soapResponse = null;
    if ($exception instanceof RequestException) {
        $soapResponse = Decoder::decode($exception->getResponse()->getBody()->getContents());
    }
    ...
});

Guzzle配置

类的第二个参数接受配置,第三个参数接受对象。您可以通过这些参数更新Guzzle文档中提到的任何配置。最常用的配置是配置。此配置限制了一次性可以发送的请求数量。您需要在第二个参数中指定所需数组的列表。