Search code examples
phparrayspostgziphttpservice

How do I decompress a $_POST array?


I have a $_POST that is in array format, and when the $_POST is over 256B, because what sends it has limitations, it's automatically compressed using gzip. I can't prevent that, but what I can do is decompress it (using PHP) and still have it as an array. I think. gzuncompress is only for strings. I can use this: (to turn it into a string that's decompresssed)

$post_body = file_get_contents('php://input');
$post_body = (ord(substr($post_body,0,1)) == 31 ? gzinflate(substr($post_body,10,-8)) : $post_body);

But that is a string, and I can't write PHP for the life of me, I had help writing what I currently got but the helper is stumped here.

Assuming the $post_body is:

userid=&name=&level=&exp=&key1=&key2=&key3=&key4=&key5=&key6=&key7=&key8=&key9=&key10=&key11=&key12=

How would I turn that back into an array? Like, how I could previously do:

$userid = $_POST["userid"];
$name = $_POST["name"];

and so on. I sort of don't know how to see the $_POST as an array though. The method automatically converts it from the string to the array. But then I need to handle it in PHP as an array. I don't see the array though. I can echo it, but it shows up as

Array

Thats all.

Help is very much appreciated, I'm storing data with this, and I had to take down a 'game' of mine to deal with this problem. Up until now $_POST was under 256B, so I didn't have the problem. :/

Edit: thanks @Marcin Orlowski for telling me b = bit and B = bytes


Solution

  • You need parse_str() like this:

    $post_body = file_get_contents('php://input');
    $post_body = (ord(substr($post_body,0,1)) == 31 ? gzinflate(substr($post_body,10,-8)) : $post_body);
    
    parse_str($post_body);
    
    // Now just call $userid or $name, etc...