paolobezerr/soap_threads

使用线程发送SOAP请求的简单代码

1.1.0 2017-01-30 20:07 UTC

This package is not auto-updated.

Last update: 2024-09-29 02:34:06 UTC


README

Average time to resolve an issue Percentage of issues still open

这是一个非常简单的库,简化了SOAP线程请求。
代码可以作为pthread使用示例或分离线程中SOAP请求的真实实现。

需求

  • PHP7+
  • Pthreads 3.1.6

示例

使用pimple和Pool

<?php

$pimple = new Pimple\Container();
$pimple->register(new Soap\Provider);

// Configure and get instance of Pool.
$pimple['soap_pool_threads'] = 2;
$pimple['soap_pool_url'] = 'http://SOAP_URL?wsdl';
$soapPool = $pimple['soap_pool_factory'];

$elements = array(
    array(
        'SOAP_FUNCTION' => 'soap_fuction_name',
        'SOAP_PARAMS' => array('soap', 'function', 'params')
    ),
    array(
        'SOAP_FUNCTION' => 'soap_fuction_name',
        'SOAP_PARAMS' => array('soap', 'function', 'params')
    )
);

foreach ($elements as $item) {
    $soapThreadFunc = $pimple['soap_thread_factory'];
    $soapPool->submit(
        $soapThreadFunc($item['SOAP_FUNCTION'], $item['SOAP_PARAMS'])
    );
}

$workerCount = count($elements);

// Here we collect all soap results and put in array
$soapResults = array();
while ($soapPool->collect(function(Soap\Thread $thread) use (&$soapResults) {
        if ($thread->isGarbage()) {
            $soapResults[] = $thread->soapResult;
        }
        return $thread->isGarbage();
    }) || count($soapResults) < $workerCount) {
    continue;
};

$soapPool->shutdown();

var_dump($soapResults);

不使用pimple,使用Pool

<?php

use Soap;
use Pool;

$numberOfThreads = 2;
$workerParams = array(
    'http://SOAP_URL?wsdl',
    array('trace' => false, 'exceptions' => false) // Or anything that you need
);

$elements = array(
    array(
        'SOAP_FUNCTION' => 'soap_fuction_name',
        'SOAP_PARAMS' => array('soap', 'function', 'params')
    ),
    array(
        'SOAP_FUNCTION' => 'soap_fuction_name',
        'SOAP_PARAMS' => array('soap', 'function', 'params')
    )
);

$pool = new Pool($numberOfThreads, Worker::class, $workerParams);

foreach ($elements as $item) {
    $pool->submit(new Thread(
        $item['SOAP_FUNCTION'],
        $item['SOAP_PARAMS']
    ));
}

$workerCount = count($elements);

// Here we collect all soap results and put in array
$soapResults = array();
while ($pool->collect(function($thread) use (&$soapResults) {
        if ($thread->isGarbage()) {
            $soapResults[] = $thread->soapResult;
        }
        return $thread->isGarbage();
    }) || count($soapResults) < $workerCount) {
    continue;
};

$pool->shutdown();

var_dump($soapResults);