8

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.

18
  • The add script has to echo something. Commented Sep 25, 2014 at 13:34
  • 2
    curl is not asynchronous. Commented Sep 25, 2014 at 13:35
  • i searched in google many articles about it and i found some of them talking about asynchronous cURL. For example in this page they are talking about this: php.net/manual/pt_BR/function.curl-setopt.php Commented Sep 25, 2014 at 13:44
  • 1
    See curl_multi_init (not async, but can run multiple requests in parallel). For async, you have to use Gearman (gearman.org). It's available as a php extension. Not sure, if that would help you. Commented Sep 25, 2014 at 14:16
  • 1
    Your code doesn't work because you set a 4-second timeout, and the server sleeps for 15 seconds. Commented Sep 25, 2014 at 14:24

1 Answer 1

10

Maybe take a look at this: https://gist.github.com/Xeoncross/2362936

Request:

class Requests
{
    public $handle;

    public function __construct()
    {
        $this->handle = curl_multi_init();
    }

    public function process($urls, $callback)
    {
        foreach ($urls as $url) {
            $ch = curl_init($url);
            curl_setopt_array($ch, array(CURLOPT_RETURNTRANSFER => TRUE));
            curl_multi_add_handle($this->handle, $ch);
        }

        do {
            $mrc = curl_multi_exec($this->handle, $active);

            if ($state = curl_multi_info_read($this->handle)) {
                //print_r($state);
                $info = curl_getinfo($state['handle']);
                //print_r($info);
                $callback(curl_multi_getcontent($state['handle']), $info);
                curl_multi_remove_handle($this->handle, $state['handle']);
            }

            usleep(10000); // stop wasting CPU cycles and rest for a couple ms

        } while ($mrc == CURLM_CALL_MULTI_PERFORM || $active);

    }

    public function __destruct()
    {
        curl_multi_close($this->handle);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for sharing seem useful, I will sure try this.. Does it also work with cron system ?
Sure, I wouldn't see a reason it wouldn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.