1

我有一个 GPS 跟踪器,它通过 GPRS 将数据发送到特定 IP 和特定端口

我需要一个 php 中的脚本来接收数据并将其写入 txt 文件。

4

1 回答 1

4

也许下面的代码会有所帮助。你可以把它放在一个循环中,它会一直监听下一条消息。

<?php
// Server IP address
$address = "xx.xxx.xxx.xxx";
// Port to listen
$port = 80;

$mysock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);

socket_bind($mysock,$address, $port) or die('Could not bind to address'); 
socket_listen($mysock, 5);
$client = socket_accept($mysock);

// read 1024 bytes from client
$input = socket_read($client, 1024);

// write received gprs data to the file
writeToFile('gprs.log', $input);

socket_close($client);
socket_close($mysock);
?> 

<?php
function writeToFile($strFilename, $strText) { 
    if($fp = @fopen($strFilename,"w"))  { 
          $contents = fwrite($fp, $strText); 
          fclose($fp); 
          return true; 
    } else { 
      return false; 
    } 
} 
?> 
于 2013-10-08T10:47:33.647 回答