Current Location: Home> Latest Articles> How to Use PHP’s pack() Function to Encode GPS Coordinates and Timestamps for IoT Device Transmission

How to Use PHP’s pack() Function to Encode GPS Coordinates and Timestamps for IoT Device Transmission

M66 2025-06-26

1. Why use binary packing?

GPS coordinates are usually represented as floating-point numbers, for example:

  • Latitude: 31.2304

  • Longitude: 121.4737

  • Timestamp: for instance, a Unix timestamp 1716720000

If sent directly as a string format, such as JSON, the size is relatively large and not suitable for bandwidth-constrained IoT devices. Using the pack() function, we can encode these values into a fixed-length binary data packet, saving bandwidth and ensuring accurate reconstruction on the receiving end.


2. Introduction to PHP pack() function

pack() is a built-in PHP function used to convert data into a binary string, commonly used in network communication, file storage, and more. Its basic syntax is as follows:

string pack(string $format, mixed ...$values)

The $format parameter is a format control string that specifies the types and order of the following values, such as:

  • f: 32-bit floating point (little-endian)

  • d: 64-bit floating point

  • N: 32-bit unsigned integer (big-endian)

  • V: 32-bit unsigned integer (little-endian)


3. Packing GPS coordinates and timestamp

For compatibility and data compactness, assume we use:

  • Latitude: float (4 bytes, little-endian)

  • Longitude: float (4 bytes, little-endian)

  • Timestamp: uint32 (4 bytes, little-endian)

We can pack these data using the following code:

<?php
// GPS data
$latitude = 31.2304;
$longitude = 121.4737;
$timestamp = time(); // current timestamp
<p>// Pack using little-endian format (latitude: float, longitude: float, timestamp: uint32)<br>
$binaryData = pack('ffV', $latitude, $longitude, $timestamp);</p>
<p>// Optional: convert packed binary data to hex for easier viewing<br>
$hexData = bin2hex($binaryData);</p>
<p>echo "Packed binary data: $hexData\n";<br>
?><br>

This code outputs something like:

Packed binary data: 9ad4fc3f7089514270128c66

Each segment corresponds to the packed latitude, longitude, and timestamp. You can use unpack() on the server side to parse it back:

<?php
$receivedData = $binaryData; // assuming data received from the device
$unpacked = unpack('flatitude/flongitude/Vtimestamp', $receivedData);
<p>print_r($unpacked);<br>
?><br>


4. Device transmission recommendations

The device can use the same binary structure to pack data and upload it to the server via MQTT, HTTP POST, or UDP. For example:

$url = "https://m66.net/api/receive.php";
$data = $binaryData;
<p>$options = [<br>
'http' => [<br>
'method' => "POST",<br>
'header' => "Content-Type: application/octet-stream\r\n",<br>
'content' => $data<br>
]<br>
];<br>
$context = stream_context_create($options);<br>
$response = file_get_contents($url, false, $context);</p>
<p>echo "Server response: $response\n";<br>

The server reads the raw binary data through php://input and parses it accordingly.


5. Notes

  • The floating-point format used in pack() may slightly differ across platforms, so it's important to consistently use either little-endian or big-endian and document the format.

  • Ensure the device and server use the same byte order.

  • If the transmitted data structure is complex, consider adding a header with identifiers such as type or version number.