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

 承志68694849 发布于 2022-11-13 11:53

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

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

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

7 个回答
  • 服务器到接口之间速度慢

    2022-11-13 12:01 回答
  • 是不是楼主自己的dns服务器比较卡

    2022-11-13 12:01 回答
  • 遇到了一样的问题,你这个url直接访问只有0.12s,但通过php访问5.3s

    2022-11-13 12:01 回答
  • 使用xdebug或者xhprof进行性能分析。

    2022-11-13 12:01 回答
  • 不慢吖,试了请求返回0.2s

    2022-11-13 12:01 回答
  • 你确定是php在这一步慢?最好将整个PHP执行顺序依次输出时间戳看一下到底哪一步慢。

    2022-11-13 12:01 回答
  • 简单用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
    
    2022-11-13 12:01 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有