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?
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With