douguo7431 2012-08-27 08:12
浏览 325

base64_decode返回null

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>
  • 写回答

2条回答 默认 最新

  • dongmei2956 2012-08-27 09:18
    关注

    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.
    ?> 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 fesafe材料库问题
  • ¥35 beats蓝牙耳机怎么查看日志
  • ¥15 Fluent齿轮搅油
  • ¥15 八爪鱼爬数据为什么自己停了
  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统