zf2 socket http适配器无法正常工作

时间:2014-10-02 06:39:17

标签: sockets rss zend-framework2

我正在尝试使用rss feed,在SO上,我找到了以下代码:

try {

    $rss = Reader::import('http://www.wdcdn.net/rss/presentation/library/client/skunkus/id/cc3d06c1cc3834464aef22836c55d13a');
} catch (Exception\RuntimeException $e) {
    echo "error : " . $e->getMessage();
    exit;
}

$channel = array(
    'title' => $rss->getTitle(),
    'description' => $rss->getDescription(),
    'link' => $rss->getLink(),
    'items' => array()
);

foreach ($rss as $item) {
    $channel['items'][] = array(
        'title' => $item->getTitle(),
        'link' => $item->getLink(),
        'description' => $item->getDescription(),
        // 'image'       => $item->getImage(),
    );
}

return new  ViewModel(array(
    'channel' => $channel
));

当我尝试运行代码时,出现以下错误

Zend\Http\Client\Adapter\Exception\RuntimeException

Fichier:
C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Http\Client\Adapter\Socket.php:258
Message:
Unable to connect to www.wdcdn.net:80 . Error #0: stream_socket_client(): 
Pile d'exécution:
#0 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Http\Client.php(1358): Zend\Http\Client\Adapter\Socket->connect('www.wdcdn.net', 80, false)
#1 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Http\Client.php(878): Zend\Http\Client->doRequest(Object(Zend\Uri\Http), 'GET', false, Array, '')
#2 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Feed\Reader\Reader.php(249): Zend\Http\Client->send()
#3 C:\Workspace\app\intranet\module\Application\src\Application\Controller\IndexController.php(60): Zend\Feed\Reader\Reader::import('http://www.wdcd...')
#4 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Mvc\Controller\AbstractActionController.php(83): Application\Controller\IndexController->homeAction()
#5 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#6 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#7 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#8 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Mvc\Controller\AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#9 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Mvc\DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#10 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#11 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#12 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#13 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Mvc\Application.php(316): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#14 C:\Workspace\app\intranet\public\index.php(12): Zend\Mvc\Application->run()
#15 {main}
Exceptions précédentes:

ErrorException

Fichier:
C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Http\Client\Adapter\Socket.php:253
Message:
stream_socket_client(): 
Pile d'exécution:
#0 [internal function]: Zend\Stdlib\ErrorHandler::addError(2, 'stream_socket_c...', 'C:\Workspace\df...', 253, Array)
#1 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Http\Client\Adapter\Socket.php(253): stream_socket_client('www.wdcdn.net:8...', 10061, 'Aucune connexio...', 10, 4, Resource id #207)
#2 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Http\Client.php(1358): Zend\Http\Client\Adapter\Socket->connect('www.wdcdn.net', 80, false)
#3 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Http\Client.php(878): Zend\Http\Client->doRequest(Object(Zend\Uri\Http), 'GET', false, Array, '')
#4 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Feed\Reader\Reader.php(249): Zend\Http\Client->send()
#5 C:\Workspace\app\intranet\module\Application\src\Application\Controller\IndexController.php(60): Zend\Feed\Reader\Reader::import('http://www.wdcd...')
#6 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Mvc\Controller\AbstractActionController.php(83): Application\Controller\IndexController->homeAction()
#7 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#8 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#9 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#10 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Mvc\Controller\AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#11 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Mvc\DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#12 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#13 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\EventManager\EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#14 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\EventManager\EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#15 C:\Workspace\app\intranet\vendor\ZF2\library\Zend\Mvc\Application.php(316): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#16 C:\Workspace\app\intranet\public\index.php(12): Zend\Mvc\Application->run()
#17 {main}

我试图深入研究Zend代码,如果我将默认的ZendHttp \ Client更改为cURL,那么每件事情都可以。

我的问题是,为什么套接字引发错误#0 (来自php.net:如果errno中返回的值为0且函数返回FALSE,则表明在connect()调用之前发生错误。

1 个答案:

答案 0 :(得分:1)

Zend的Feed Reader有一个静态setClient()方法,此方法适用于任何具有实现AdapterInterface的适配器的http客户端。

所以你可以轻松尝试这样的事情:

$config = array(
   'adapter' => 'Zend\Http\Client\Adapter\Curl',
   );

$client = new HttpClient(null, $config);
Reader::setHttpClient($client);
$feed = Reader::import('http://www.wdcdn.net/rss/presentation/library/client/skunkus/id/cc3d06c1cc3834464aef22836c55d13a');

不要忘记在命名空间声明后添加以下行:

use Zend\Feed\Reader\Reader;
use Zend\Http\Client as HttpClient;

它应该可以工作,因为您没有任何网络问题。