title: PHP Posting data to another php file hosted in a remote server using file_get_contents
body: <p>I have access to 3 different remote servers. They all host the same <strong>php file</strong>, it's named <code>processing.php</code>.</p>
  
  <p>And in a different server I have 3 pages :</p>
  
  <ol>
  <li><code>index.html</code> : that contains a form with POST method that send data to <code>forward.php</code></li>
  <li><code>forward.php</code> : that forward the form values to <code>processing.php</code> on the other servers</li>
  <li><code>processing.php</code> : Display the posted data</li>
  </ol>
  
  <p><strong>The Problem :</strong> the code in <code>processing.php</code> is not executed and the returned result is a plain text of the source code of <code>processing.php</code>!!</p>
  
  <p><strong>forward.php :</strong></p>
  
  <pre><code>$field1 = $_POST['field1']; 
  $field2 = $_POST['field2'];
  rtrim($_POST['listserver'],"-");
  $listServer = explode("-",$_POST['listserver']);
  
  foreach ($listServer as $server){
      if(!empty($server)){
          $url = 'http://'.$server.'/processing.php';
          $data = array('field1' =&gt; $field1, 'field2' =&gt; $field2);
          $options = array(
              'http' =&gt; array(
                  'header'  =&gt; "Content-type: application/x-www-form-urlencoded",
                  'method'  =&gt; 'POST',
                  'content' =&gt; http_build_query($data)
              )
          );
          $context  = stream_context_create($options);
          $result = file_get_contents($url, false, $context);
          if ($result === FALSE) {
              echo "You can't ";
          }else{
              echo ('result : '.$result);
          }
      }
  }
  </code></pre>
  
  <p><strong>Processing.php</strong></p>
  
  <pre><code>$field1 = $_POST['field1']; 
  $field2 = $_POST['field2'];
  $result = $field1+$field2;
  $myFile = "files/result.txt";
  $fh = fopen($myFile, 'w') or die("can't open file");
  fwrite($fh, $result);   
  fclose($fh);
  </code></pre>
  
  <p>But in all the servers, <code>result.txt</code> is empty!!</p>