Php Guzzle正在同步发送请求
我正在使用Guzzle来使用SOAP API。我必须提出6个请求,但在未来,这甚至可能是一个不确定数量的请求 问题是请求是发送同步的,而不是异步的。每个请求本身都需要+-2.5秒。当我并行发送所有6个请求时(至少我正在尝试这样做),需要+-15秒 我在Guzzle上尝试了所有的例子,一个带有$promises的固定数组,甚至还有池(我最终需要它)。当我把所有东西都放在一个文件(功能)中时,我设法将总计时时间恢复到5-6秒(哪一个是正确的?)。但当我把所有的东西都放在对象和函数中时,我做了一些让Guzzle决定再次同步它们的事情 Checks.php:Php Guzzle正在同步发送请求,php,asynchronous,concurrency,guzzle6,Php,Asynchronous,Concurrency,Guzzle6,我正在使用Guzzle来使用SOAP API。我必须提出6个请求,但在未来,这甚至可能是一个不确定数量的请求 问题是请求是发送同步的,而不是异步的。每个请求本身都需要+-2.5秒。当我并行发送所有6个请求时(至少我正在尝试这样做),需要+-15秒 我在Guzzle上尝试了所有的例子,一个带有$promises的固定数组,甚至还有池(我最终需要它)。当我把所有东西都放在一个文件(功能)中时,我设法将总计时时间恢复到5-6秒(哪一个是正确的?)。但当我把所有的东西都放在对象和函数中时,我做了一些让G
public function request()
{
$promises = [];
$promises['requestOne'] = $this->requestOne();
$promises['requestTwo'] = $this->requestTwo();
$promises['requestThree'] = $this->requestThree();
// etc
// wait for all requests to complete
$results = \GuzzleHttp\Promise\settle($promises)->wait();
// Return results
return $results;
}
public function requestOne()
{
$promise = (new API\GetProposition())
->requestAsync();
return $promise;
}
// requestTwo, requestThree, etc
API\GetProposition.php
public function requestAsync()
{
$webservice = new Webservice();
$xmlBody = '<some-xml></some-xml>';
return $webservice->requestAsync($xmlBody, 'GetProposition');
}
public function requestAsync($xmlBody, $soapAction)
{
$client = new Client([
'base_uri' => 'some_url',
'timeout' => 5.0
]);
$xml = '<soapenv:Envelope>
<soapenv:Body>
'.$xmlBody.'
</soapenv:Body>
</soapenv:Envelope>';
$promise = $client->requestAsync('POST', 'NameOfService', [
'body' => $xml,
'headers' => [
'Content-Type' => 'text/xml',
'SOAPAction' => $soapAction, // SOAP Method to post to
],
]);
return $promise;
}
公共函数requestAsync()
{
$webservice=newwebservice();
$xmlBody='';
返回$webservice->requestAsync($xmlBody,'GetProposition');
}
Webservice.php
public function requestAsync()
{
$webservice = new Webservice();
$xmlBody = '<some-xml></some-xml>';
return $webservice->requestAsync($xmlBody, 'GetProposition');
}
public function requestAsync($xmlBody, $soapAction)
{
$client = new Client([
'base_uri' => 'some_url',
'timeout' => 5.0
]);
$xml = '<soapenv:Envelope>
<soapenv:Body>
'.$xmlBody.'
</soapenv:Body>
</soapenv:Envelope>';
$promise = $client->requestAsync('POST', 'NameOfService', [
'body' => $xml,
'headers' => [
'Content-Type' => 'text/xml',
'SOAPAction' => $soapAction, // SOAP Method to post to
],
]);
return $promise;
}
公共函数requestAsync($xmlBody,$soapAction)
{
$client=新客户端([
“基本url”=>“某些url”,
“超时”=>5.0
]);
$xml='0
“.$xmlBody。”
';
$promise=$client->requestAsync('POST','NameOfService'[
“body”=>$xml,
“标题”=>[
'内容类型'=>'文本/xml',
'SOAPAction'=>$SOAPAction,//要发布到的SOAP方法
],
]);
退还$PROFECT;
}
我更改了XML和缩写的一些参数。结构是这样的,因为我最终不得不讨论多个API,这就是为什么我在它们之间有一个webservice类来完成API所需的所有准备工作。大多数API都有多个可以调用的方法/操作,这就是为什么我有类似的功能。API\GetProposition
在->wait()
语句之前,我可以看到所有的$promission都挂起了。所以看起来好像是异步发送。在->wait()
之后,它们都已完成
除了表演之外,一切都正常。所有6个请求最多只需2.5秒
希望有人能帮忙
Nick问题在于$client对象是在每次请求时创建的。导致curl multi curl无法知道要使用哪个处理程序。 通过找到答案