How to write a web service in php?

I am trying to write a simple web service for a function that I wrote in php that I am going to provide for another server. I never did this and have no idea where and how to start it. I tried some sites, but got lost in the middle. Does anyone know a simple document, template, website, for example ... so that I can get started quickly?

+6
php web-services
source share
6 answers

You can create a very simple JSON / JSONP API without libraries.

Here is a very simple example, when you take an array of data, then JSON encodes that array and returns JSON or JSONP depending on the request:

$data = array(); //....do something to prepare your data.... if(isset($_GET['callback'])) { $callback = preg_replace("/[^A-Za-z0-9\.]/", '', $_GET['callback']); header("Content-type: application/javascript"); echo $callback . "(" . json_encode($data) . ");"; } else{ header("Content-type: application/json"); echo json_encode($data); //JSON } 
+2
source share

This is a great tutorial when I start writing my own web service. David Walsh FTW!

It uses PHP / MySQL with outputs located in JSON and / or XML

+2
source share

What resources did you try to follow? There's a good article on Web Services Development Using PHP at O'Reilly PHP Devcenter, which is fairly comprehensive and easy to use.

+2
source share

So essentially a REST service?

Basically, you need a script that sits on the server and waits for something to call it, with or without input, depending on whether you need to send it information for processing or if it simply returns information without filters or whatever something else. Processing the information looks the same as processing the form from the local page, since you will probably send requests to GET or POST using cURL, although you can also do something like file_get_contents (" http://www.server.com/ RESTscript.php? Id = 23 ") when using $ _GET.

When a request arrives, the script passes the information sent and processes it and returns the information, possibly in JSON format with json_encode () / json_decode () or the XML format that the script that called the REST service will have to do everything you need.

So, for a small, quick, dirty, and static example:

 //You would use whatever scripting to build the needed array //Also, note that the only output on the script should be what is being returned to the caller $requested = array('id' => 23, 'name' => 'John Smith', 'info' => 'abcdefghijklmnopqrstuvwxyz'); echo json_encode($requested); 

and the calling script will do something like this:

 $request = json_decode(file_get_contents("http://www.server.com/REST.php?optional=value")); 

which would make $ request an array the same as the requested $, or it could make it stdobj, not sure to forget that. In any case, it’s easy to work. This is all that is required, as a simple and useless web service that you can get.

+2
source share

To let you know, this is exactly the same as any other web application. No difference. Processes> input and print result. - Col. Shrapnel 5 seconds ago

Input 1-Accept Input 2-process Result 3-return

1- Accept input. your code is largely regarded as a form and the information is passed to it to work with a web request

2 Process Input - playback with input, however you need to come up with a result to return

3- Return result - return results similar to how you will output the html page from php, although its format is expected by the calling system (html, json, etc.)

+1
source share

There is a large number of PEARs in this , one of them may suit your needs.

0
source share

All Articles