MENU

使用PHP在线远程唤醒电脑

• April 1, 2019 • Read: 7696 • 工作阅读设置

最近249买了个蜗牛星际的小主机,J1900 + 4G + 16G + 4盘位。

装了群辉后机器设置好,找了个在线唤醒的PHP代码。

//api.php文件

<?php
header("Content-type:text/html;charset=utf-8");

include("wol.php");

$hostname = $_POST['hostname'];
$mac = $_POST['mac'];
$port = $_POST['port'];
$ip = $_POST['ip'];

/*$json_arr = array("hostname"=>$hostname,"mac"=>$mac,"port"=>$port,"ip"=>$ip);
$json_obj = json_encode($json_arr);
echo $json_obj;
*/

$WOL = new WOL($hostname,$mac,$port,$ip);
$status = $WOL->wake_on_wan();
echo json_encode($status);
?>

wol.php文件

<?php
/**
* 实现网络唤醒功能
*/
class WOL
{
    private $hostname;    // 唤醒设备的url地址
    private $mac;         // 唤醒设备的mac地址
    private $port;        // 唤醒设备的端口
    private $ip;          // 唤醒设备的ip地址(不是必须的,程序会自动根据$hostname来获取对应的ip)
 
    private $msg = array(
        0 => "目标机器可能已经是唤醒的.",
        1 => "socket_create 方法执行失败",
        2 => "socket_set_option 方法执行失败",
        3 => "发送唤醒数据包成功!",
        4 => "唤醒数据包发送失败 \n \n  请检查所填信息!"
    );
     
    function __construct($hostname,$mac,$port,$ip = false)
    {
        $this->hostname = $hostname;
        $this->mac      = $mac;
        $this->port     = $port;
        if (!$ip)
        {
            $this->ip   = $this->get_ip_from_hostname();
        }
    }
 
 
    public function wake_on_wan()
    {
        $addr_byte = explode(':', $this->mac);
        $hw_addr = '';
        for ($a=0; $a<6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));
        $msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
        for ($a=1; $a<=16; $a++) $msg .= $hw_addr;
        // 通过 UDP 发送数据包
        $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
         
        if ($s == false)
        {
            return $this->msg[1]; // socket_create 执行失败
        }

        $set_opt = @socket_set_option($s, 1, 6, TRUE);

        if ($set_opt < 0)
        {
            return $this->msg[2]; // socket_set_option 执行失败
        }

        $sendto = @socket_sendto($s, $msg, strlen($msg), 0, $this->ip, $this->port);
         
        if ($sendto)
        {
            socket_close($s);
            return $this->msg[3]; // magic packet 发送成功!
        }

        return $this->msg[4]; // magic packet 发送失败!
         
        /*}*/
    }
 
     /*通过目标主机的端口情况来判断是否开机,稍有延迟*/
    private function is_awake()
    {
        $awake = @fsockopen($this->ip, 5000, $errno, $errstr, 3);
        
        if ($awake)
        {
            fclose($awake);
        }
         
        return $awake;
    }
 
    private function get_ip_from_hostname()
    {
        return gethostbyname($this->hostname);
    }
 
}
?>

地址:
tool.fyun.org/wol

Last Modified: June 2, 2024
Archives QR Code Tip
QR Code for this page
Tipping QR Code
Leave a Comment

7 Comments
  1. 小易玩NAS 小易玩NAS

    博主你好 这个开机源码可以发一份吗?前后端的 跪谢 34912776@qq.com

  2. 博主你好,tool.fyun.org/wol的源码能否给一份,谢谢

    1. @老李菜刀右键,查看网页源代码即可。

      wol.php文件参考文中代码。

    2. @ZhensJoke问题就是不会代码啊@(汗),可以的话,麻烦有空发一份给www@caidao.net,再次感谢

  3. 35行多了个括号,感谢站长提供的代码。

    1. @罗知水已修改~

  4. Parse error: syntax error, unexpected '}', expecting end of file in /www/zhishui/welk.php on line 35