我将使用codeigniter创建restful服务器。 我确实从github下载了Rest_Controller.php和Format.php并将它们放在库文件夹中。 顺便说一下,所有请求都没有正常工作,发布,删除等。 我使用了邮差工具。输出总是
{"":false}
帮帮我。 enter image description here
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require(APPPATH.'/libraries/Rest_controller.php');
class Api extends REST_Controller{
public function __construct()
{
parent::__construct();
$this->load->database();
$this->load->model('book_model');
}
function put_info()
{
if($_SERVER['REQUEST_METHOD'] == 'PUT'){
echo "this is put request\n";
var_dump($this->input->input_stream());
}
else if($_SERVER['REQUEST_METHOD'] == 'POST')
echo "this is post request\n";
else if($_SERVER['REQUEST_METHOD'] == 'DELETE')
echo "this is delete request\n";
return;
}
}
?>
答案 0 :(得分:0)
答案 1 :(得分:0)
可以将以下方法添加到助手中,从那里您可以在任何控制器中解析放置请求:
function parsePutRequest()
{
// Fetch content and determine boundary
$raw_data = file_get_contents('php://input');
$boundary = substr($raw_data, 0, strpos($raw_data, "\r\n"));
// Fetch each part
$parts = array_slice(explode($boundary, $raw_data), 1);
$data = array();
foreach ($parts as $part) {
// If this is the last part, break
if ($part == "--\r\n") break;
// Separate content from headers
$part = ltrim($part, "\r\n");
list($raw_headers, $body) = explode("\r\n\r\n", $part, 2);
// Parse the headers list
$raw_headers = explode("\r\n", $raw_headers);
$headers = array();
foreach ($raw_headers as $header) {
list($name, $value) = explode(':', $header);
$headers[strtolower($name)] = ltrim($value, ' ');
}
// Parse the Content-Disposition to get the field name, etc.
if (isset($headers['content-disposition'])) {
$filename = null;
preg_match(
'/^(.+); *name="([^"]+)"(; *filename="([^"]+)")?/',
$headers['content-disposition'],
$matches
);
list(, $type, $name) = $matches;
isset($matches[4]) and $filename = $matches[4];
// handle your fields here
switch ($name) {
// this is a file upload
case 'userfile':
file_put_contents($filename, $body);
break;
// default for all other files is to populate $data
default:
$data[$name] = substr($body, 0, strlen($body) - 2);
break;
}
}
}
return $data;
}