我想編程的雅虎信使機器人,現在我的機器人可以得到消息並回答他們。 雅虎只有在每次收到通知時纔可以發送100點。像在PHP中的線程
現在我想回答每個下午。我使用while(true){ }
來回答第一個下午,然後第二個,然後第三個......。 這太慢了,因爲我可以只通過這個(我使用curlib)與雅虎建立一個連接。 我怎樣才能同時發送一些消息?我想我需要的東西像線程,但在PHP中。
我想編程的雅虎信使機器人,現在我的機器人可以得到消息並回答他們。 雅虎只有在每次收到通知時纔可以發送100點。像在PHP中的線程
現在我想回答每個下午。我使用while(true){ }
來回答第一個下午,然後第二個,然後第三個......。 這太慢了,因爲我可以只通過這個(我使用curlib)與雅虎建立一個連接。 我怎樣才能同時發送一些消息?我想我需要的東西像線程,但在PHP中。
你可以使用pcntl_fork()。 http://www.php.net/manual/en/function.pcntl-fork.php
你需要了pcntl擴展,如果使用捲曲功能你可以看看curl_multi_init()只適用於Unix
我已經寫了下面的簡單功能,這啓動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);
}