Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP asynchronous cURL with callback

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.

like image 279
canibal_uruguayo Avatar asked Sep 25 '14 13:09

canibal_uruguayo


People also ask

Is curl asynchronous in PHP?

Short answer is no it isn't asynchronous. Longer answer is "Not unless you wrote the backend yourself to do so." If you're using XHR, each request is going to have a different worker thread on the backend which means no request should block any other, barring hitting process and memory limits.

How do you run asynchronous curls?

So, basically you need to create a class which extends the Thread class and everything you want to run asynchronously (rather parallely), would be put in the function run() of the class.


1 Answers

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);
    }
}
like image 94
Beachhouse Avatar answered Sep 22 '22 13:09

Beachhouse