1、下载驱动 用什么浏览器就下载什么驱动,注意浏览器版本和下载的驱动版本匹配,推荐谷歌浏览器,其他浏览器可能部分功能不能用,下载之后双击运行

https://www.selenium.dev/ecosystem/

2、上代码

<?php

function dd(...$data ){
    foreach ($data as $v) {
        echo '<pre>';
        var_dump($v);
        echo '</pre>';
    }
    die;
}

require_once 'vendor/autoload.php';

set_time_limit(0);

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebElement;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\WebDriverBy;

interface TaskInterface{
    function run(RemoteWebDriver $driver);
    /**
     * 倒计时 毫秒
     *
     * @return int
     */
    function interval(): int ;
}

abstract class Attr{
    protected $config = [];
    public function __construct($config = [])
    {
        $this->config = array_merge($this->config, $config);
    }
}

/**
 * 收到消息
 * Class Message
 * Author: zsw zswemail@qq.com
 */
class Message extends Attr implements TaskInterface{

    protected $config = [
        'messagesClass' => '.webcast-chatroom___items > div:not(.webcast-chatroom___bottom-message) .webcast-chatroom___item',
        'messageClass' => '.webcast-chatroom___item',
        'usernameClass' => '.b5f39b72244cdc51ae219014c5ce1780-scss',
        'msgClass' => '.webcast-chatroom___content-with-emoji-text',
        'giftClass' => '.cc8fb1cd7175d77a863ef597f3b8c11a-scss>img',
        'giftNumClass' => '._8a773759f2bb4fffa84f5bd6264e58ac-scss',
    ];

    public function interval(): int
    {
        return 1000;
    }

    private $lastNode = '';

    public function run(RemoteWebDriver $driver)
    {
        try{
            $class = $this->config['messagesClass'];
            if ($this->lastNode) {
                $class .= '[data-id="'.$this->lastNode.'"]~'.$this->config['messageClass'];
            }
            $messages = $driver->findElements(WebDriverBy::cssSelector($class));
            foreach ($messages as $m) {
                try{
                    $username = $m->findElement(WebDriverBy::cssSelector($this->config['usernameClass']))->getText();
                    $username = trim($username,  ' :');
                    $this->text($m, $username);
                }catch (\Exception $e){}
            }
            if (isset($m)) {
                $this->lastNode = $m->getAttribute('data-id');
            }
        }catch (\Exception $e) {}
    }

    /**
     * 文本消息
     */
    private function text(RemoteWebElement $driver, $username){
        try{
            $msg = $driver->findElement(WebDriverBy::cssSelector($this->config['msgClass']))->getText();
            echo $username . ":" . $msg . PHP_EOL;
        }catch (\Exception $e) {
            $this->gift($driver, $username);
        }
    }

    /**
     * 打赏
     */
    private function gift(RemoteWebElement $driver, $username){
        try{
            $number = $driver->findElement(WebDriverBy::cssSelector($this->config['giftNumClass']))->getText();
            $number = (int)trim($number, '× ');
            $url = $driver->findElement(WebDriverBy::cssSelector($this->config['giftClass']))->getAttribute('src');
            echo $username . " 送出礼物 ". $number . PHP_EOL;
        }catch (\Exception $e) {}
    }
}

/**
 * 进入直播间
 * Class Incoming
 * Author: zsw zswemail@qq.com
 */
class Incoming extends Attr implements TaskInterface{

    protected $config = [
        'messageClass' => '.webcast-chatroom___bottom-message > .webcast-chatroom___item',
        'usernameClass' => '.b5f39b72244cdc51ae219014c5ce1780-scss',
    ];

    private $users = [];

    public function interval(): int
    {
        return 500;
    }

    public function run(RemoteWebDriver $driver)
    {
        try{
            $messages = $driver->findElements(WebDriverBy::cssSelector($this->config['messageClass']));
            foreach ($messages as $m) {
                try{
                    $username = $m->findElement(WebDriverBy::cssSelector($this->config['usernameClass']))->getText();
                    $username = trim($username);
                    if (false === array_search($username, $this->users)) {
                        $this->users[] = $username;
                        if (count($this->users) > 10) {
                            array_shift($this->users);
                        }

                        echo "欢迎 {$username} 进入直播间" . PHP_EOL;
                    }
                }catch (\Exception $e){}
            }

        }catch (Exception $e) {}
    }
}

class WebDriver{

    private $host = '';

    /* @var $driver RemoteWebDriver*/
    private $driver;

    private $desiredCapabilities;

    public function __construct($host='http://localhost:9515')
    {
        $this->host = $host;
    }

    public function chrome(){
        $this->desiredCapabilities = DesiredCapabilities::chrome();
        $this->desiredCapabilities->setCapability(
            'moz:firefoxOptions',
            ['args' => ['-headless']]
        );

        return $this;
    }

    public function start(){
        $this->driver = RemoteWebDriver::create($this->host, $this->desiredCapabilities);
        return $this->driver;
    }
}

class dy
{

    private $interval = 100; // 0.1 秒钟循环一次

    private $task = [];

    /* @var RemoteWebDriver*/
    private $driver;

    public function __construct($url)
    {
        $this->driver = (new WebDriver())->chrome()->start()->get($url);
    }

    public function addTask(TaskInterface $task)
    {
        $this->task[] = [
            'class' => $task,
            'time'  => 0
        ];
        return $this;
    }

    public function run()
    {
        while (1) {
            foreach ($this->task as $k => $taskData) {
                /* @var $task TaskInterface */
                $task = $taskData['class'];
                $this->task[$k]['time'] -= $this->interval;
                if ($taskData['time'] > 0) {continue;}
                $this->task[$k]['time'] = $task->interval();
                $task->run($this->driver);
            }
            usleep($this->interval * 1000);
        }
    }

    public function __destruct()
    {
        $this->driver->quit();
    }
}

// 修改这儿直播间地址
// 分享直播间地址用浏览器打开复制地址到下面 测试带货直播间不能在网页版播放
$dy = new dy('https://live.douyin.com/56155910213');

$dy->addTask(new Message());
$dy->addTask(new Incoming());

$dy->run();

composer引入包

composer require php-webdriver/webdriver

3、直接执行PHP文件 查看效果

微信图片_20210920115048.png