PHP异步卷曲与回调 [英] PHP asynchronous cURL with callback

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

问题描述

我试图做与回调的异步方式使用卷曲的请求。我使用的是一块code的,我从一个网站复制。

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 / exemplo /指数/异​​步/ 其执行的函数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/");
}

然后卷曲执行卷曲到URL来执行操作,现在是在同一类的其他功能,只需进行检测。这个动作的addAction,只是返回一个字符串消息回调。

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.

推荐答案

也许看看这个: 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异步卷曲与回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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