异步调用PHP? [英] Asynchronous PHP calls?

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

问题描述

有没有在PHP中的一种方式进行异步HTTP调用?我不在乎响应,我只想做这样的事的file_get_contents(),而不是等待请求执行我的$ C $的其余部分之前完成C。这将是掀起一种事件在我的应用程序,或触发一个漫长的过程超好用。

Is there a way in PHP to make asynchronous http calls? I don't care about the response, I just want to do something like file_get_contents(), but not wait for the request to finish before executing the rest of my code. This would be super useful for setting off "events" of a sort in my application, or triggering long processes.

任何想法?

推荐答案

我倒是previously接受没有工作的答​​案。它仍然在等待答复。这不工作,虽然,从<一所href=\"http://stackoverflow.com/questions/962915/how-do-i-make-an-asynchronous-get-request-in-php\">How做我做在PHP中的异步GET请求?

The answer I'd previously accepted didn't work. It still waited for responses. This does work though, taken from How do I make an asynchronous GET request in PHP?

function post_without_wait($url, $params)
{
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);
    }
    $post_string = implode('&', $post_params);

    $parts=parse_url($url);

    $fp = fsockopen($parts['host'],
        isset($parts['port'])?$parts['port']:80,
        $errno, $errstr, 30);

    $out = "POST ".$parts['path']." HTTP/1.1\r\n";
    $out.= "Host: ".$parts['host']."\r\n";
    $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
    $out.= "Content-Length: ".strlen($post_string)."\r\n";
    $out.= "Connection: Close\r\n\r\n";
    if (isset($post_string)) $out.= $post_string;

    fwrite($fp, $out);
    fclose($fp);
}

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

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