关于飞信,我上次写过学着写PHP飞信,然而今天晚上我把上文中测试的内容重现时却发现,现实有点残酷。我用curl的时候,速度居然没有fsocketopen快?我于是多次测试,然而,我却得到了一些令人沮丧的结果

【$postData,$postLength,都不提供。。。】

1、curl,利用curl来post

$ch = curl_init();
$chOptions = array(
CURLOPT_URL => "http://nav.fetion.com.cn/nav/getsystemconfig.aspx",
CURLOPT_HTTP_VERSION => 'HTTP/1.0',
CURLOPT_USERAGENT => sprintf('IIC2.0/pc %s' , self::FETION_CLIENT_VERSION),
CURLOPT_ENCODING => 'gzip,deflate',
CURLOPT_HTTPHEADER => array('Content-Type: text/xml; charset=utf-8'),
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postData ,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 0
);
curl_setopt_array($ch, $chOptions);
$res = curl_exec($ch);

2、fsocketopen,同样POST数据

$headers=<<<eot
POST /nav/getsystemconfig.aspx HTTP/1.0
User-Agent: IIC2.0/pc 3.1.0480
Accept-Encoding: deflate, gzip
Host: nav.fetion.com.cn:80
Content-Length: {$postLength}

{$postData}
eot;
fwrite($fp,$headers);
$received=fread($fp,1024);
fclose($fp);

3、stream,利用文件流

$opts = array (
'http' => array (
'method' => 'POST',
'header'=> "Content-type: application/x-www-form-urlencoded" .
"Content-Length: " . $postLength . "",
'content' => $postData
),
);
$context = stream_context_create($opts);
//$received = file_get_contents('http://nav.fetion.com.cn/nav/getsystemconfig.aspx', false, $context , 0 ,1024);
$fp = fopen('http://nav.fetion.com.cn/nav/getsystemconfig.aspx','r',false,$context);
if($fp){
$received = fgets($fp, 512); //在这里我用了三种测试,fgets,fread,stream_get_contents
fclose($fp);
}

在这里我用了两种方式,一种是file_get_contents还有一种是fopen,file_get_contents的方法被我注释了。。。

在这三种中,我发现真的只有fsocketopen后然后fread是最快的。

真让我沮丧。。因为第三种方法中file_get_contens也可以指定读取的长度。但PHPRPC作者andot却认为,file_get_contents把头已经读回来了。我想,file_get_contents其实已经把数据读回来了,虽然是指定了长度,但其实只是读回来后再截取的。

唉。看来,不得不用fsocketopen了。。不知道除此之外有没有什么好办法。