2013-11-25 56 views
18

我想從本地主機上的PHP執行PhantomJS。如何從PHP執行PhantomJS

任何機構都可以解釋如何從PHP執行PhantomJS以及我應該從phantomjs.org下載哪些軟件包?

+1

https://packagist.org/packages/jonnyw/php-phantomjs – jszobody

+0

http://stackoverflow.com/questions/10651320/:

下載和安裝您只需使用下面的代碼後開始 - phantomjs-server-from-php-and-waiting-for-its-response(google爲「phantomjs php」提供了很多很好的點擊,有些甚至在stackoverflow) –

+0

我無法在我的本地運行php phantomjs腳本,一些錯誤,我不知道他們發生了什麼...我測試一些這個腳本git – MOB

回答

24
  • 下載PhantomJS binary,上傳它的地方,並使其可執行(chmod +x
  • 如果你要作的截圖,設置fontconfig(這是非常具體的,以我的配置,但我們的目標是確保有您的系統上至少某些字體)
  • 運行PHP如下:
    $response = exec('/path/to/phantomjs myscript.js');
+1

thanx很多的答案。 即時通訊嘗試在Windows 7和xampp服務器的本地主機上使用它。 我如何讓它可執行?以及我應該下載什麼軟件包? Windows或Linux版本? – MOB

+0

它的工作。 thnx很多 – MOB

+0

對於任何有此解決方案問題的人,通過關注[本答案](https://groups.google.com/d/msg/phantomjs/s8RS-Glf7fg/z82YTZpWaycJ),我設法成功地調用了我的Phantom腳本PHP。 – Fisu

7

有實際上是一個名爲庫,旨在讓您更輕鬆!

PHP PhantomJS是一個靈活的PHP庫加載通過 PhantomJS無頭的瀏覽器頁面,並返回頁面響應。測試需要javascript支持的網站並支持 屏幕截圖是便利的 。

Full documentation

功能列表:

  • 通過PhantomJS加載網頁模擬瀏覽器
  • 查看詳細的響應數據包括頁面內容,標題,狀態代碼等
  • 手柄重定向
  • 查看javas CRIPT控制檯錯誤
  • 查看詳細PhantomJS調試信息
  • 保存屏幕捕獲到本地磁盤
  • 設置視口大小
  • 定義屏幕捕捉X,Y,寬度和高度參數
  • 延遲頁面呈現在指定的時間
  • 通過命令行選項執行PhantomJS
  • 輕鬆構建和運行自定義腳本PhantomJS

確保,雖然,你的PhantomJS版本與您PHP PhantomJS的版本不兼容:

請注意:這個庫的 4.0版目前正在等待一個 PhantomJS 2.0尚未解決的問題。

+0

什麼是未解決的問題,是它打開後頁面1.9後的網頁速度太慢了嗎? –

2

我最近發佈了一個讓PHP訪問瀏覽器的項目。在這裏獲取:https://github.com/merlinthemagic/MTS。在引擎蓋下它依賴於PhantomJS。

$myUrl   = "http://www.example.com"; 
$windowObj  = \MTS\Factories::getDevices()->getLocalHost()->getBrowser('phantomjs')->getNewWindow($myUrl); 

//now you can either retrive the DOM and parse it, like this: 
$domData = $windowObj->getDom(); 

//or take screen shots 
$imageData = $windowObj->screenshot(); 

//or use the mouse to click buttons: 
$windowObj->mouseEventOnElement("[id=searchInput]", 'leftclick'); 

//or type with the keyboard : 
$windowObj->sendKeyPresses("my search words"); 

//or load and execute custom javascript, fill forms etc, etc.