zoukankan      html  css  js  c++  java
  • file_get_content没远程没响应解决方案

     

    file_get_contents是个好东西,在接口调用中经常用到,弊端就是在远程接口没有响应时,执行就会卡住直到超时很不爽,于是修改http头,如下:
     
    $ctx=stream_context_create(array(
            'http'=>array(
                'timeout'=>3//等待3秒
                )
            )
        );
        $result = @file_get_contents ( $url,0,$ctx );
     
    bingo,解决问题,另一种思路是模拟请求:
     
    function file_get_content($url) {
            $ch = curl_init();   
            $timeout = 3;
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
            $result = curl_exec($ch);
            curl_close($ch);
    }
     




  • 相关阅读:
    jeesite导入导出
    jeesite下载
    百度echart
    js获取日期
    清除svn
    父子窗口
    JS高级程序设计之高级技巧
    JS中离线应用与客户端存储
    JS最佳实践
    JSON之JS高级程序设计笔记
  • 原文地址:https://www.cnblogs.com/firmy/p/2818975.html
Copyright © 2011-2022 走看看