类似php中的线程之类的东西

发布于 2024-12-06 04:16:50 字数 229 浏览 0 评论 0原文

我正在尝试对雅虎信使机器人进行编程,现在我的机器人可以接收消息并回复它们。 每次我收到通知时,雅虎只能发送下午 100 点消息。

现在我想回答每个下午。我使用 while(true){ } 来回答,然后回答第一个下午,然后第二个,然后第三个......。 它太慢了,因为我只能通过这个与雅虎建立一个连接(我使用curlib)。 我怎样才能同时发送一些消息?我想我需要类似线程的东西,但是在 php 中。

I'm trying to program yahoo messenger robot,now my robot can get messages and answer to them.
Yahoo only can send 100 pm at each time I get notification.

now I wanna answer the each pm. I use while(true){ } to it and answer first pm,then second ,then 3rd and ... .
It's so slow ,because I can make only one connection to yahoo by this(I use curlib).
how can I send some messages at same time?I think I need something like thread but in php.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屋顶上的小猫咪 2024-12-13 04:16:50

您可以使用 pcntl_fork()。 http://www.php.net/manual/en/function.pcntl -fork.php

你需要 pcntl 扩展,它只适用于 Unix

如果你使用curl 函数,你可以看看curl_multi_init()。 http://www.php.net/manual/en/function .curl-multi-init.php

you can use pcntl_fork(). http://www.php.net/manual/en/function.pcntl-fork.php

You need pcntl extension and it only works on Unix

If you use curl function you might take a look at curl_multi_init(). http://www.php.net/manual/en/function.curl-multi-init.php

攀登最高峰 2024-12-13 04:16:50

我在下面编写了一个简单的函数,它启动 URL,并且不等待结果,因此您可以在自己的网站上启动许多 URL,这将使您的循环速度更快,并且也无需在服务器上安装任何扩展。

function call_url_async($url, $params, $type='POST', $timeout_in_seconds = 1)
{
    //call the URL and don't wait for the result - useful to do time-consuming tasks in background
    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, $timeout_in_seconds);
    //if ($fp == FALSE)
    //  echo "Couldn't open a socket to ".$url." (".$errstr.")<br><br>";

    // Data goes in the path for a GET request
    if ('GET' == $type) 
        $parts['path'] .= '?'.$post_string;

    $out = "$type ".$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";
    // Data goes in the request body for a POST request
    if ('POST' == $type && isset($post_string)) 
        $out.= $post_string;

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

I've wrote a simple function below, which launches URL, and doesn't wait for a result, so like this you can launch many URLs on your own site, it will make your loop fast, and also no need to install any extensions to your server.

function call_url_async($url, $params, $type='POST', $timeout_in_seconds = 1)
{
    //call the URL and don't wait for the result - useful to do time-consuming tasks in background
    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, $timeout_in_seconds);
    //if ($fp == FALSE)
    //  echo "Couldn't open a socket to ".$url." (".$errstr.")<br><br>";

    // Data goes in the path for a GET request
    if ('GET' == $type) 
        $parts['path'] .= '?'.$post_string;

    $out = "$type ".$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";
    // Data goes in the request body for a POST request
    if ('POST' == $type && isset($post_string)) 
        $out.= $post_string;

    fwrite($fp, $out);
    fclose($fp);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文