首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用REST从表中获取数据

使用REST从表中获取数据
EN

Stack Overflow用户
提问于 2011-10-21 08:39:04
回答 3查看 3.6K关注 0票数 0

我正在使用Slim框架设计REST。我正在使用数据库mySql。我正在用php设计这个API。

我正试着从我的桌子上提取关于学生的数据。

我试着这样做:-

代码语言:javascript
复制
<?php
header('Content-type: application/json');
// Include the Slim library
require 'Slim/Slim.php';
// Instantiate the Slim class
$app = new Slim();
// Create a GET-based route
$app->get('/hello/:name', 'hello');

  function hello($name)
  {
     // here is code to access detail of $name 
     echo $name
     // how can i get detail if i have value of name=:kuntal  not name=kuntal
  }

  // Ready the routes and run the application
   $app->run();
 ?>

我正在使用这个url:- 192.168.1.101/hello/:kuntal尝试这个函数。

我需要得到名称的值,如,kuntal,,但在函数中,我得到的值为:kuntal,,所以请告诉我,如何在名称之前删除这个:(冒号)。

是另一种方法。

如果您知道瘦框架来制作REST,请给我您的建议。提前谢谢你。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-10-21 15:15:36

试试这个url: 192.168.1.101/hello/kuntal,我认为这对您的代码一定有用。

票数 0
EN

Stack Overflow用户

发布于 2012-01-31 09:20:57

斯利姆是一个非常好的框架。在我开始使用它之前,我对框架、REST、理解HTTP没有任何经验.我仍然是个菜鸟,但斯利姆让这很有趣。

答1:

代码语言:javascript
复制
$app->get('/hello/:name', function($name) use ($app) {
    // Your mySQL code here
    // Process that information into output JSON?
    // echo json_encode($array);
});

回答2:

我可以补充一下,您可以根据数据库的需要签出Idiorm/巴黎吗?在与Slim相同的哲学中,越少越好。这就是巴黎的代码。

代码语言:javascript
复制
class Friend extends Model {}

$app->get('/hello/:name', function($name) use ($app) {

    $friend = Model::factory('Friend')->find_many($name);  // Paris: all rows with name
    $friendAry = $friend->as_array('id', 'name');

    $response = $app->response();    // Slim Response object at work
    $response['Content-Type'] = 'application/json';

    echo json_encode($friendAry);    // Output
});

虽然,问题(也许是一个更了解休息的人可以回答)。这真的很好吗?从我对休息的理解来看,我们想把人们引向资源。我猜是名词。我不知道在REST上下文中Hello到底意味着什么。为什么不让资源用户或朋友使用ID作为段塞呢?

代码语言:javascript
复制
$app->get('/friend/:id', function($id) use ($app) {
    // Returns the friend with unique id
    $friend = Model::factory('Friend')->find_one($id);  // Name is already part of obj
    $friendAry = $friend->as_array('id', 'name');
    echo json_encode($friendAry);
}

然后,您可以处理该信息,并将其打包为hello问候语,无论客户机期望什么。您可以向这样的参数传递额外的信息,如name。

http://search.twitter.com/search?q=potato&count=10

希望这能有所帮助。很酷的东西。有人给我反馈,让我知道我的想法是否正确。我也还在学习。

票数 0
EN

Stack Overflow用户

发布于 2019-04-15 10:51:55

代码语言:javascript
复制
enter code here
    <?php
    error_reporting('E_NOTICE ^ E_ALL');
    include_once 'api_function.php';
    $con = new DB_con();
    function getCurrentURL()
    {
        $currentURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
        $currentURL .= $_SERVER["SERVER_NAME"];

        if($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443")
        {
            $currentURL .= ":".$_SERVER["SERVER_PORT"];
        } 

            $currentURL .= $_SERVER["REQUEST_URI"];
        return $currentURL;
    }

    if($_POST['action']!="")
    {
    $action = $_POST['action'];

    }
    else
    {
    header("Content-type: application/json; charset=iso-8859-1");
    $inputdata = file_get_contents('php://input');
    $data = json_decode($inputdata, TRUE);
    $action = $data['action'];
    }
    /**********************select Detail API Starts here**********************/
    if($action == 'select')
    {
        $insert = $con->select("table_name",$data);

         $msg = array("result" => $insert);

             header('content-type: application/json');
             echo json_encode($msg,true);
    }
    /**********************delete Detail API Ends here************************/
   /*2nd file api_function.php*/
    error_reporting('E_NOTICE ^ E_ALL');
    define('DB_SERVER', 'localhost');
    define('DB_USER', 'username');
    define('DB_PASS', 'password');
    define('DB_NAME', 'db_name');
    class DB_con {
        protected $conn;
        function __construct() {
          $this->conn = mysqli_connec`enter code here`t(DB_SERVER, DB_USER, DB_PASS, DB_NAME) or die(mysqli_connection_error($this->conn));
            //mysql_select_db(DB_NAME, $conn);
        }
    /**********************select API Ends here**********************/
    public function select($ssTableName, $asFields) {
            if (strcmp($asFields['action'], "select") == 0) {


                if ($asFields['id'] != '') {
                $query_checklogin = mysqli_query($this->conn, "delete from  $ssTableName  where id='".$asFields['id']."'")or die(mysqli_error($this->conn));
               if ($query_checklogin != '') {
                     $msg = array('msg' => 'succesfully', 'statuscode' => '201');
                    return $msg;

                        }
                } else {
                    $deta = array();
                    $select = mysqli_query($this->conn, "SELECT * from $ssTableName ")or die(mysqli_error($this->conn)); 
                    while($row = mysqli_fetch_assoc($select)){
                        array_push($deta,$row);

                    }
                     $msg = array('msg' => 'Record does not exist', 'statuscode' => '202', 'detail'=>$deta);

                     return $msg;
                }
            } else {
                $msg = array('msg' => 'Something Error','statuscode' => '203');
                return $msg;
            }
        }
    /********************** API Ends here**********************/
    }
    ?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7846968

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档