title: PHP - Fopen to cURL
body: <p>I'm adapting the code of a web based system I use, given that on my development ambient I have the fopen() function activated, but on the production ambient, I haven't this function active, for security reasons.
  On the code, I have this function:
  
  
  <pre><code>function PostRequest($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)
      {
          die("Problem reading data from " . $url . "");
      }
  
      $response = @stream_get_contents($fp);
  
      // var_dump($response);
  
      if ($response == false)
      {
          die("Problem reading data from " . $url . "");
      }
  
      return $response;
  }
  </code></pre>
  
  <p>I changed its to:</p>
  
  <pre><code>function PostRequest($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;
      }
  
      // Customizations for fopen() or curl()
  
      if (ini_get('allow_url_fopen') == true)
      {
          $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)
          {
              die("Problem reading data from " . $url . "");
          }
  
          $response = @stream_get_contents($fp);
  
          // var_dump($response);
  
          if ($response == false)
          {
              die("Problem reading data from " . $url . "");
          }
  
          return $response;
      }
      else
      if (function_exists('curl_init'))
      {
          $curl = curl_init($url);
          curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($curl, CURLOPT_HTTPHEADER, $params);
          $response = curl_exec($curl);
          curl_close($curl);
          return $response;
      }
      else
      {
          die("Problem reading data from " . $url . "");
      }
  }
  </code></pre>
  
  <p>The problem in here, is this error:</p>
  
  <blockquote>
    <p>Notice: Array to string conversion in
    /path/classes/xmwsclient.class.php on line 133 API Error: No 'request'
    method was recieved</p>
  </blockquote>
  
  <p>And from what I'm searched, the problem is because:</p>
  
  <p>curl_setopt($curl, CURLOPT_HTTPHEADER, $params);</p>
  
  <p>But I don't know how to adapt this piece of code, because the array $params is created as soon: </p>
  
  <pre><code>&lt;?php
  
  $params = array(
      'http' =&gt; array(
          'method' =&gt; 'POST',
          'content' =&gt; $data
      )
  );
  
  if ($optional_headers !== null)
  {
      $params['http']['header'] = $optional_headers;
  }
  </code></pre>
  
  <p>And when I print this array, it's what I obtain:</p>
  
  <blockquote>
    <p>Array ( [http] => Array ( [method] => POST [content] =>
    0e0c97c0663f5db12a6ccfef0a513da3 GetSettings 1 ) )</p>
  </blockquote>
  
  <p>Someone can help me?
  Thanks!</p>