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.
When i write in my browser this url: http://www.myhost:3049/exemplo/index/async/ its execute the function asyncAction thats execute the curl_post function.
/**
* 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 => 1,
CURLOPT_HEADER => 0,
CURLOPT_URL => $url,
CURLOPT_FRESH_CONNECT => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_FORBID_REUSE => 1,
CURLOPT_TIMEOUT => 4,
CURLOPT_POSTFIELDS => 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->curl_post("http://www.myhost:3049/exemplo/index/add/");
}
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".
function addAction() {
sleep(15);
return "CALLBACK";
}
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.
Maybe take a look at this: https://gist.github.com/Xeoncross/2362936
Request: