According to the manual, the only difference between the two functions is that thePfsockopen is a continuous connection, while the Fsockopen is not.
I wrote a code for a moment:
Copy Code code as follows:
<?php
$data = "1,0,721,73,1,0,0,43290000,0,60D81D509BC00451,3,FFFFFFFF";
//http://10.144.99.114/sanex_new/modules/subscribemanager/test.php
$host = ' 127.0.0.1 ';
$url = "/aa.php";
$pffirst = false;
$times = 1000;
$startTime = Microtime (true);
for ($index = 0; $index < $times; $index + +) {
Echo HttpPost ($host, $url, $data, $pffirst). " <hr><br/> ";
}
$middleTime = Microtime (true);
for ($index = 0; $index < $times; $index + +) {
Echo HttpPost ($host, $url, $data,! $pffirst). " <hr><br/> ";;
}
$endTime = Microtime (true);
Echo ($pffirst?) Pfsocket ":" Fsocket ").": ". ($middleTime-$startTime);
echo "<br/>";
Echo ($pffirst?) Fsocket ":" Pfsocket ").": ". ($endTime-$middleTime);
$count = 0;
//Contract function
function HttpPost ($host, $url, $data, $p)
{
Global $count;
$func = $p? " Pfsockopen ":" Fsockopen ";
$conn = $func ($host, $errno, $errstr, 30);
if (! $conn)
{
echo "$errstr ($errno) <br/>n";
return;
}
$header = "POST". $url. " Http/1.1rn ";
$header. = "Host: {$host}rn";
$header. = "CONTENT-TYPE:APPLICATION/X-WWW-FORM-URLENCODEDRN";
$header. = "Content-length:". strlen ($data). " RN ";
$header. = "Connection:keep-alivernrn";
$header. = "{$data}rnrn";
fwrite ($conn, $header);
$count + +;
echo $count. ' '. $header. " <br/><br/> ";
$resp = ';
//while (!feof ($conn)) {
//$resp. = Fgets ($conn);
//}
//fclose ($conn);
return $RESP;
}
?>
The results showed that :
The second line of code, if the//fclose ($conn), comments out, the result is:
fsocket:11.04693198204
pfsocket:0.34867787361145
If you do not comment:
fsocket:12.509312152863
pfsocket:11.120275974274
Can be seen, fsocketopen default each processing end, even if the protocol head is keep-alive, the connection is still broken off.
and Pfsocketopen under keep-alive conditions, the connection can be reused for the next time.
Pfsocketopen is recommended when sending large amounts of data on a single connection