phantomphp / phantomphp
从 PHP 中无缝管理和与 phantomjs 进程进行通信
dev-master
2016-10-17 11:41 UTC
Requires
- php: >=5.5
Requires (Dev)
- jakoch/phantomjs-installer: ~2.1.1
- phpunit/phpunit: ~4.1
- squizlabs/php_codesniffer: ~2.5
This package is auto-updated.
Last update: 2024-09-06 19:08:02 UTC
README
进行中 - 该项目正在开发中,您现在可以使用它,但请注意,任何内容都可能随时更改。
项目
启动一个 phantomjs 进程并从 php 中控制它。最突出的功能是能够从多个 php 脚本与单个 phantomjs 脚本进行通信。
注意:该项目在 windows 上未进行测试,因此它可能只能在 Unix 系统上运行
概述
use PhantomPhp\PhantomClient; use PhantomPhp\PageManager; // Create a phantom client that is responsible for managing phantomjs process $client = new PhantomClient(); // Start the phantomjs js process $client->start(); // Page manager helps to generate page (like a browser with pages/tabs) $pageManager = new PageManager($client); // Create a new page $page = $pageManager->createPage(); // Open an url on this page $page->navigate('http://example.com'); // Get the dom $domString = $page->getDomContent(); // Run custom javascript on the page $divWidth = $page->runScript('return document.getElementsByTagName("div")[0].offsetWidth;'); // Stop the phantomjs process $client->stop();
页面 API
TODO: 页面 API 文档
多进程通信
通信通道
PhnatomPhp 支持不同的通信通道。通信通道代表与底层 phantomjs 进程通信的方式。默认情况下,php 和 phantomjs 通过管道通信,但您可以将 phantomjs 转换为真实应用程序,等待任何脚本与之交互。
为此,只需启动一个带有指定端口的 HttpClient 即可。
use PhantomPhp\HttpClient; use PhantomPhp\PageManager; $client = new HttpClient(8080); $client->start(); // Now phantomjs is listening on port 8080 // As with the stream client you can start a pageManager based uppon this http client $pageManager = new PageManager($client); // Create a page named foo $page = $pageManager->createPage('foo'); $page->navigate('http://example.com'); // ....
如果您留下之前的过程打开并打开另一个过程,另一个过程能够调用 phantomjs 而不必启动新的客户端。为此,您只需为相同的端口定义一个 http 通道即可。
use PhantomPhp\Communication\HttpRequest; use PhantomPhp\PageManager; $channel = new HttpRequest(8080); // and pass this channel to a new page manager $pageManager = new PageManager($client); // Get the page named foo created on the other process $page = $pageManager->getPage('foo'); // warning: getPage is not implemented yet $dom = $page->getDomContent(); // ....
路线图
- 更多页面 API 端点
- 按需使页面高级同步化(也在
$page->navigate
中降低请求数量) - 支持 PSR-7 请求
- 支持页面 Cookie、代理、视口、UA 等...
- 支持默认代理、默认 UA、默认视口等...
- 测试错误
- 文件日志