一直想解决EMlog飞信插件发表留言后漫长等待返回发送结果的困扰,今天用fsockopen()函数解决了这个问题。原理很简单,就是php模拟post表单,且去掉返回结果。这样速度就快多了。核心代码如下:
<?php
$msg='测试飞信用';
$host =$_SERVER['HTTP_HOST'];
$post=rawurlencode('msg').'='.rawurlencode($msg);
$len = strlen($post);
$file ="/fetion/api.php";
$fp = @fsockopen( $host , 80, $errno, $errstr, 30);
if (!$fp)
{
echo "$errstr ($errno)\n";
}
else
{
$out = "POST $file HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Content-type: application/x-www-form-urlencoded\r\n";
$out .= "Connection: Close\r\n";
$out .= "Content-Length: $len\r\n";
$out .="\r\n";
$out .= $post."\r\n";
fwrite($fp, $out);
fclose($fp);
}
?>