在Module
Zend Framework 2 应用程序的一个类中,我将 HTTP 请求发送到一个特殊的端点以获取有关用户的一些信息。当我在工厂内执行此操作时(如下代码中的第一种方法),它可以工作。它也适用于Module#onBootstrap(...)
(第二种方法)中的侦听器定义。但是当我尝试直接从Module#onBootstrap(...)
(s. the 3d approach)执行调用时,调用失败并出现错误:
PHP Fatal error: Uncaught exception 'Zend\\Http\\Client\\Adapter\\Exception\\TimeoutException' with message 'Read timed out after 30 seconds' in /var/www/path/to/project/vendor/zendframework/zend-http/src/Client/Adapter/Socket.php:600
Stack trace:
#0 /var/www/path/to/project/vendor/zendframework/zend-http/src/Client/Adapter/Socket.php(412): Zend\\Http\\Client\\Adapter\\Socket->_checkSocketReadTimeout()
#1 /var/www/path/to/project/vendor/zendframework/zend-http/src/Client.php(1389): Zend\\Http\\Client\\Adapter\\Socket->read()
#2 /var/www/path/to/project/vendor/zendframework/zend-http/src/Client.php(893): Zend\\Http\\Client->doRequest(Object(Zend\\Uri\\Http), 'POST', false, Array, '')
#3 /var/www/path/to/project/module/MyApi/src/MyApi/Module.php(158): Zend\\Http\\Client->send()
#4 /var/www/path/to/project/module/MyApi/src/MyApi/Module.php(33): MyApi\\Module->retrieveUserInfosEndpoint(Object(ZF\\ContentNegotiation\\Request), 'http://my-project...')
#5 [intern in /var/www/path/to/project/vendor/zendframework/zend-http/src/Client/Adapter/Socket.php on line 600
为什么会发生错误?如何从 Module#onBootstrap(...) 发送 HTTP 请求?
namespace MyModule;
...
class Module
{
protected $userInfo;
public function onBootstrap(MvcEvent $mvcEvent)
{
// 3d approach -- it does NOT work
$userInfoEndpointUrl = $serviceManager->get('Config')['user_info_endpoint_url'];
$request = $serviceManager->get('Request');
$this->userInfo = $this->retrieveUserInfosEndpoint($request, $userInfoEndpointUrl);
...
$halPlugin->getEventManager()->attach('myeventname', function ($event) use (..., $serviceManager) {
// 2nd approach -- it works
$userInfoEndpointUrl = $serviceManager->get('Config')['user_info_endpoint_url'];
$request = $serviceManager->get('Request');
$this->userInfo = $this->retrieveUserInfosEndpoint($request, $userInfoEndpointUrl);
/*
PHP Fatal error:
Uncaught exception 'Zend\\Http\\Client\\Adapter\\Exception\\TimeoutException'
with message 'Read timed out after 30 seconds'
in /var/www/path/to/project/vendor/zendframework/zend-http/src/Client/Adapter/Socket.php:600
*/
...
});
...
}
...
public function getServiceConfig()
{
return array(
'factories' => array(
'MyModule\\V1\\Rest\\Foo\\FooService' => function(ServiceManager $serviceManager) {
// 1st approach -- it works
$userInfoEndpointUrl = $serviceManager->get('Config')['user_info_endpoint_url'];
$request = $serviceManager->get('Request');
$this->userInfo = $this->retrieveUserInfosEndpoint($request, $userInfoEndpointUrl);
...
return $fooService;
},
...
),
...
);
}
private function retrieveUserInfosEndpoint($request, $userInfoEndpointUrl)
{
$authorizationHeaderValue = $request->getHeader('Authorization')->getFieldValue();
$client = new Client();
$client->setUri($userInfoEndpointUrl);
$client->setMethod('POST');
$client->setOptions(['sslverifypeer' => false]);
$client->setHeaders(['Authorization' => $authorizationHeaderValue]);
$client->setOptions([
'maxredirects' => 10,
'timeout' => 30,
]);
$client->setParameterPost([]);
$response = $client->send();
$userInfo = json_decode($response->getContent(), true);
return $userInfo;
}
}