stream_get_contents()
真的吓坏了我。它是来自互联网的二十四小时不间断调试和测试解决方案,以使其发挥作用。
它基本上只是停止随机阅读。
我正在做的是通过proc_open()
加载带有PhantomJs的网页,然后加载读取进程给出的管道的结果。问题是每当我尝试读取管道,stdout和stderr时,stream_get_contents()
块:
我知道它会随机阻塞,因为我试图用这段代码调试它:
while(! feof($pipes[2])) {
echo stream_get_contents($pipes[2], 50);
}
echo "Finished!";
页面永远无法回复"已完成!" 。
我使用的库是php-phantomjs。
我完全相信它不是我身边的问题,因为如果我尝试使用从库生成的临时文件自己执行phantomjs,phantomjs会正确执行并且它会给我正确的结果。
我已经在Windows和Linux OS(使用nginx)上测试了代码。
图书馆给我带来麻烦的地方是here。
然而,这是我用于网页抓取页面的PHP代码(简化):
$client = Client::getInstance();
$client->setBinDir(Config::get('phantomjs.bin'));
$client->setPhantomJs(Config::get('phantomjs.path'));
$client->setPhantomLoader(Config::get('phantomjs.loader'));
$client->debug(true);
$request = $client->getMessageFactory()->createRequest();
$request->setMethod('GET');
$request->setUrl('http://google.com');
$request->setTimeout("120000");
$response = $client->getMessageFactory()->createResponse();
$client->send($request, $response);
stream_get_contents()
在阅读时 或 有趣的事实
我为网站建立的图书馆抓一个网站并返回结果 使用一个月前工作。相同的代码现在不再起作用而不改变任何东西。
答案 0 :(得分:0)
问题已通过在proc_open()
中使用临时文件而非流管道来解决。
此外,PhantomJs 1.9.8与 php-phantomjs 不应该安装,因为它会解决从文件中解析数据的问题。 It's a known issue.
I have released a repository that fixes the problem to whoever wishes to use it.
在您的应用程序composer.json中添加以下代码行。
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/GiampaoloFalqui/php-phantomjs"
}
],
"require": {
"jonnyw/php-phantomjs": "3.*"
}
},