dtbc37573 2018-09-07 11:50
浏览 118
已采纳

PHP cURL查找时间大约需要5秒

I have an API which makes a get request using curl to get response from an external host.

The lookup time is 5 seconds which is about 10-20 times slower than it should be. I also tried file_get_contents(<url>) and that also takes about 5 seconds.

Then I tried pinging the same source through my VPS (through ssh) and found that DNS lookup time for ping was also 5 secs so I tried changing the dns setting of server and gave it another DNS ip to use (google and one another) and solved the problem for the server. Now the ping from server is instant and doesn't take time to resolve domain name to IP.

However, curl requests still take same time. 5 seconds for an API command is too slow. What can be changed here? What can I do to make this faster?

Here is the curl_info log:

[url] => https://www.hungrybulb.com/pony/relay.php/?object=user&user=4fd582133861b5c74b4dab7ba42934aa1&scene=home-tv+series
[content_type] => text/html; charset=UTF-8
[http_code] => 200
[header_size] => 234
[request_size] => 143
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 5.254223
[namelookup_time] => 5.191469
[connect_time] => 5.192079
[pretransfer_time] => 5.246915
[size_upload] => 0
[size_download] => 1
[speed_download] => 0
[speed_upload] => 0
[download_content_length] => 1
[upload_content_length] => 0
[starttransfer_time] => 5.254214
[redirect_time] => 0
[certinfo] => Array
    (
    )

[request_header] => GET /pony/relay.php/?object=user&user=4fd582133861b5c74b4dab7ba42934aa1&scene=home-tv+series HTTP/1.1
Host: www.hungrybulb.com
Accept: */*

Update 1: The same code works quite fast when I run it on localhost. I assume that is because my local machine's DNS lookup doesn't take time.

  • 写回答

1条回答 默认 最新

  • dpka7974 2018-09-20 20:24
    关注

    The problem was with the DNS server that my server was using as primary. I was using 8.8.8.8 as primary and 8.8.4.4 for secondary. I removed 8.8.8.8 and made 8.8.4.4 as primary and added one more free DNS as secondary.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line