通过在CodeIgniter中发送PUT请求来插入数据(Phil Sturgeon Rest Server)

时间:2014-01-06 06:16:28

标签: php codeigniter rest put

我通过API中的REST CLIENT发送的PUT和POST请求之间存在差异。它在CodeIgniter中与Phil Sturgeon的REST服务器一起实现。

function station_put(){

    $data = array(
        'name' => $this->input->post('name'),
        'number' => $this->input->post('number'),
        'longitude' => $this->input->post('longitude'),
        'lat' => $this->input->post('latitude'),
        'typecode' => $this->input->post('typecode'),
        'description' => $this->input->post('description'),
        'height' => $this->input->post('height'),
        'mult' => $this->input->post('mult'),
        'exp' => $this->input->post('exp'),
        'elevation' => $this->input->post('elevation')
    );

    $id_returned = $this->station_model->add_station($data);
    $this->response(array('id'=>$id_returned,'message'=>"Successfully created."),201);


}

此请求成功将数据插入服务器BUT - 除了id之外,它将其余值呈现为NULL。

但如果将函数名称更改为station_post,则会正确插入数据。

有人请指出为什么PUT请求不起作用?我使用的是谷歌浏览器的最新版本。

Btw此API将集成到BackBone处理的应用程序中。我真的需要使用PUT吗?或者在使用post?

时,是否还有另一种使用骨干模型保存功能的解决方法?

2 个答案:

答案 0 :(得分:7)

终于回答了。它不是$this->input->post$this->input->put,而必须是$this->put$this->post,因为数据不是来自表单。

答案 1 :(得分:1)

Codeigniter put_stream也无法获取放置数据,因此我不得不处理php PUT请求,并且发现此功能足以节省某人的时间:

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;
}