tags:

views:

580

answers:

2

Hi

I've successfully managed to connect to Apple's feedback APNS server but I'm not sure how to unpack the binary data you get from fread(). Does anyone know how to do this? The documentation says the first 4 bytes are the timestamp, the next 2 are the token length and the rest are the device token.

How does this info get unpacked into readable characters after the call to fread?

+2  A: 

Once you have your binary stream, you can process it like this:

while ($data = fread($stream, 38)) {
  $feedback = unpack("N1timestamp/n1length/H*devtoken", $data);
  // Do something
}

$feedback will be an associative array containing elements "timestamp", "length" and "devtoken".

Chris Newman
+1  A: 

Actually figured it out, this seems to be more reliable:

$arr = unpack("H*", $devconts); 
$rawhex = trim(implode("", $arr));

$feedbackTime = hexdec(substr($rawhex, 0, 8)); 
$feedbackDate = date('Y-m-d H:i', $feedbackTime); 
$feedbackLen = hexdec(substr($rawhex, 8, 4)); 
$feedbackDeviceToken = substr($rawhex, 12, 64);