title: PHP Curl script generating duplicate requests
body: <p>I have a PHP Curl based function that is used for various requests in an application. The function is normally used to communicate/trigger requests on other servers on same machine (localhost). The function is below: -</p>
  
  <pre><code>/**
   * @desc If a curl_multi handle is passed, a new curl 
   * instance is added to the handle and the curl id is returned as string.
   * @param string $theServer
   * @param string $thePath
   * @param number $thePort
   * @param curl_multi $mcHandle
   * @return string|curl_handle
   */
  function sendPOSTtoURL($theServer, $thePath, $theData, 
                          $thePort = 80, $mcHandle = null) {
      global $bDebug; $theResult = "";    //$bDebug = true;
      $cPost = curl_init();
      if ($cPost !== false)   {
          curl_setopt($cPost, CURLOPT_URL, "http://".$theServer.$thePath);
          if ($thePort != 80) curl_setopt($cPost, CURLOPT_PORT, $thePort);    
              // Set port if different from 80
          curl_setopt($cPost, CURLOPT_HEADER, false);
          curl_setopt($cPost, CURLOPT_FORBID_REUSE, true);
          curl_setopt($cPost, CURLOPT_FRESH_CONNECT, true);
          curl_setopt ($cPost, CURLOPT_POST, true);
          curl_setopt ($cPost, CURLOPT_POSTFIELDS, $theData);
          curl_setopt ($cPost, CURLOPT_RETURNTRANSFER, true);
          $returndata = curl_exec ($cPost);
          if ($mcHandle == null)  {
              if (! $theResult = curl_exec($cPost))   {
                  if (curl_errno($cPost) &gt; 0) {   
                                  $theResult = CURL_ERROR;    
                                  if ($bDebug)    
                                        $theResult .= fcURLError($cPost); 
                              }
                  else    {
                                  $theResult = CURL_NOERR_NOINFO; 
                                  if ($bDebug)
                                        $theResult .= fcURLError($cPost); 
                              }
              }
              curl_close($cPost);
              return $theResult;
          }   else    {
              curl_multi_add_handle($mcHandle, $cPost);
              return $cPost;
          }
      }   else    return CURL_INIT_FAIL;
  }
  </code></pre>
  
  <p>I wont go into complexities, but it is designed to return text if it is a single request or a curl handle is appended if a multi_curl handle is passed.</p>
  
  <p>The issue is, when I request it to do a task on a remote server, it does a double request. We have a setup where the requesting server is connected to remote server via VPN and can do a requests to external Internet through it. I thought the target server was running it twice so I made a call (for doing an email) from the calling server as follows: -</p>
  
  <pre><code>    $aEmail = array("email_address" =&gt; $sEmailAddress, 
                      "email_subject" =&gt; $sEmailSubject, 
                      "email_body" =&gt; $sEmailBody);
      echo sendPOSTtoURL("&lt;REMOTE IP HERE", 
                         "/emailService/doMail.php?rand=".mt_rand(), 
                         $aEmail);
  </code></pre>
  
  <p>the call to mt_rand generates a random number so I can identify each request. This is what I got at the remote server: -</p>
  
  <pre><code>&lt;CALLING IP&gt; - - [15/Jun/2011:20:39:57 +0500] "POST /emailService/doMail.php?rand=1551627310 HTTP/1.1" 200 1060 "-" "-"
  &lt;CALLING IP&gt; - - [15/Jun/2011:20:40:01 +0500] "POST /emailService/doMail.php?rand=1551627310 HTTP/1.1" 200 1060 "-" "-"
  </code></pre>
  
  <p>As you can see, the same request came in twice. and what do you think happens? right, 2 emails to the target! I cant (due to managerial [S@#$] reasons) develop logic to handle this on the Remote server.</p>