cURL POST字符串的格式

I have seen post data passed to a cURL option in several ways such as an array with key value pairs, as a string like so: "username=user&password=pass" but I came accross a different type of format which appeared odd to me and maybe some of you can clarify this. It looks as follows:

$sPost = "session[username_or_email]=$username&session[password]=$password&return_to_ssl=true&scribe_log=&redirect_after_login=%2F&authenticity_token=$authenticity_token";

curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost);

Why exactly does it look that way "session[username_or_email]"?

Some form processors will treat a POST variable name with square brackets like an associative array. Imagine your post was coming from a form:

<form>
  <input type="hidden" name="session[username_or_email]">
  <input type="hidden" name="session[password]">
</form>

In PHP, for instance, this will be processed as:

$_POST=array(
   'session' => array(
      'username_or_email' => whatever,
      'password' => whatever
   )
)

This is just by convention. As far as the HTTP POST itself, just think of these as two different variable names that happen to contain square brackets.

That is because when you parse it, it will be stored as an array. So $_POST['session'] will be an array of keys and values

That's the notation used to indicate an associative array in your query string. When the POST string is received by PHP, it'll take values like that and turn them into an array called session that has the key listed within the square brackets.

For example, if you were building the array manually, the declaration of the array would look something like this:

$_POST = array(
    'session' => array(
        'username_or_email' => 'username',
        'password' => 'password'
    ),
    'return_to_ssl' => true // etc.
);