但是如果有一個PUT,PATCH,或任何請求類型,其他 比POST?
好吧,既然你是一個設計的API,然後你是誰決定是否它僅接受POST
,PUT
,POST
+ PUT
或請求頭的任何其他組合的一個。
該API不應被設計爲「接受並嘗試處理」第三方應用程序提交給您的API的所有內容。這是應用程序的工作(我的意思是,連接到API的應用程序)以這種方式準備請求,API接受它。請注意,啓用多個請求方法(特別是那些必須以不同方式處理的請求方法)有多種處理請求的方式(例如安全性,類型等)。 這基本上意味着要麼巧妙地設計一個請求處理過程,要麼會遇到不同請求類型調用的API方法的問題,這會很麻煩。
如果您需要獲取請求的原始內容 - @Adil Abbasi似乎在正確的軌道上(就解析php://input
而言)。 但請注意,php://input
不適用於enctype =「multipart/form-data」as described in the docs。
<?php
$input = file_get_contents('php://input');
// assuming it's JSON you allow - convert json to array of params
$requestParams = json_decode($input, true);
if ($requestParams === FALSE) {
// not proper JSON received - set response headers properly
header("HTTP/1.1 400 Bad Request");
// respond with error
die("Bad Request");
}
// proceed with API call - JSON parsed correctly
如果你需要使用的enctype = 「的multipart/form-data的」 - 讀I/O Streams docs約STDIN
,並嘗試這樣的:
<?php
$bytesToRead = 4096000;
$input = fread(STDIN, $bytesToRead); // reads 4096K bytes from STDIN
if ($input === FALSE) {
// handle "failed to read STDIN"
}
// assuming it's json you accept:
$requestParams = json_decode($input , true);
if ($requestParams === FALSE) {
// not proper JSON received - set response headers properly
header("HTTP/1.1 400 Bad Request");
// respond with error
die("Bad Request");
}
我測試過和'PHP: // input'可與PUT請求,「根據文檔它不是在PHP可用://輸入」沒有看到任何東西contradictious在文檔 – cske
- 我看不出它說,在文檔。 – Quentin
檢查這個鏈接,如果它是有幫助的,[鏈接](http://stackoverflow.com/questions/9464935/php-multipart-form-data-put-request)。 –