YouTip LogoYouTip

Php Restful

-- Learn not just technology, but also dreams! Home HTML JAVASCRIPT CSS VUE REACT PYTHON3 JAVA C C++ C# AI GO SQL LINUX VS CODE BOOTSTRAP GIT Local Bookmark PHP Tutorial PHP Tutorial PHP Introduction PHP Installation PHP Syntax PHP Variables PHP echo/print PHP EOF(heredoc) PHP Data Types PHP Type Comparison PHP Constants PHP Strings PHP Operators PHP If...Else PHP Switch PHP Arrays PHP Array Sorting PHP Superglobal Variables PHP While Loop PHP For Loop PHP Functions PHP Magic Constants PHP Namespaces PHP Object-Oriented PHP Quiz PHP Forms PHP Forms PHP Form Validation PHP Form - Required Fields PHP Form - Validate Email and URL PHP Complete Form Example PHP $_GET Variable PHP $_POST Variable PHP Advanced Tutorial PHP Multidimensional Arrays PHP Date PHP Include PHP Files PHP File Upload PHP Cookie PHP Session PHP E-mail PHP Secure E-mail PHP Error PHP Exception PHP Filters PHP Advanced Filters PHP JSON PHP 7 New Features PHP 7 New Features PHP Database PHP MySQL Introduction PHP MySQL Connect PHP MySQL Create Database PHP MySQL Create Table PHP MySQL Insert Data PHP MySQL Insert Multiple Data PHP MySQL Prepared Statements PHP MySQL Read Data PHP MySQL Where PHP MySQL Order By PHP MySQL Update PHP MySQL Delete PHP ODBC PHP XML XML Expat Parser XML DOM XML SimpleXML PHP and AJAX AJAX Introduction AJAX PHP AJAX Database AJAX XML AJAX Real-time Search AJAX RSS Reader AJAX Poll PHP Reference Manual PHP Array PHP Calendar PHP cURL PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP PDO PHP SimpleXML PHP String PHP XML PHP Zip PHP Timezones PHP Image Processing PHP RESTful PHP PCRE PHP Available Functions PHP Composer PHP Image Processing PHP Regular Expressions (PCRE) PHP RESTful REST (Representational State Transfer) refers to a set of architectural constraints and principles. A Web API that conforms to the REST design style is called a RESTful API. It defines resources from the following three aspects: Intuitive and short resource address: URI, for example: http://example.com/resources/. Resources being transferred: Internet media types accepted and returned by web services, such as: JSON, XML, YAM, etc. Operations on resources: A series of request methods supported by the web service on this resource (such as: POST, GET, PUT, or DELETE). In this tutorial, we will use PHP (without framework) to create a RESTful web service. At the end of the article, you can download the code used in this chapter. Through this tutorial you will learn: Create a RESTful Webservice. Use native PHP without relying on any framework. URI pattern needs to follow REST rules. RESTful service can accept and return formats such as JSON, XML, etc. Respond with corresponding HTTP status codes according to different situations. Demonstrate the use of request headers. Use REST client to test RESTful web service. RESTful Webservice Example The following code is the RESTful service class Site.php: Example 'TaoBao', 2 => 'Google', 3 => 'Tutorial', 4 => 'Baidu', 5 => 'Weibo', 6 => 'Sina' ); public function getAllSite(){ return $this->sites; } public function getSite($id){ $site = array($id => ($this->sites[$id]) ? $this->sites[$id] : $this->sites); return $site; } } ?> RESTful Services URI Mapping RESTful Services URI should be set as an intuitive and short resource address. The .htaccess file of Apache server should be configured with corresponding Rewrite rules. In this example, we will use two URI rules: 1. Get all site list: http://localhost/restexample/site/list/ 2. Use id to get the specified site, the following URI is to get the site with id 3: http://localhost/restexample/site/list/3/ The .htaccess file configuration rules for the project are as follows: # Enable rewrite function Options +FollowSymlinks RewriteEngine on # Rewrite rules RewriteRule ^site/list/$ RestController.php?view=all [nc,qsa] RewriteRule ^site/list/(+)/$ RestController.php?view=single&id=$1 [nc,qsa] RESTful Web Service Controller In the .htaccess file, we set the parameter 'view' to get the corresponding request in the RestController.php file. By getting different parameters of 'view', we can dispatch to different methods. The RestController.php file code is as follows: Example getAllSites(); break; case "single": // Handle REST Url /site/show// $siteRestHandler = new SiteRestHandler(); $siteRestHandler->getSite($_GET); break; case "" : //404 - not found; break; } ?> Simple RESTful Base Class The following provides a base class for RESTful, which is used to handle HTTP status codes for response requests. The SimpleRest.php file code is as follows: Example getHttpStatusMessage($statusCode); header($this->httpVersion. " ". $statusCode ." ". $statusMessage); header("Content-Type:". $contentType); } public function getHttpStatusMessage($statusCode){ $httpStatus = array( 100 => 'Continue', 101 => 'Switching Protocols', 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 306 => '(Unused)', 307 => 'Temporary Redirect', 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Timeout', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large',
← Php Scalar Return TypeHtml5 Syntax β†’