Alright, so I'm implementing some push notifications in my app. I've written some PHP which should interact with a MySQL DB.
I want to store the device tokens in SQL as char(64) because I've come to believe this is good (I mean, it's supposed to be sent as binary later anyways).
On the iOS side I do this:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
NSString *tokenString = [deviceToken base64EncodedString];
NSDictionary *jsonDic = [NSDictionary dictionaryWithObjectsAndKeys:tokenString, @"Token", [NSNumber numberWithBool:YES], @"IsiDevice", [NSNumber numberWithBool:YES], @"Active", nil];
//... http request stuff
// what might be important is that the POST body is encoded as UTF8 (content-type set appropriately)
}
The base64 stuff comes from NSData+Base64 by Matt Gallagher. Now, on the PHP side, this happens:
// $tokenEncoded is exactly the same string as I sent from the app (type is string)
$tokenEncoded = $jsonData['Token'];
// $token becomes null! (type is string)
$token = base64_decode($tokenEncoded);
From php.net/manual on base64_decode's return values:
Returns the original data or FALSE on failure. The returned data may be binary.
Now, I don't know more PHP then I've taught myself the last two days; but if the documentation says it should return FALSE or the original data, I find it very confusing that the returned value is a null-string (because I'm very sure the original data wasn't null!).
As the base64_decoded string becomes null, so does apparently my whole query string when I append it. That does not make for a very convincing query.
So my questions are, I guess: why does base64_decode return things it shouldn't? Once I actually get the data out of base64_decode, how should I include it in the query so that it is stored as I want it to in the DB (just append?)?
Additional info: I'm using the Slim Framework. I'm doing all this in a route. $jsonData is taken from the body like such:
$jsonBody = Slim::getInstance()->request()->getBody();
$jsonData = json_decode($jsonBody, true);
Did some additional tests from the app, just to make sure nothing gets fudged up by my app's implementation of base 64:
DLOG(@"Device token as NSData: %@", deviceToken);
NSString *base64token = [deviceToken base64EncodedString];
DLOG(@"Device token as base64: %@", base64token);
DLOG(@"Device token back from base64: %@", [NSData dataFromBase64String:base64token]);
Output:
Device token as NSData: <65625f1c c33b9480 5a46f346 8b2877d0 95e92823 33e88e91 fd3a2abf febad972>
Device token as base64: ZWJfHMM7lIBaRvNGiyh30JXpKCMz6I6R/Toqv/662XI=
Device token back from base64: <65625f1c c33b9480 5a46f346 8b2877d0 95e92823 33e88e91 fd3a2abf febad972>
please write your query string to a file in php this is best idea is to test your encoded token value is correct or not. eg
<?php
$querystring=$_REQUEST['YOUR_QUERY_STRING'];
file_put_contents("query_string.txt",$querystring);
?>
This will write the whole query string to "query_string.txt" file. then open the file. and check the value of your base64 encoded value. Then copy the value and check with a simple program like.
<?php
echo base64_decode("YOUR_COPIED_VALUE");
// if this works fine there is no problem. you have to go through your php code.
?>
In my case if I decode a string is not right format it will return null
echo base64_decode("i'm not encode, or wrong format");//null
echo base64_decode('bWVvdyBtZW93');//meow meow
So that check your string format again
But in some case: someone missing base64_decode and base64_encode, when they want encode a string but they using base64_decode so it's show null