I have a setup where I have two servers running a thin client (Apache, PHP). On server A, it examines the client machine and connects to server B to receive data through the Restful API. Both servers are on the same network. On server B, the request response is shown below:
{ "code": 200, "response_time": { "time": 0.43, "measure": "seconds" } }
Server B calculates the time completed for each task using microseconds to mark the beginning and end of the request block. But when I use curl on server A to call to server B, I get very strange results in terms of runtime:
$url = "https://example.com/api"; $start_time = microtime(true); $curl2 = curl_init(); curl_setopt($curl2, CURLOPT_URL, $url); curl_setopt($curl2, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl2, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl2, CURLOPT_USERAGENT, "Server A User Agent"); $result = curl_exec($curl2); $HttpCode = curl_getinfo($curl2, CURLINFO_HTTP_CODE); $total_time = curl_getinfo($curl2, CURLINFO_TOTAL_TIME); $connect_time = curl_getinfo($curl2, CURLINFO_CONNECT_TIME); $namelookup_time = curl_getinfo($curl2, CURLINFO_NAMELOOKUP_TIME); $end_time = microtime(true); $timeDiff = round(((float)$end_time - (float)$start_time), 3);
For each time check, I get the following:
$timeDiff = 18.7381 (Using Microseconds) $total_time = 18.7381 (Transfer Time) $connect_time = 0.020679 $namelookup_time = 0.004144
So I'm not sure why this is happening. Is there a better way to source data from another server on your network that contains your API? It would be as if the Twitter site was using its API from another server that is not an API server. I think the time for curl for the API will be very similar to the time indicated by the API. I understand that the API does not take into account network traffic and speed to open a connection, but it seems to me that 18 seconds against 0.43 seems strange to me.
Any ideas here?
rest api php curl
gregavola
source share