首页 文章

在PHP中检测请求类型(GET,POST,PUT或DELETE)

提问于
浏览
807

如何在PHP中检测使用了哪种请求类型(GET,POST,PUT或DELETE)?

11 回答

  • 6

    当请求方法时,它将具有 array . 所以只需查看 count() 即可 .

    $m=['GET'=>$_GET,'POST'=>$_POST];
    foreach($m as$k=>$v){
        echo count($v)?
        $k.' was requested.':null;
    }
    

    3v4l.org/U51TE

  • 18

    通过使用

    $_SERVER['REQUEST_METHOD']
    

    示例

    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
         // The request is using the POST method
    }
    

    有关详细信息,请参阅documentation for the $_SERVER variable .

  • 8

    由于这是关于REST,仅从服务器获取请求方法是不够的 . 您还需要接收RESTful路由参数 . 分离RESTful参数和GET / POST / PUT参数的原因是资源需要有自己唯一的URL用于标识 .

    这是使用Slim在PHP中实现RESTful路由的一种方法:

    https://github.com/codeguy/Slim

    $app = new \Slim\Slim();
    $app->get('/hello/:name', function ($name) {
      echo "Hello, $name";
    });
    $app->run();
    

    并相应地配置服务器 .

    这是使用AltoRouter的另一个例子:

    https://github.com/dannyvankooten/AltoRouter

    $router = new AltoRouter();
    $router->setBasePath('/AltoRouter'); // (optional) the subdir AltoRouter lives in
    
    // mapping routes
    $router->map('GET|POST','/', 'home#index', 'home');
    $router->map('GET','/users', array('c' => 'UserController', 'a' => 'ListAction'));
    $router->map('GET','/users/[i:id]', 'users#show', 'users_show');
    $router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
    
  • 210
    $request = new \Zend\Http\PhpEnvironment\Request();
    $httpMethod = $request->getMethod();
    

    通过这种方式,您还可以在zend框架2中实现 . 谢谢 .

  • -3

    可以使用以下代码片段来检测HTTP方法或所谓的 REQUEST METHOD .

    $method = $_SERVER['REQUEST_METHOD']
    if ($method == 'POST') {
        // Method is POST
    } elseif ($method == 'GET') {
        // Method is GET
    } elseif ($method == 'PUT') {
        // Method is PUT
    } elseif ($method == 'DELETE') {
        // Method is DELETE
    } else {
        // Method unknown
    }
    

    如果你喜欢 if-else 语句,你也可以使用 switch 来做 .

    如果html表单中需要 GETPOST 以外的方法,则通常使用表单中的隐藏字段来解决此问题 .

    <!-- DELETE method -->
    <form action='' method='POST'>
        <input type="hidden" name'_METHOD' value="DELETE">
    </form>
    
    <!-- PUT method -->
    <form action='' method='POST'>
        <input type="hidden" name'_METHOD' value="PUT">
    </form>
    

    有关HTTP方法的更多信息,我想参考以下StackOverflow问题:

    HTTP protocol's PUT and DELETE and their usage in PHP

  • 9

    PHP中的REST可以非常简单地完成 . 创建http://example.com/test.php(概述如下) . 将此用于REST调用,例如http://example.com/test.php/testing/123/hello . 这适用于Apache和Lighttpd开箱即用,不需要重写规则 .

    <?php
    $method = $_SERVER['REQUEST_METHOD'];
    $request = explode("/", substr(@$_SERVER['PATH_INFO'], 1));
    
    switch ($method) {
      case 'PUT':
        do_something_with_put($request);  
        break;
      case 'POST':
        do_something_with_post($request);  
        break;
      case 'GET':
        do_something_with_get($request);  
        break;
      default:
        handle_error($request);  
        break;
    }
    
  • 1144

    非常简单只需使用$ _SERVER ['REQUEST_METHOD'];

    Example:

    <?php
    $method = $_SERVER['REQUEST_METHOD'];
    switch ($method) {
      case 'GET':
        //Here Handle GET Request 
        break;
      case 'POST':
        //Here Handle POST Request 
        break;
      case 'DELETE':
        //Here Handle DELETE Request 
        break;
      case 'PUT':
        //Here Handle PUT Request 
        break;
    }
    ?>
    
  • 5

    您可以使用 getenv 函数,而不必使用 $_SERVER 变量:

    getenv('REQUEST_METHOD');
    

    更多信息:

    http://php.net/manual/en/function.getenv.php

  • 1

    我们还可以使用input_filter来检测请求方法,同时还通过输入卫生提供安全性 .

    $request = filter_input(INPUT_SERVER, 'REQUEST_METHOD', FILTER_SANITIZE_ENCODED);
    
  • 0

    你可以得到任何查询字符串数据,即 www.example.com?id=2&name=r

    您必须使用 $_GET['id']$_REQUEST['id'] 获取数据 .

    发布数据意味着形式 <form action='' method='POST'> 您必须使用 $_POST$_REQUEST .

  • 7

    在核心php中你可以这样做:

    <?php
    
    $method = $_SERVER['REQUEST_METHOD'];
    
    switch ($method) {
      case 'GET':
        //Here Handle GET Request
        echo 'You are using '.$method.' Method';
        break;
      case 'POST':
        //Here Handle POST Request
        echo 'You are using '.$method.' Method';
        break;
      case 'PUT':
        //Here Handle PUT Request
        echo 'You are using '.$method.' Method';
        break;
      case 'PATCH':
        //Here Handle PATCH Request
        echo 'You are using '.$method.' Method';
        break;
      case 'DELETE':
        //Here Handle DELETE Request
        echo 'You are using '.$method.' Method';
        break;
      case 'COPY':
          //Here Handle COPY Request
          echo 'You are using '.$method.' Method';
          break;
    
      case 'OPTIONS':
          //Here Handle OPTIONS Request
          echo 'You are using '.$method.' Method';
          break;
      case 'LINK':
          //Here Handle LINK Request
          echo 'You are using '.$method.' Method';
          break;
      case 'UNLINK':
          //Here Handle UNLINK Request
          echo 'You are using '.$method.' Method';
          break;
      case 'PURGE':
          //Here Handle PURGE Request
          echo 'You are using '.$method.' Method';
          break;
      case 'LOCK':
          //Here Handle LOCK Request
          echo 'You are using '.$method.' Method';
          break;
      case 'UNLOCK':
          //Here Handle UNLOCK Request
          echo 'You are using '.$method.' Method';
          break;
      case 'PROPFIND':
          //Here Handle PROPFIND Request
          echo 'You are using '.$method.' Method';
          break;
      case 'VIEW':
          //Here Handle VIEW Request
          echo 'You are using '.$method.' Method';
          break;
      Default:
        echo 'You are using '.$method.' Method';
      break;
    }
    
    
    ?>
    

相关问题