title: Send HTTP Post with Array - PHP
body: <p>I'm trying to use this nice function:</p>
  
  <pre><code>function do_post_request($url, $data, $optional_headers = null) {
      $params = array('http' =&gt; array(
          'method' =&gt; 'POST',
          'content' =&gt; $data
      ));
  
      if ($optional_headers !== null) {
          $params['http']['header'] = $optional_headers;
      }
      $ctx = stream_context_create($params);
      $fp = @fopen($url, 'rb', false, $ctx);
      if (!$fp) {
          throw new Exception("Problem with $url, $php_errormsg");
      }
      $response = @stream_get_contents($fp);
      if ($response === false) {
          throw new Exception("Problem reading data from $url, $php_errormsg");
      }
  
      return $response;
  }
  </code></pre>
  
  <p>to send a POST command to a specific url, my problem is i'm trying to send the post paramters in a form of an  array, something like this</p>
  
  <pre><code>login[username]: myusername
  login[password]: mypassword,
  </code></pre>
  
  <p>however i'm not able to do that, calling the function with :</p>
  
  <pre><code>            $login_post = array('login[username]' =&gt; $email,
                              'login[password]' =&gt; '');
          do_post_request(getHost($website['code']), $login_post);
  </code></pre>
  
  <p>always send the data to the post in the form:</p>
  
  <pre><code>username: myusername
  password: mypassword
  </code></pre>
  
  <p>How can I avoid this (without using curl)? Thanks a lot.</p>
  
  <p>Thanks</p>
  
  <p>Yehia</p>