i am having some problems using pack() in php
$currencypair = "EUR/USD";
$buy_sell = "buy";
$alert_device_token =array("a","a","b");
$message = "Your " . $currencypair . " " . $buy_sell . " alert price has been reached!";
$payload['aps'] = array (
'alert' => $message,
'badge' => 1,
'sound' => 'default'
);
$payload = json_encode($payload);
foreach ($alert_device_token as $alert_device)
{
$apnsMessage = chr(0) . chr(0) . chr(32) .
pack('H*', str_replace(' ', '', $alert_device)) .
chr(0) . chr(strlen($payload)) . $payload;
echo $apnsMessage;
}
Now sometimes i get following warnings running the same code -
Warning: pack() [function.pack]: Type H: illegal hex digit g in /code/FR2BVl
the illegal hex digit keeps varying though. Any ideas about this warning and ways to remove it.
check it live here
Try to save your file in utf-8 encoding.
pack
converts hexadecimal number to binary, e.g.:produces
!3
, since!
has code 0x21 and3
has code 0x33. Sinceg
is not hex digit, warning is given. To be useful for pack'sH
format, the argument must be hex number. If$alert_device
isn't - you should use something else, depending on what it is and what you expect as the result.One of the reason for the error is related to the checksums,
To fix the error this might be sufficient,
In this case,
Ref: https://github.com/bearsunday/BEAR.Package/issues/136
I was having the same issue when developing a hybrid app using Ionic/Cordova/PhoneGap. As the same code is run in Android and iOS devices, I had made a mistake of storing Google FCM token as APNS token. The APNS token is purely hexadecimal but Google FCM token can have non-hexadecimal characters. So, packing a Google FCM token using PHP's
pack()
function will result in theillegal hex digit
error.Use
strtr(rtrim(base64_encode(pack('H*', sprintf('%u', $algo($data)))), '='), '+/', '-_')
insted of usingpack('H*', $value)
.In this case,
$alert_device
is an array.For packing it needs a value.
Use
pack('H*', str_replace(' ', '', $alert_device[0]))
instead.