arrow_back Is it right to expect ping in this way?

3 votes
There is a small function which I check whether the server:
public function check_url($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($httpcode>=200 && $httpcode<300 or $httpcode == 302) {
return 'alive';
} else {
return 'dead';
Is it right to measure the response time from the server using this function but remembering first microtime(true), and before return the output difference with the current microtime(true) and multiply all this a thousand? If ping thus regular domains it seems to be all right, but if I try to ping some files(stick .swf), then the ping becomes ~10 times more.


Alex Kiselev what should be done to consider only the response time from the server?
"ping becomes ~10 times more"
Of course, you do not feel the response time from the server, and the download of the specified resource. And since the flash drive weighs more, and she swings longer than a page of the website.

1 Answer

Best answer
0 votes
ping is to check the server availability, to obtain the answer for this is optional.
accordingly, something like this will be faster:
$host = ''; 
$port = 80;
$waitTimeoutInSeconds = 1;
if($fp = fsockopen($host,$port,$errCode,$errStr,$waitTimeoutInSeconds)){
// It worked
} else {
// It didn't work
you have not ping and the address verification. to speed up to change the CURLOPT_RETURNTRANSFER to false and check just the response code.