PUT和POST查询字符串模式

时间:2015-10-16 18:07:06

标签: php restful-url

我在做自己的restfull api。我已经阅读了很多文章,但之前我更加困惑。 拳头问题:

PUT请求中的

是否正确使用问号(?)ex:

  http://myapi.me/testApi/v5/create/places?address=Five&building=old

或者我必须使用

 http://myapi.me/testApi/v5/create/places/address=Five&building=old

如果第二种方法是正确的,我如何爆炸查询字符串来创建我的插入语句?我的代码适用于(?)。但是curl命令不想输入记录。所以我需要修改脚本以反映(/)而不是(?)的正确方法。

请帮忙......

我的代码:

$body = file_get_contents("php://input");
    $content_type = false;
    if(isset($_SERVER['CONTENT_TYPE'])) {
        $content_type = $_SERVER['CONTENT_TYPE'];
      //  echo $content_type;
    }
    switch($content_type) {
        //other case...
        case "application/x-www-form-urlencoded":
            parse_str($body, $postvars);
            foreach($postvars as $field => $value) {
                $parameters[$field] = $value;

            }
            $this->format = "html";
            break;
        default:
            // we could parse other supported formats here
            break;
    }
    $this->parameters = $parameters;

 private function createRecord(){
        if($this->get_request_method() != "PUT"){
            $this->response('',406);
        }

        $table = strtolower(trim(str_replace("/", "", $_REQUEST['rquest'])));
    /*
        $Q = explode("/", $_SERVER["rquest"]);
        print_r(array_values($Q));
        echo "\n";
    */              
        $uri = $this->parameters;

        if($uri){  
        array_shift($uri);

        foreach( $uri as $k => $v )
        {
        $aKeyPholder[]= $k;         
        $aKey[]= ':'.$k;    

        }
        $aKeyPholder= implode(',', $aKeyPholder);
        $aKey= implode(',', $aKey);

        $insert = "INSERT INTO '$table' ($aKeyPholder)  VALUES ($aKey)";
      // echo "insert=$insert\n";
        $stmt = $this->db->prepare($insert);

        foreach($uri as $k => &$v){
        $stmt->bindParam(':'.$k, $v, PDO::PARAM_STR);

        } 
        //$stmt->execute();
        if ($stmt->execute()) { echo "Record succesfully created!";}

        $this->response($this->json($success),200);
        }
        else{
        $this->response('',204); // If no records "No Content" status
        }
    }

0 个答案:

没有答案