带回调的 PHP 异步 cURL [英] PHP asynchronous cURL with callback

查看:61
本文介绍了带回调的 PHP 异步 cURL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以异步方式使用 cURL 和回调来执行请求.我正在使用从网站复制的一段代码.

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.

当我在浏览器中输入这个网址时:http://www.myhost:3049/例如/index/async/ 其执行函数asyncAction,即执行curl_post 函数.

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/");
}

然后 cURL 对那个 URL 执行 cURL 以执行一个动作,该动作现在与其他函数在同一个类中,仅用于测试.这个动作是addAction,它只返回一个带有消息CALLBACK"的字符串.

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";
}

$result 返回的只是假的.也许问题是我正在请求尝试执行与 cURL 函数在同一类中的操作.但无论如何,我怎么能得到错误信息.有没有更好的解决方案,经过测试,并很好地解释了使用异步回调?因为我读的东西没有很好的解释,也没有解释什么时候,如何管理回调的东西.

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.

推荐答案

也许看看这个:https://gist.github.com/Xeoncross/2362936

请求:

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);
    }
}

这篇关于带回调的 PHP 异步 cURL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆