Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write a web service in php? [closed]

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

like image 251
Kourosh Samia Avatar asked Dec 23 '10 20:12

Kourosh Samia


1 Answers

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

Here's a very basic example where you'd take an array of data, then JSON encode that array and returns the 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
}
like image 100
Yahel Avatar answered Oct 05 '22 03:10

Yahel