php不管curl还是file_get_contents请求接口都很慢,但直接在浏览器里访问就很快,这是说明情况?

这是接口:
http://api.k780.com:88/?app=w...

现在问题就是无论我怎么用curl或者是file_get_contents()来请求接口都很慢,自己也查了下,说是将域名换成ip就好了,但ip有时候会跟换,而且这网站配置了虚拟域名,用ip访问不了

想问下有没有方法能缩短时间?

阅读 9k
7 个回答

简单用curl测了下,三次分别耗时0.229s, 0.127s0.117s,挺快的呀。从浏览器访问也是这个速度。 LZ还是应该检查代码,是不是在其他地方有高耗时的操作 -- 代码看不出来的话就打点看看时间呗。

➜  ~ psysh

Psy Shell v0.6.1 (PHP 5.5.35 — cli) by Justin Hileman
>>> 
>>> 
>>> 
>>> $x = curl_init('http://api.k780.com:88/?app=weather.today&weaid=%E9%9D%92%E6%B5%A6&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json')
=> curl resource #291
>>> $s = microtime(true); curl_exec($x); $e = microtime(true);
{"success":"1","result":{"weaid":"41","days":"2016-08-09","week":"星期二","cityno":"qingpu","citynm":"青浦","cityid":"101020800","temperature":"33℃/27℃","temperature_curr":"29℃","humidity":"79%","weather":"阵雨","weather_curr":"阵雨","weather_icon":"http://api.k780.com:88/upload/weather/d/3.gif","weather_icon1":"","wind":"东风","winp":"2级","temp_high":"33","temp_low":"27","temp_curr":"29","humi_high":"0","humi_low":"0","weatid":"4","weatid1":"","windid":"10","winpid":"203"}}⏎
=> 1470706896.6087
>>> $e - $s
=> 0.22865319252014
>>> $s = microtime(true); curl_exec($x); $e = microtime(true);
{"success":"1","result":{"weaid":"41","days":"2016-08-09","week":"星期二","cityno":"qingpu","citynm":"青浦","cityid":"101020800","temperature":"33℃/27℃","temperature_curr":"29℃","humidity":"79%","weather":"阵雨","weather_curr":"阵雨","weather_icon":"http://api.k780.com:88/upload/weather/d/3.gif","weather_icon1":"","wind":"东风","winp":"2级","temp_high":"33","temp_low":"27","temp_curr":"29","humi_high":"0","humi_low":"0","weatid":"4","weatid1":"","windid":"10","winpid":"203"}}⏎
=> 1470706909.3393
>>> $e - $s
=> 0.12704491615295
>>> $s = microtime(true); curl_exec($x); $e = microtime(true);
{"success":"1","result":{"weaid":"41","days":"2016-08-09","week":"星期二","cityno":"qingpu","citynm":"青浦","cityid":"101020800","temperature":"33℃/27℃","temperature_curr":"29℃","humidity":"79%","weather":"阵雨","weather_curr":"阵雨","weather_icon":"http://api.k780.com:88/upload/weather/d/3.gif","weather_icon1":"","wind":"东风","winp":"2级","temp_high":"33","temp_low":"27","temp_curr":"29","humi_high":"0","humi_low":"0","weatid":"4","weatid1":"","windid":"10","winpid":"203"}}⏎
=> 1470706919.4255
>>> $e - $s
=> 0.11749792098999

你确定是php在这一步慢?最好将整个PHP执行顺序依次输出时间戳看一下到底哪一步慢。

不慢吖,试了请求返回0.2s

使用xdebug或者xhprof进行性能分析。

遇到了一样的问题,你这个url直接访问只有0.12s,但通过php访问5.3s

是不是楼主自己的dns服务器比较卡

服务器到接口之间速度慢

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题