dongyuzhu2244 2013-07-21 07:34
浏览 41
已采纳

什么是一个好的和简单的PHP框架来创建一个RESTFUL API WebService [关闭]

What is a good and easy PHP framework to create a RESTFUL API WebService? What I want to do, is to create API the performs CRUD operations on the table and aims to make a response as JSON. Not only database operations, but also it may have extra logic to add in the code. Hope the question is more clear now.

  • 写回答

1条回答 默认 最新

  • dpxw17759 2013-07-21 07:40
    关注

    Answer edited based upon updated question

    There is no mature framework that I know of which does a RESTful API with JSON responses out of the box. It is largely up to you as the coder to implement those details.

    I would honestly pick a well documented framework like Codeigniter, and go from there. Your API endpoints will be whatever you define in either /application/config/routes.php, or based on your controller naming convention. Remember that the key to a RESTful webservice is that it's stateless. Your URLs should be something like:

    /products/381
    /users/812
    /tutorials/19
    

    You don't want /tutorials/updatetutorial/19 (that is not restful).

    In strict terms, you will want your controllers to listen for HTTP request methods on certain URLs. For example, say the endpoint for a given product is at

    http://somedomain.com/api/products/381

    You will then want to use HTTP request methods to determine what you are doing to that endpoint:

    POST to that URL means you are updating it/editing it GET to that URL means you are simply requesting it DELETE to that URL means you want to delete the resource

    Don't do things like /products/381/delete or products/381/edit. That is not strictly RESTful. Your URLs should NEVER contain verbs.

    So what you do is simply structure your controller methods to listen for those HTTP request methods, and call CRUD operations from your models accordingly. To respond with JSON, it's quite easy: just take any object or array from a database query, and echo json_encode($data) it.

    Remember to use the php header() function to create headers that indicate the appropriate responses for your API. You will probably want to create a class explicitly for outputting your query results in JSON with the right headers, just so you don't have to repeat yourself a lot.

    But you'll also want to secure your API, which is a whole other topic that is out of the scope of what I can answer here. If you search StackOverflow or google, you'll find a whole bunch of topics about securing an API. Do your research though, it's quite a big subject.


    Well the answer to that I think depends on your understanding of RESTful, and what you ultimately want to use it for. Is it meant to be an API? What kind of webservice is it going to be?

    The short answer is that there isn't really a PHP Framework built explicitly to be a RESTful web service. RESTfulness is all in how you choose to implement it, structure your database, and structure your URLs.

    Personally I like working with Codeigniter, and I have built a RESTful API webservice with it before, but I had to code the API part of it myself - CI didn't have anything explicitly built in.

    I've played around with Epiphany framework before, which is very much API-focused, but it does things a little oddly and I didn't explore it far enough to know.

    But at the end of the day, a RESTful implementation really depends on you as the coder, and what your ultimately goals are. So perhaps if you update your post with some more details, we can better point you in the right direction.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败