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 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3