PHP & Curl : post variable in square brackets
<p>I have to post a request with variable names that contains brackets to a distant webpage through Curl.</p>

<p>The distant form contains variables in brackets, for instance :</p>

<pre><code>&lt;input name="test[firstname]"&gt;
&lt;input name="test[lastname]"&gt;
</code></pre>

<p>So, I build my Curl request like this :</p>

<pre><code>function httpPost($url,$params)
{
  $postData = '';
   foreach($params as $k =&gt; $v) 
   { 
      $postData .= $k . '='.$v.'&amp;'; 
   }
   $postData = rtrim($postData, '&amp;');

    $ch = curl_init();  

    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    curl_setopt($ch,CURLOPT_HEADER,true); 
    curl_setopt($ch,CURLOPT_POST, count($postData));
    curl_setopt($ch,CURLOPT_POSTFIELDS, $postData); 

    $output = curl_exec($ch);

    if($output === false)
    {
        return 'Curl error: ' . curl_error($ch);
    }
    else
    {
        return $output;
    }

    curl_close($ch);
}

$params = array(
  "test[firstname]" =&gt; "Frank",
  "test[lastname]" =&gt; "Tomaso"
);

var_dump($params);
// array(2) { ["test[firstname]"]=&gt; string(5) "Frank" ["test[lastname]"]=&gt; string(6) "Tomaso" }

echo httpPost("http://www.mywebsite.tld/search/",$params);
</code></pre>

<p>The script doesn't return any error, but nothing appears on screen ($output). If I remove the $params content, it outputs the page but without any results. So I think there is a problem with the variable names in brackets. What do you think ?</p>