dongyou9721 2013-12-02 04:04
浏览 195
已采纳

如何在PHP中获取PUT / DELETE参数

i am writing a REST webservice and would like to know how can i handle put or delete argument is PHP.

i am taking the inputs as following,

$input = file_get_contents('php://input');
echo $input;

output = imei=1234567890&email=hello%40gmail1.com

how can i access these variables like

echo $_POST['imei'];
echo $_POST['email'];
echo $_GET['imei'];
echo $_GET['email'];

i found there is nothing like $_PUT or $_DELETE in php to handle the input params. what is the way to get this ?

  • 写回答

2条回答 默认 最新

  • dotwc62080 2013-12-02 04:11
    关注

    Can you try this, PHP doesn't have a built-in way to do this, can be read from the incoming stream to PHP, php://input.

     parse_str(file_get_contents("php://input"));
    

    EX:

      if($_SERVER['REQUEST_METHOD'] == 'GET') {
        echo "this is a get request
    ";
        echo $_GET['fruit']." is the fruit
    ";
        echo "I want ".$_GET['quantity']." of them
    
    ";
    } elseif($_SERVER['REQUEST_METHOD'] == 'PUT') {
        echo "this is a put request
    ";
        parse_str(file_get_contents("php://input"),$post_vars);
        echo $post_vars['fruit']." is the fruit
    ";
        echo "I want ".$post_vars['quantity']." of them
    
    ";
    }
    

    Ref: http://www.lornajane.net/posts/2008/accessing-incoming-put-data-from-php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭