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条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?