PHP射后不理POST请求 [英] PHP Fire and Forget POST Request

查看:794
本文介绍了PHP射后不理POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创造一个火,忘记PHP的方法让我能 POST 数据到网络服务器,而不必等待回应。我读这可以通过使用来实现卷曲如以下code:

I'm trying to create a fire and forget method in PHP so that I can POST data to a web server and not have wait for a response. I read that this could be achieved by using CURL like in the following code:

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_exec($ch);
curl_close($ch);

不过,我不认为它会像我期望的那样。例如,如果我将请求发送到URL有一个错误会引起我的脚本抛出错误也。如果是火灾,忘记我预计不会发生的。

However I don't think it works as I expect. For example if the URL I send the request to has an error it causes my script to throw an error as well. If it was fire and forget I would expect that not to happen.

谁能告诉我,我是不是做错了什么,或可供选择的建议。我使用Windows本地和Linux进行开发,分期和生产环境。

Can anyone tell me whether I'm doing something wrong or offer an alternative suggestion. I'm using Windows locally and Linux for dev, staging and production environments.

更新

我已经找到一个替代解决方案在这里: http://blog.markturansky.com/archives/205

I have found an alternative solution here: http://blog.markturansky.com/archives/205

我已经清理起来到下面的code:

I've cleaned it up into the code below:

function curl_post_async($url, $params = array())
{
    // create POST string   
    $post_params = array();
    foreach ($params as $key => &$val) 
    {
        $post_params[] = $key . '=' . urlencode($val);
    }
    $post_string = implode('&', $post_params);

    // get URL segments
    $parts = parse_url($url);

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

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

    // send output to $url handle
    fwrite($fp, $output);
    fclose($fp);
}

这一次似乎更好地为我工作。

This one seems to work better for me.

它是一个有效的解决方案?

Is it a valid solution?

推荐答案

是的,使用套接字是,如果你不关心从你调用的URL的响应要走的路。这是因为socket连接可以直接发送请求,而无需等待后终止,这正是你追求什么 - 射后不理

Yes, using sockets is the way to go if you don't care about the response from the URL you're calling. This is because socket connection can be terminated straight after sending the request without waiting and this is exactly what you're after - Fire and Forget.

有两点需要注意,虽然


  1. 这不再是一个卷曲的请求,所以它的价值重命名功能。 :)

  2. 这绝对值得一试是否能插座已经被打开,prevent的脚本抱怨后,当如果失败:

  1. It's no longer a cURL request, so it's worth renaming the function. :)
  2. It's definitely worth checking whether the socket could've been opened to prevent the script from complaining later when if fails:

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

if ( ! $fp)
{
   return FALSE;
}


这是值得链接到你现在正在使用的fsocket()脚本的原始来源:结果
http://w-shadow.com/blog/2007/10/16/how-to-run-a-php-script-in-the-background/

It's worth linking to the original source of the fsocket() script you're now using:
http://w-shadow.com/blog/2007/10/16/how-to-run-a-php-script-in-the-background/

这篇关于PHP射后不理POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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