【问题标题】:any idea to decrease requests via GET通过 GET 减少请求的任何想法
【发布时间】:2012-12-22 08:34:48
【问题描述】:

我制作了一个 video.php 页面,该页面将根据 youtube 上视频的 ID 显示来自 youtube 的视频,所以我做了类似的东西

if($_GET['id']=="jquery11"){
    ?>
<div id="video" ><iframe   width="640" height="360" src="http://www.youtube.com/embed/6PM6t5RFR2w?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>

    <?
    }

if($_GET['id']=="jquery12"){
    ?>
<div id="video" ><iframe   width="640" height="360" src="http://www.youtube.com/embed/fE0GC7KFIno?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>

    <?
    }

if($_GET['id']=="jquery13"){
    ?>
<div id="video" ><iframe   width="640" height="360" src="http://www.youtube.com/embed/xuFa2R4c6Gc?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>

    <?
    }

if($_GET['id']=="jquery14"){
    ?>
<div id="video" ><iframe   width="640" height="360" src="http://www.youtube.com/embed/M971xYWiS7M?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>

    <?
    }

if($_GET['id']=="jquery15"){
    ?>
<div id="video" ><iframe   width="640" height="360" src="http://www.youtube.com/embed/oZ_J422z4WI?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>

    <?
    }

if($_GET['id']=="jquery16"){
    ?>
<div id="video" ><iframe   width="640" height="360" src="http://www.youtube.com/embed/tYJMitUfSvs?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>

    <?
    }


if($_GET['id']=="jquery17"){
    ?>
<div id="video" ><iframe   width="640" height="360" src="http://www.youtube.com/embed/wZPhQzqGzls?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>

    <?
    }


if($_GET['id']=="jquery18"){
    ?>
<div id="video" ><iframe   width="640" height="360" src="http://www.youtube.com/embed/Cdwn2JoCYQ0?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>

    <?
    }


if($_GET['id']=="jquery19"){
    ?>
<div id="video" ><iframe   width="640" height="360" src="http://www.youtube.com/embed/cKCiNf23Atg?autoplay=1&controls=2" frameborder="5" allowfullscreen></iframe></div>

    <?
    }

?>

有什么想法可以改变这种方法以减少获取请求吗?我尝试制作关联数组,但我认为它是相同的

【问题讨论】:

    标签: php get request


    【解决方案1】:
    $map = array(
        "jquery11" => "6PM6t5RFR2w",
        "jquery12" => "fE0GC7KFIno",
        "jquery13" => "xuFa2R4c6Gc",
        "jquery14" => "M971xYWiS7M",
        "jquery15" => "oZ_J422z4WI",
        "jquery16" => "tYJMitUfSvs",
        "jquery17" => "wZPhQzqGzls",
        "jquery18" => "Cdwn2JoCYQ0",
        "jquery19" => "cKCiNf23Atg"                 
    );
    
    $id = $_GET['id'];
    
    if( array_key_exists( $id, $map ) ) {
        echo <<<HTML
        <div id="video" >
            <iframe   width="640" height="360" src="http://www.youtube.com/embed/{$map[$id]}?autoplay=1&controls=2" frameborder="5" allowfullscreen>
            </iframe>
        </div>
    HTML;
    }
    

    【讨论】:

      【解决方案2】:

      创建一个可能的 id 数组,然后在 if 语句下使用 in_array 检查被传递的变量是否在数组中,然后从那里分配正确的 url。

      【讨论】:

      • 感谢 mrtchie,但检查 var 需要相同的 GET 请求,因为访问者会选择从另一个页面打开视频,我想知道他选择观看的视频是什么,而这个只能通过 get 获取视频的 id
      【解决方案3】:

      未经测试 - 应该可以工作:

      <?php
      
      $request = $_GET['request'];
      
      
      
      $accepted_requests = array(1 =>'jquery1', 2 => 'jqery2', 3 => 'jquery3');
      
      $utube_urls = array(1 => 'url', 2 => 'url', 3 => 'url');
      
      
          if(in_array($request, $accepted_requests)){
      
                      foreach($accepted_requests as $k=>$v){
      
                          if($request === $v){
      
                                  foreach($utube_urls as $keys => $vals){
      
                                          if($k == $keys){
                                              echo $vals;
                                          }
      
                                  }
      
                          }
      
      
                      }
      
      
          }
      

      ?>

      更新了代码:错过了 $val 上的 's' - 现已修复

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-28
        • 2023-03-18
        • 1970-01-01
        • 1970-01-01
        • 2019-12-25
        • 1970-01-01
        • 2016-04-10
        • 2013-02-24
        相关资源
        最近更新 更多