PHP Curl API Response Time Differs from different Server

api, curl, php, rest

Solution

This is not the issue with curl anymore. Rather its the problem with your network setup. You can check this out by doing few things.

1) Use `ping` command to check the response time.

From Server-A: ping Server-B-IP
From Server-B: ping Server-A-IP

2) Similarly you can use the `traceroute`(for windows `tracert`) command to check the response time as well. You should get the response instantly.

From Server-A: traceroute Server-B-IP
From Server-B: traceroute Server-A-IP

3) Use `wget` or `curl` commandline to download a large file(let say 100 MB) From one server to another, and then check how long does they take. For example using `wget`:

From Server-B: wget http://server-A-IP/test/test-file.flv
From Server-A: wget http://server-B-IP/test/test-file.flv

4) Apart from these basic routine check, you can also use some advance tools to sort this network problem out. For example the commands/examples from the following two links:

Test network connection performance between two Linux servers Command line tool to test bandwidth between 2 servers

Problem

I have a set up where I have two servers running a thin-client (Apache, PHP). On Server A, it's consider a client machine and connects to Server B to obtain data via a Restful API. Both servers are on the same network. On Server B, the response of the request is shown below: ``` { "code": 200, "response_time": { "time": 0.43, "measure": "seconds" } } ``` Server B calculates the time completed for each task by using `microseconds` to flag the start and end of a request block. But when I use `curl` on Server A to make the call to the Server B, I get very strange results in terms on execution time: ``` $url = "https://example.com/api"; /*server B address. I've tried IP address as well without any change in results. This must go over a SSL connection. */ $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); ``` I get the following for each Time Check: ``` $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 in your network that holds your API? It would be like if Twitter's Site was consuming their API from another server that isn't the API server. I would think that the time for the `curl` to the API would be pretty similar to the time reported by the API. I understand there the API doesn't take into account network traffic and speed to open the connection - but 18 seconds versus 0.43 seems strange to me. Any ideas here?

Original source