类似于php中的线程
我正在尝试为yahoo messenger机器人编程,现在我的机器人可以获取消息并回复它们。 每次我收到通知,雅虎只能发送100个下午 现在我想回答每个下午的问题。我对它使用类似于php中的线程,php,sockets,curl,Php,Sockets,Curl,我正在尝试为yahoo messenger机器人编程,现在我的机器人可以获取消息并回复它们。 每次我收到通知,雅虎只能发送100个下午 现在我想回答每个下午的问题。我对它使用while(true){},然后回答第一个pm,第二个,第三个和。 它太慢了,因为我只能通过它与雅虎建立一个连接(我使用curlib)。 我怎么能同时发送一些消息呢?我想我需要一些类似于线程的东西,但是用php 您可以使用pcntl_fork() 您需要pcntl扩展,它只在Unix上工作 如果您使用curl函数,您可以查看
while(true){}
,然后回答第一个pm,第二个,第三个和。
它太慢了,因为我只能通过它与雅虎建立一个连接(我使用curlib)。
我怎么能同时发送一些消息呢?我想我需要一些类似于线程的东西,但是用php 您可以使用pcntl_fork()
您需要pcntl扩展,它只在Unix上工作
如果您使用curl函数,您可以查看curl\u multi\u init() 您可以使用pcntl_fork()
您需要pcntl扩展,它只在Unix上工作
如果您使用curl函数,您可以查看curl\u multi\u init() 我在下面编写了一个简单的函数,启动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);
}
函数调用\u url\u async($url,$params,$type='POST',$timeout\u in_seconds=1)
{
//调用URL,不要等待结果-在后台执行耗时的任务非常有用
foreach($key=>&$val的参数)
{
if(is_数组($val))
$val=内爆(“,”,$val);
$post_params[]=$key.'='.urlencode($val);
}
$post_string=内爆('&',$post_参数);
$parts=parse_url($url);
$fp=fsockopen($parts['host'],isset($parts['port'])?$parts['port']:80,$errno,$errstr,$timeout(以秒计);
//如果($fp==FALSE)
//echo“无法打开“$url.”(“$errstr.”)的套接字
”;
//数据进入GET请求的路径
if('GET'=$type)
$parts['path'].='?'.$post_字符串;
$out=“$type”。$parts['path']”HTTP/1.1\r\n;
$out.=“主机:”.$parts[“主机”]。“\r\n”;
$out.=“内容类型:应用程序/x-www-form-urlencoded\r\n”;
$out.=“内容长度:”.strlen($post\u字符串)。“\r\n”;
$out.=“连接:关闭\r\n\r\n”;
//数据进入POST请求的请求正文
if('POST'=$type&&isset($POST\u string))
$out.=$post\u字符串;
fwrite($fp,$out);
fclose($fp);
}
我在下面编写了一个简单的函数,它启动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);
}
函数调用\u url\u async($url,$params,$type='POST',$timeout\u in_seconds=1)
{
//调用URL,不要等待结果-在后台执行耗时的任务非常有用
foreach($key=>&$val的参数)
{
if(is_数组($val))
$val=内爆(“,”,$val);
$post_params[]=$key.'='.urlencode($val);
}
$post_string=内爆('&',$post_参数);
$parts=parse_url($url);
$fp=fsockopen($parts['host'],isset($parts['port'])?$parts['port']:80,$errno,$errstr,$timeout(以秒计);
//如果($fp==FALSE)
//echo“无法打开“$url.”(“$errstr.”)的套接字
”;
//数据进入GET请求的路径
if('GET'=$type)
$parts['path'].='?'.$post_字符串;
$out=“$type”。$parts['path']”HTTP/1.1\r\n;
$out.=“主机:”.$parts[“主机”]。“\r\n”;
$out.=“内容类型:应用程序/x-www-form-urlencoded\r\n”;
$out.=“内容长度:”.strlen($post\u字符串)。“\r\n”;
$out.=“连接:关闭\r\n\r\n”;
//数据进入POST请求的请求正文
if('POST'=$type&&isset($POST\u string))
$out.=$post\u字符串;
fwrite($fp,$out);
fclose($fp);
}