收听端口并将数据写入文件

I have a GPS tracker which sends data via GPRS to a specific IP, and specific port

I need a script in php which receives the data and write it to a txt file.

Perhaps the following code will help. You can place it in a loop it keeps listening for the next message.

<?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; 
    } 
} 
?>