doulechou0700 2015-07-25 07:30
浏览 40

使用php时“CDN无法访问”服务器端的“后备”

i want simulate server side something so:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined')
{
    document.write(unescape("%3Cscript src='/Scripts/jquery-1.3.2.min.js' type='text/javascript'%3E%3C/script%3E"));
}
</script>

And so, i have in php:

function isurlok($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    if(!curl_errno($ch))
        $info = curl_getinfo($ch);
    curl_close($ch);
    if (isset($info) && $info['http_code'] == 200) {
        if ($info['http_code'] == 200 ) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

For get the available of the resource (in this case the file) and then this:

if ( isurlok ("https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js") {
  // remote file
  print "<script type=\"text/javascript\" src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js\"></script>";
  print "remote file";
} else {
 // local file
 print "<script type=\"text/javascript\" src=\"jquery/1.11.3/jquery.min.js\"></script>";
 print "local file";
}

Why it not work? I mean that script return "local file" when remote file exists and i wait that return "remote file". If i change url of Google with: "http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" then it work correctly. Where i mistake? And in case, how i can fix the script becouse it work Always? Thanks very much.

  • 写回答

1条回答 默认 最新

  • doufei5315 2015-07-25 07:42
    关注

    The curl function requires a few more parameters as you are trying to fetch from a https source.

    function isurlok( $url ) {
        /* You can download the cacert.pem from the web - search on Google */
        $cacert='/path/to/cacert.pem';
    
        $ch = curl_init( $url );
        curl_setopt( $ch, CURLOPT_NOBODY, true );
        curl_setopt( $ch, CURLOPT_USERAGENT, 'curl-fetchurl' );
    
        if( parse_url( $url,PHP_URL_SCHEME )=='https' ){
            curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, FALSE );
            curl_setopt( $curl, CURLOPT_SSL_VERIFYHOST, 1 );
            curl_setopt( $curl, CURLOPT_CAINFO, realpath( $cacert ) );
        }
    
        curl_exec( $ch );
        if( !curl_errno( $ch ) ) $info = curl_getinfo( $ch );
        curl_close( $ch );
    
    
        if ( isset( $info ) && array_key_exists('http_code',$info)) {
            return $info['http_code'] == 200 ? true : false;
        }
        return false;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?