REST API with PHP – How to do it?

What is meant by REST API?
A RESTful API — also referred to as a RESTful web service — is based on representational state transfer (REST) technology, an architectural style and approach to communications often used in web services development.

Building RESTful web services, like other programming skills is part art, part science. As the Internet industry progresses, creating a REST API becomes more concrete with emerging best practices. As RESTful web services don’t follow a prescribed standard except for HTTP, it’s important to build your RESTful API in accordance with industry best practices to ease development and increase client adoption.

Presently, there aren’t a lot of REST API guides to help the lonely developer. RestApiTutorial.com is dedicated to tracking REST API best practices and making resources available to enable quick reference and self education for the development crafts-person. We’ll discuss both the art and science of creating REST Web services.

—Todd Fredrich, The REST API Tutor

 

REST API with PHP
So I got this one project, that need me to read from one database, push this data into MySQL server, but using REST API and PHP need to process this REST API. The data from teh database sent using JSON format in POST method.
Here, I show the PHP code:

<?php
$content = file_get_contents(“php://input”); // get the JSON Content
//You only need this one line actually.

 

//From here onwards, you may process the data – putting into database, or write into text file

var_dump(json_decode($content)); // if you want to show the data

$file = fopen(“test.txt”,”w”); //in this case, I put the data into a text file.
echo fwrite($file,$content);
fclose($file);
?>

— adam —

adam

Software engineer by passion

Leave a Reply

Your email address will not be published. Required fields are marked *