title: PHP asynchronous cURL with callback
body: <p>I am trying to do a request using cURL in an asynchronous way with callback. I am using a piece of code that i copy from a site.</p>
  
  <p>When i write in my browser this url: <a href="http://www.myhost:3049/exemplo/index/async/" rel="noreferrer">http://www.myhost:3049/exemplo/index/async/</a> its execute the function asyncAction thats execute the curl_post function.</p>
  
  <pre><code>/** 
  * Send a POST requst using cURL 
  * @param string $url to request 
  * @param array $post values to send 
  * @param array $options for cURL 
  * @return string 
  */ 
  function curl_post($url, array $post = NULL, array $options = array()) 
  { 
      $defaults = array( 
          CURLOPT_POST =&gt; 1, 
          CURLOPT_HEADER =&gt; 0, 
          CURLOPT_URL =&gt; $url, 
          CURLOPT_FRESH_CONNECT =&gt; 1, 
          CURLOPT_RETURNTRANSFER =&gt; 1, 
          CURLOPT_FORBID_REUSE =&gt; 1, 
          CURLOPT_TIMEOUT =&gt; 4, 
          CURLOPT_POSTFIELDS =&gt; http_build_query($post) 
      ); 
  
      $ch = curl_init(); 
      curl_setopt_array($ch, ($options + $defaults)); 
      if( ! $result = curl_exec($ch)) 
      { 
          $result = curl_error($ch);
      } 
      curl_close($ch); 
      return $result; 
  } 
  
  
  public function asyncAction() {
      $this-&gt;curl_post("http://www.myhost:3049/exemplo/index/add/");
  }
  </code></pre>
  
  <p>Then the cURL execute cURL to that URL to execute an action that NOW is in the same class that the others function, just for testing. This action is addAction, that just return a string with the message "CALLBACK".</p>
  
  <pre><code>function addAction() {
      sleep(15);
      return "CALLBACK";
  }
  </code></pre>
  
  <p>The $result is returning just false. 
  Maybe the problem is that i am requesting trying to execute an action that is in the same class that the cURL function. But anyways, how can i get the error message.
  Is there any better solution, tested, and with good explanation about using as asynchronous with callback? Because the things i read are not well explained and also it does not explain when, how to manage the callback thing.</p>