简体   繁体   English

base64_decode返回null

[英]base64_decode returns null

Alright, so I'm implementing some push notifications in my app. 好了,所以我正在我的应用程序中实现一些推送通知。 I've written some PHP which should interact with a MySQL DB. 我已经编写了一些PHP,该PHP应该与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). 我想将设备令牌以char(64)的形式存储在SQL中,因为我已经相信这很好(我的意思是,无论如何以后都应该以二进制形式发送)。

On the iOS side I do this: 在iOS方面,我这样做:

- (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. base64内容来自Matt Mattaglagher的NSData + Base64。 Now, on the PHP side, this happens: 现在,在PHP方面,这发生了:

// $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: 从php.net/manual关于base64_decode的返回值:

Returns the original data or FALSE on failure. 返回原始数据,如果失败则返回FALSE。 The returned data may be binary. 返回的数据可以是二进制的。

Now, I don't know more PHP then I've taught myself the last two days; 现在,我对PHP的了解不多了,所以我过去两天自学了。 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!). 但是如果文档说它应该返回FALSE或原始数据,我会感到非常困惑,因为返回的值是一个空字符串(因为我非常确定原始数据不是null!)。

As the base64_decoded string becomes null, so does apparently my whole query string when I append it. 当base64_decoded字符串变为null时,附加查询后,整个查询字符串也会明显变为null。 That does not make for a very convincing query. 但这并不能令人信服。

So my questions are, I guess: why does base64_decode return things it shouldn't? 所以我的问题是,我猜:为什么base64_decode返回不应该返回的内容? 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?)? 一旦我实际上从base64_decode中获取了数据,我应该如何将其包括在查询中,以便将其存储在数据库中(只需追加?)?

Additional info: I'm using the Slim Framework. 附加信息:我正在使用Slim框架。 I'm doing all this in a route. 我正在做这一切。 $jsonData is taken from the body like such: $ jsonData像这样从正文中获取:

$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: 对应用程序进行了一些额外的测试,只是为了确保我的应用程序对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. 请将您的查询字符串写入php中的文件中,这是最好的方法,目的是测试您编码的令牌值是否正确。 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. 这会将整个查询字符串写入“ query_string.txt”文件。 then open the file. 然后打开文件。 and check the value of your base64 encoded value. 并检查您的base64编码值的值。 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 就我而言,如果我解码的字符串格式不正确,它将返回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 但在某些情况下:某人在想对字符串进行编码但使用base64_decode时缺少了base64_decode和base64_encode,因此显示为null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM