【问题标题】:A single function for CDN-hosted scripts in WordPressWordPress 中 CDN 托管脚本的单一功能
【发布时间】:2012-12-12 03:35:38
【问题描述】:

根据服务器配置,我通常会使用两种方法来使用 PHP 远程检查 CDN 托管脚本的可用性。一个是cURL,另一个是fopen。我结合了我在各自情况下使用的两个函数,如下所示:

function use_cdn(){
   $url = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'; // the URL to check against
   $ret = false;
   if(function_exists('curl_init')) {
      $curl = curl_init($url);
      curl_setopt($curl, CURLOPT_NOBODY, true);
      $result = curl_exec($curl);
      if (($result !== false) && (curl_getinfo($curl, CURLINFO_HTTP_CODE) == 200)) $ret = true;   
      curl_close($curl);
   }
   else {
      $ret = @fopen($url,'r');
   }
   if($ret) {
      wp_deregister_script('jquery'); // deregisters the default WordPress jQuery
      wp_register_script('jquery', $url); // register the external file
      wp_enqueue_script('jquery'); // enqueue the external file
   }
   else {
      wp_enqueue_script('jquery'); // enqueue the local file
   }
}

...但我不想重新发明轮子。这是一种好的、可靠的技术,还是任何人都可以提供有关如何简化/简化流程的指示?

【问题讨论】:

  • 如果谷歌 CDN 宕机了,我会非常担心世界末日。 O.o
  • @cryptic 我知道,但 Google 不必关闭它的 CDN 以使其在某些地区/某些网络/等不可用。
  • 那么请看我的简化方法。

标签: php wordpress curl fopen cdn


【解决方案1】:

使用get_headers(),我们可以发出 HEAD 请求并检查响应代码以查看文件是否可用,还可以让我们查看网络或 DNS 是否关闭,因为它会导致 get_headers() 失败(保持如果域不可解析,@ 符号将抑制 PHP 错误,这将导致它在这种情况下返回 FALSE 并因此加载本地文件:

function use_cdn()
{
    $url = 'http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'; // the URL to check against
    $online = FALSE;

    if(function_exists('curl_init')) 
    {
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_NOBODY, TRUE);
        $result = curl_exec($curl);
        if ((FALSE !== $result) && ('200' == curl_getinfo($curl, CURLINFO_HTTP_CODE)))
        {
            $online = TRUE;
        }
        curl_close($curl);
    } 
    else if (ini_get('allow_url_fopen'))
    {
        stream_context_set_default(array('http' => array('method' => 'HEAD'))); // set as HEAD request
        $headers = @get_headers($url, 1); // get HTTP response headers
        if ($headers && FALSE !== strpos($headers[0], '200')) // if get_headers() passed and 200 OK
        {
            $online = TRUE;
        }
    }

    if ($online)
    {
        wp_deregister_script('jquery'); // deregisters the default WordPress jQuery
        wp_register_script('jquery', $url); // register the external file
    }
    wp_enqueue_script('jquery'); // enqueue registered files
}

get_headers() 会更快,因为它是一个内置函数,而不必加载诸如 cURL 之类的 PECL 扩展。至于 fopen() 你需要做的任务是检查响应头,get_headers() 的唯一用途就是这样做,fopen() 无法获取头,cURL 还有其他用途更不用说不必要的了开销并且不专门用于获取标头,因此在这种情况下使用它是最合适的选择。

【讨论】:

  • 我喜欢这样。与fopencURL 相比,使用get_headers() 有什么优势吗?有事实上的标准吗?
  • get_headers() 会更快,因为它是一个内置函数,而不必加载诸如 cURL 之类的 PECL 扩展。至于 fopen() 你需要做的任务是检查响应头,get_headers() 的唯一用途就是这样做,fopen() 无法获取头,cURL 还有其他用途更不用说不必要的了开销并且不专注于获取标头,因此在这种情况下使用它是最合适的选择。
  • 嗯,太好了!公认。也许在您的答案中添加注释来解释这些优势?
  • 有趣的是,我目前正在处理的服务器不执行get_headers() 请求。因此,如果有的话,我不得不退回到其他两种检查 200 响应的方法。猜猜这个问题毕竟没有灵丹妙药。
  • 好吧,如果你真的想要健壮的代码,你也不会使用 cURL,因为它也可以被禁用或在主机上不可用。我会做的可能是使用所有三个,按照效率最高到最低效率的顺序,并通过像你一样使用 function_exists() 来检查每个是否可用,并检查 ini_get('allow_url_fopen') == '1'对于 fopen() 和 get_headers(),这样您就可以回退到另一个,同时确保首先尝试最有效的方法。
猜你喜欢
  • 2015-04-24
  • 2021-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-18
  • 2017-12-03
  • 1970-01-01
  • 2020-06-13
相关资源
最近更新 更多