PHP Curl API response time is different from another server

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"; /*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); 

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?

+8
rest api php curl
source share
2 answers

This is no longer a problem with curl. Rather, it is a network setup problem. You can verify this by doing a few things.

1) Use the ping command to check response time.

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

2) Similarly, you can use the traceroute command (for windows tracert ) to check the response time. You should get an answer instantly.

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

3) Use the wget or curl command line to download a large file (say 100 MB). From one server to another, and then check how long 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) In addition to this basic routine check, you can also use some preliminary tools to sort this network problem. For example, commands / examples from the following two links:

Check network connectivity between two Linux servers
Command line tool for checking bandwidth between two servers

+7
source share

I had the same problem about 3 days ago. I spent all day to find a problem. In the end, I contacted the server provider and told him about the problem. He said that this is not a problem of my script, but a carrier (network).

Perhaps this is the same problem that I encountered, so contact your server provider and ask for it. Have you tried it with file_get_contents? It would be interesting if the response time was the same.

+1
source share

All Articles