前提:已經安裝好寶塔面板、php-webdriver
如果沒有安裝,請在本站搜索webdriver,裡面有詳細教程
1.下載火狐瀏覽器
直接下載最新版的火狐瀏覽器進行安裝
2.下載Geckodriver
進網址https://github.com/mozilla/geckodriver/releases下載最新版的驅動並打開
3.測試
根目錄新建一個文件夾,在裡面再新建一個php文件,將下面的內容複製到文件裡面進行測試
<?php
// An example of using php-webdriver.
// Do not forget to run composer install before. You must also have Selenium server started and listening on port 4444.
namespace Facebook\WebDriver;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Firefox\FirefoxOptions;
use Facebook\WebDriver\Remote\DesiredCapabilities;
require_once('../vendor/autoload.php'); //注意這個位置,我是在網站根目錄額外創建了一個文件夾,所以有../,請根據實際位置修改
$serverUrl = 'http://localhost:4444';
$desiredCapabilities = DesiredCapabilities::firefox();
// Disable accepting SSL certificates
$desiredCapabilities->setCapability('acceptSslCerts', false);
// Add arguments via FirefoxOptions to start headless firefox
$firefoxOptions = new FirefoxOptions();
$firefoxOptions->addArguments(
[
//'--headless', // 瀏覽器不提供可視化頁面
//'--incognito', //無痕模式啟動
]
);
$desiredCapabilities->setCapability(FirefoxOptions::CAPABILITY, $firefoxOptions);
$driver = RemoteWebDriver::create($serverUrl, $desiredCapabilities);
$url = $_GET['url'];
// navigate to Selenium page on Wikipedia
$driver->get($url);
$content = $driver->getPageSource();
echo $content;
$driver->quit();
?>
上述代碼經過測試,是可以正常訪問的
到此為止。
發布者:彬彬筆記,轉載請註明出處:https://www.binbinbiji.com/zh-hant/php/2856.html