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

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

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

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

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


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


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


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


简单用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
【热门文章】
【热门文章】