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.

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.

For future questions please have one question per entry.

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

That really depends on the web service, what they require. It can be as simple as a short text string, to sending a XML formatted or JSON formatted array. You need to research the relevant web service.

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

Again it depends on the web service. They are not the same. A web service will have documentation on what it expects and how it will respond.

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

Curl is a good method and is usually the method used from within PHP.