Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/252.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过在CodeIgniter(Phil Sturgeon Rest服务器)中发送PUT请求来插入数据_Php_Codeigniter_Rest_Put - Fatal编程技术网

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

Php 通过在CodeIgniter(Phil Sturgeon Rest服务器)中发送PUT请求来插入数据,php,codeigniter,rest,put,Php,Codeigniter,Rest,Put,在API中通过REST客户端发送的PUT和POST请求之间存在差异。它是通过Phil Sturgeon的REST服务器在CodeIgniter中实现的 function station_put(){ $data = array( 'name' => $this->input->post('name'), 'number' => $this->input->post('number'), 'longitu

在API中通过REST客户端发送的PUT和POST请求之间存在差异。它是通过Phil Sturgeon的REST服务器在CodeIgniter中实现的

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


}
此请求成功地将数据插入服务器,但-它会将除id之外的其余值呈现为NULL

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

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


顺便说一句,此API将集成到主干处理的应用程序中。我真的需要使用PUT吗?或者在使用post时,主干网中的模型保存功能是否还有其他解决方法?

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

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

对不起,恐怕我犯了一个致命的错误。如果要调用put请求而不是POST,则数据数组必须由$this->input->put()组成。仍然不工作。可能不支持PUT。“你觉得人们怎么样?”最后回答。不是$this->input->post或$this->input->put,而是$this->put或$this->post,因为数据不是来自表单。