In Internet of Things (IoT) projects, the server usually needs to receive data uploaded by the device in real time. These devices may use a TCP connection to send sensor data, status information, etc. to the server. Although PHP is not the preferred language for network programming in the traditional sense, it can also be competent for this task through its built-in socket extension. This article will introduce how to use the socket_accept() function to receive data from IoT devices and perform basic processing.
First, make sure your PHP environment has sockets extension enabled. You can confirm whether it is enabled by running phpinfo() ;.
Here are the steps to create a basic TCP socket server:
<?php
// Setting up server address and port
$host = '0.0.0.0';
$port = 8888;
// create socket
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (!$socket) {
die("无法create socket:" . socket_strerror(socket_last_error()));
}
// Bind socket arrive IP and ports
if (!socket_bind($socket, $host, $port)) {
die("Bindfail:" . socket_strerror(socket_last_error($socket)));
}
// Start listening to the connection
if (!socket_listen($socket)) {
die("Listening failed:" . socket_strerror(socket_last_error($socket)));
}
echo "The server has started,Wait for the device to connect...\n";
Use socket_accept() to receive the connection. This function blocks until there is a device connection:
while (true) {
$clientSocket = socket_accept($socket);
if ($clientSocket === false) {
echo "socket_accept fail:" . socket_strerror(socket_last_error($socket)) . "\n";
continue;
}
echo "The device is connected。\n";
// Read data sent by the device
$input = socket_read($clientSocket, 1024);
if ($input === false) {
echo "读取fail:" . socket_strerror(socket_last_error($clientSocket)) . "\n";
} else {
echo "接收arrive数据:$input\n";
// Assume the data is JSON Format,Perform decoding
$data = json_decode(trim($input), true);
if (json_last_error() === JSON_ERROR_NONE) {
// Example:Processing temperature sensor data
if (isset($data['device_id'], $data['temperature'])) {
$deviceId = $data['device_id'];
$temperature = $data['temperature'];
// 假设保存arrive数据库或执行其他处理逻辑
echo "equipment $deviceId Upload temperature:$temperature °C\n";
// Return the processing result
$response = json_encode(['status' => 'ok']);
socket_write($clientSocket, $response);
} else {
echo "收arrive非法数据。\n";
}
} else {
echo "JSON 解码fail。\n";
}
}
// Close the current connection
socket_close($clientSocket);
}
socket_close($socket);
The IoT device only needs to establish a TCP connection and upload data in JSON format. For example:
{
"device_id": "sensor_001",
"temperature": 25.6
}
Data can be sent to the m66.net:8888 address of the server through TCP clients (such as the Arduino program of ESP8266, Python socket, or even curl).
Security : It is recommended to use encryption (such as TLS) to transmit data in production environments.
Concurrency processing : Consider using pcntl_fork() to create a child process to handle multiple connections.
Data persistence : Device data can be written to MySQL, Redis, or log files.
Although PHP is not designed for socket programming, developers can quickly build a simple and practical IoT data receiving server with built-in functions such as socket_accept() . In small-scale device communication and testing environments, using PHP is completely feasible. Hopefully this article helps you better understand and apply socket_accept() to process IoT data.