阅读从HTTP PUT数据 [英] Read data from HTTP PUT
问题描述
我目前正在实施利用其中谈到XML RESTful Web服务 codeIgniter 和REST由菲尔鲟鱼。我现在停留在如何读取HTTP PUT XML。这是我做的。
I am currently implementing a RESTful web service which talks XML using CodeIgniter and REST by Phil Sturgeon. I am now stuck at how to read XML from HTTP PUT. This is what I did.
在客户端:
$(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);
}
它打印出什么。在HTTP POST以上的JavaScript code和功能效果很好。
It prints out nothing. The above JavaScript code and function works well in HTTP POST.
推荐答案
本手册具有一个很好的参考:的 http://php.net/manual/en/features.file-upload.put-method.php
The manual has a good reference for that: http://php.net/manual/en/features.file-upload.put-method.php
您不能处理PUT请求,而不改变HTTP守护进程的设置。
You cannot handle PUT requests without altering the HTTP daemon's setup.
如果你使用Apache,并有机会获得mod_rewrite的,请在根文件夹中的.htaccess文件,你放的东西,如:
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:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ handler.php?uri=$1 [L,QSA]
但细节取决于什么样的HTTP守护进程(阿帕奇,IIS,lighttpd的,等等),你使用的PHP框架。
But the details depend on what HTTP daemon (Apache, IIS, lighttpd, etc) and which PHP framework you use.
这篇关于阅读从HTTP PUT数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!