dryl34156 2016-09-26 21:11
浏览 93
已采纳

Web服务如何处理请求和响应,以及PHP如何正确访问它?

I'm having troubles to figure out how web services handle requests and send responses, and how my site/module will handle it. So these are my questions and bellow a little about why i am asking this:

What is the right way to send a request to web service?

How a web-service will usually answer a site requesting?

How a PHP site can send a request to a web-service? Is cURL the right way?

I'm a student who are learning PHP and a lot of other things, and my job now is create a Joomla Module to show information from a web service (probably created in java or something, probably created by me, when i learn java...).

I know i will use http requests to talk with the web service, but i worry im using it wrong (making a request to the url, like the browser).

So, i did a little example site and a example API. The api uses Slim microframework to create routes, access my database and return the content in json (what means: if i access 'api.com/api/something' in my browser i see a plain white page with a lot of json). I pretend this is my web service.

The example site send a request to the API with cURL, reads the content and decode the json and do things. It seems forced to me.

I have not much support to understand web services and if i am in the right way (or far from this).

I would appreciate your help.

  • 写回答

2条回答 默认 最新

  • douchengjue9892 2016-09-26 21:39
    关注

    You are on the right track. A web service is simply a processing or storage facility intended to be accessed by some other program just like a database or fileserver service.

    What is the right way to send a request to a web service

    It depends. The most common implementations use SOAP or REST which define additional semantics on top of the HTTP protocol. SOAP uses a single URL as a gateway to the service and more specific selection of the functionality and the relevant data is embedded within an XML payload presented via POST. HTTP is merely a bearer for the message exchange. In REST, the HTTP part is integrated into the semantics of the transaction.

    • The URL identifies the location of the data (or processing function)
    • The payload contains only data, usually presented as POSTed JSON or XML,
    • further REST uses the HTTP verb (GET, POST, PUT, DELETE) to indicate the requested action
    • the HTTP headers are used to convey transaction meta-data.

    How a web service will usually answer a request

    I'm not sure what you are asking here. It should report back on the state of the request, any relevant error messages and possibly some data.

    The speciifics would be unique to the API and documented.

    Is cURL the right way?

    While it is possible to do a lot with the HTTP wrappers functionality in PHP, libcurl offers an lot more flexibility. So, yes this it would be hard to implement a REST client without using cURL, OTOH a SOAP client is (usually) less complex at the HTTP tier but life is a lot simpler if you use a SOAP library/framework to abstract the more complex protocol.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛