doujie1917 2016-07-14 16:48
浏览 927
已采纳

PHP base64_decode返回十六进制或NULL

I have a base64 string:

AgAAOwEwMzEyMTMwMDAwMDAwOTA0QG5haS5lcGMubW5jMTMwLm1jYzMxMi4zZ3BwbmV0d29yay5vcmc=

I can take the raw base64, copy and paste it and run it through bash's base64 -d or php_decode() and it works. But when I grab the variable from a post using file_get_contents('php://input') in PHP it wont work. However, I can log the JSON encoded post using error_log() and copy and paste form there and it works. I can echo the base64 value and everything appears normal. But as soon as I wrap base64_decode around the string, I get errors. If it matters, the first two characters of the decoded string is ";0". I thought possibly that may be causing PHP to assume its hex?

Sorry for the messy debug code, but this should illustrate some examples.

$sub_id = get_field($json[$i], "subscriber-id", "null");
error_log("type: " . gettype($sub_id));
error_log("$i raw:  |$sub_id|");
$decoded = base64_decode($sub_id);
error_log("$i decoded:  $decoded");
error_log("$i raw sub-id: " . $sub_id);
$iccid_a = array();
$raw_sub_id = $sub_id;
$ret = exec("echo \"$raw_sub_id\" | base64 -d ",$iccid_a);
error_log(print_r($iccid_a,1));

I get this output:

type: string

0 raw: |AgAAOwEwMzEyMTMwMDAwMDAwOTA0QG5haS5lcGMubW5jMTMwLm1jYzMxMi4zZ3BwbmV0d29yay5vcmc=|

0 decoded: \x02

0 raw sub-id: AgAAOwEwMzEyMTMwMDAwMDAwOTA0QG5haS5lcGMubW5jMTMwLm1jYzMxMi4zZ3BwbmV0d29yay5vcmc=

Array ( [0] => \x02

  • 写回答

1条回答 默认 最新

  • doushang7209 2016-07-14 18:24
    关注

    Your decoded data begins with some non-printable characters that may not be playing well with whatever you're using as a display.

    $ php -r 'echo base64_decode("AgAAOwEwMzEyMTMwMDAwMDAwOTA0QG5haS5lcGMubW5jMTMwLm1jYzMxMi4zZ3BwbmV0d29yay5vcmc=");' | hexdump -C
    00000000  02 00 00 3b 01 30 33 31  32 31 33 30 30 30 30 30  |...;.03121300000|
    00000010  30 30 39 30 34 40 6e 61  69 2e 65 70 63 2e 6d 6e  |00904@nai.epc.mn|
    00000020  63 31 33 30 2e 6d 63 63  33 31 32 2e 33 67 70 70  |c130.mcc312.3gpp|
    00000030  6e 65 74 77 6f 72 6b 2e  6f 72 67                 |network.org|
    0000003b
    

    I don't know what exactly you're expecting the decoded data to look like, but the problem is not with base64_decode().

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮