简体   繁体   English

从HTTP PUT读取数据

[英]Read data from HTTP PUT

I am currently implementing a RESTful web service which talks XML using CodeIgniter and REST by Phil Sturgeon . 我目前正在实现一个RESTful Web服务,该服务使用CodeIgniterPhil Sturgeon的 REST来讨论XML。 I am now stuck at how to read XML from HTTP PUT. 我现在坚持如何从HTTP PUT读取XML。 This is what I did. 这就是我做的。

At the client side: 在客户端:

$(function(){
    // Bind a click event to the 'ajax' object id
    $("#new_user").click(function(evt){
        // JavaScript needs totake over. So stop the browser from redirecting the page
        evt.preventDefault();
        var str = '<?xml version="1.0" encoding="UTF-8"?><xml><name>'+$("#txtname").val()+'</name><email>'+$("#txtemail").val()+'</email></xml>';

        // Ajax request to get the data
        $.ajax({
            // URL from the link that was clicked on
            url: $(this).attr("href"),
                        type: "put",
                        contentType: "application/xml",
                        processData: false,
                        data: str,
            success: function(data, textStatus, jqXHR){
                //alert('Successful AJAX request!');
                                   //var items = parseXml(data);
                                   //printXml(items);
            },
            // Failed to load request. This could be caused by any number of problems like server issues, bad links, etc.
            error: function(jqXHR, textStatus, errorThrown){
                alert('Oh no! A problem with the Ajax request!');
            }
        });
    });
});

At the server side: 在服务器端:

public function users_put(){
    $input = file_get_contents('php://input');
    print_r($input);
}

It prints out nothing. 它什么都没打印出来。 The above JavaScript code and function works well in HTTP POST. 上面的JavaScript代码和函数在HTTP POST中运行良好。

The manual has a good reference for that: http://php.net/manual/en/features.file-upload.put-method.php 该手册有一个很好的参考: http//php.net/manual/en/features.file-upload.put-method.php

You cannot handle PUT requests without altering the HTTP daemon's setup. 如果不更改HTTP守护程序的设置,则无法处理PUT请求。


If you're using Apache and have access to mod_rewrite, make a .htaccess file in the root folder that you PUT to with something like: 如果您正在使用Apache并且可以访问mod_rewrite,请在您输入的根文件夹中创建一个.htaccess文件,例如:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ handler.php?uri=$1 [L,QSA]

But the details depend on what HTTP daemon (Apache, IIS, lighttpd, etc) and which PHP framework you use. 但细节取决于HTTP守护程序(Apache,IIS,lighttpd等)以及您使用的PHP框架。

Use POST. 使用POST。 Your application will have to determine if the request is a 'PUT' or not. 您的申请必须确定请求是否为'PUT'。 If you specify the id of the object that you are modifying then you can assume it is a 'PUT' request. 如果指定要修改的对象的id,则可以假定它是“PUT”请求。 I'm not sure how CodeIgniter handles this, but I do know that Zend Framework automatically routes to the putAction when the id is specified. 我不确定CodeIgniter如何处理这个,但我知道当指定id时,Zend Framework会自动路由到putAction。 (eg /account/5) (例如/ account / 5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM