douhao7677
2015-12-22 11:15
浏览 31

在PHP中识别传入的http请求

Have a little problem. I have some task, where must process some data in client server system. On client side i can create diferent http request on JS. For example:

$http.post(url, DataObj);
$http.get(url, DataObj);
$http.insert(url, DataObj);
$http.deletet(url, DataObj);
$http.update(url, DataObj);

On the server side on NodeJS I can identify type of request, get request data and do some server logic, after that i can send back some ansver or data object. Node JS example:

var apiRoutes = express.Router();
apiRoutes.post('route', function(req, res) {
someFunction(req.params.data);
res.send(ResponseDataObj);  }
or
apiRoutes.delete('route', function(req, res) { .... }

The problem is that I can not understand how client-side PHP must identify the type of request (post, update, actually), get request data to process it and send the result result data. Can I do all these manipulations in a single script, or must to make a few scripts for each operation with data like a :

getData.php
insertData.php
updateData.php
deleteData.php

If anyone have a similar task please share code examples that I could understand, or reference to examples. Thank you very much.

图片转代码服务由CSDN问答提供 功能建议

有一点问题。 我有一些任务,必须在客户服务器系统中处理一些数据。 客户端 我可以在JS上创建不同的http请求。 例如:

  $ http.post(url,DataObj); 
 $ http.get(url,DataObj);  
 $ http.insert(url,DataObj); 
 $ http.deletet(url,DataObj); 
 $ http.update(url,DataObj); 
   
 
  

在NodeJS的服务器端,我可以识别请求类型,获取请求数据并执行一些服务器逻辑,之后我可以发回一些ansver或数据对象。 Node JS示例:

  var apiRoutes = express.Router(); 
apiRoutes.post('route',function(req,res){
someFunction(req.params.data); 
res.send(ResponseDataObj);}  
or 
apiRoutes.delete('route',function(req,res){....} 
   
 
 

问题是我无法理解客户端是怎样的 PHP必须识别请求的类型(发布,更新,实际),获取请求数据以处理它并发送结果结果数据。 n我可以在一个脚本中完成所有这些操作,或者必须使用以下数据为每个操作制作一些 scripts:

  getData.php 
insertData.php 
updateData  .php 
deleteData.php 
   
 
 

如果有人有类似的任务,请分享我能理解的代码示例,或参考示例。 非常感谢你。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanpasi6287 2015-12-22 11:29
    已采纳

    You can fetch the request method in PHP using $_SERVER['REQUEST_METHOD']. However for a more structured approach (as you're doing with Express) I would advice using a lightweight framework to do the heavy lifting for you. These come to mind:

    Slim Framework

    Lumen

    Silex

    点赞 打赏 评论

相关推荐 更多相似问题