PHP about API Interface instance sharing

Source: Internet
Author: User
Tags delete cache
An API is a calling interface that the operating system leaves to an application, which enables the operating system to execute commands (actions) of the application by invoking the operating system's APIs. This article is mainly to share with you PHP API interface examples, I hope to help everyone.

PHP generates JSON data

Json_encode ($value) method

(Response.php and testapi.php)

Communication standard Format:

Code Status Code message hint information data returned

How JSON encapsulates communication data methods

Response class

    1. <?phpclassresponse{/*** output Communication data as JSON * @param integer $code Status code * @param string $message message * @param array $data data * Return string*/publicstaticfunction JSON ($code, $message = ", $data =array ()) {if (!is_numeric ($code)) {return ';} $result =array (' Code ' = $code, ' message ' = $message, ' data ' = $data); echo Json_encode ($result); exit;}}

Instance uses

    1. <?phprequire_once ('./apitest.php '); $arr =array (' id ' =>1, ' name ' = ' Huwei ',); Response::json (200, ' data return success ', $arr);

PHP generates XML data

1. Assembling strings

2. Using the System class

DomDocument

XMLWriter

SimpleXML

Encapsulating XML Communication Interfaces

Encapsulation method

/*** output Communication data as XML * @param integer $code Status code * @param string $message message * @param array $data data *return string*/ Publicstaticfunction XML ($code, $message = ", $data =array ()) {if (!is_numeric ($code)) {return ';} $result =array (' Code ' = $code, ' message ' = $message, ' data ' = $data); Header ("Content-type:text/xml");// Convert header information to XML format $xml= "<?xml version= ' 1.0 ' encoding= ' UTF-8 '? >\n"; $xml. = "<root>\n"; $xml. =self::xmltoencode ($result); $xml. = "</root>"; return $xml;} Publicstaticfunction Xmltoencode ($data) {$xml = $attr = ""; foreach ($data as $k + = $v) {if (Is_numeric ($k)) {$attr = "id= ' { $k} ' "; $k =" Item ";} $xml. = "<{$k} {$attr}>"; $xml. =is_array ($v)? Self::xmltoencode ($v): $v; $xml. = "</{$k}>";} return $xml;} Implementation: <?phprequire_once ('./apitest.php '); $arr =array (' id ' =>1, ' name ' = ' Huwei ', ' type ' =>array (All-in-all));// Echo Response::json (200, ' data return succeeded ', $arr); Echo response::xml (200, ' data return succeeded ', $arr); Integrated communication method Encapsulation encapsulation Method Const json= "JSON";/*** Output communication data in an integrated manner * @param integer $code Status code * @param string $message message * @param array $Data * @param string $type type *return string*/publicstaticfunction show ($code, $message = ", $data =array (), $type ==self: : JSON) {if (!is_numeric ($code)) {return ';} $result =array (' Code ' = $code, ' message ' = + $message, ' data ' = $data,); if ($type = = ' json ') {Returnself::json ($ Code, $message, $data);} ElseIf ($type = = ' array ') {var_dump ($result);} ElseIf ($type = = ' xml ') {Returnself::xml ($code, $message, $data);} Else{//todo}} Call Mode: <?phprequire_once ('./apitest.php '); $arr =array (' id ' =>1, ' name ' = ' Huwei ', ' type ' = //echo Response::json (200, ' data return success ', $arr);//echo response::xml (200, ' data return success ', $arr); Echo Response:: Show (200, ' data returned successfully ', $arr, ' array ');

Caching technology:

1. Static caching

Static files stored on disk, with PHP-generated data in a static cache file

PHP operation Cache (file.php)

Generate cache, get cache, delete cache

Encapsulation class

<?phpclassfile{private $_dir;//file path const ext= '. txt ';//file Suffix publicfunction __construct () {$this->_dir=dirname ( __file__). ' \files\/';//Gets the project sibling directory}publicfunction cachedata ($key, $value = ', $path = ') {$filename = $this->_dir. $path. $ Key.self::ext;if ($value!== ") {//Writes value value to cache if (Is_null ($value)) {Return@unlink ($filename);} $dir =dirname ($filename), if (!is_dir ($dir)) {mkdir ($dir, 0777);} Return file_put_contents ($filename, Json_encode ($value));//Joschengong Returns the number of bytes, otherwise false}if (!is_file ($filename)) {Returnfalse ;} Else{return Json_decode (file_get_contents ($filename), True);}}} Call class <?php//require_once ('./apitest.php '); require_once ('./file.php '); $arr =array (' id ' =>1, ' name ' = ' Huwei ') , ' type ' =>array (//echo) Response::json (200, ' data return succeeded ', $arr);//echo response::xml (200, ' data returned successfully ', $arr);// Echo response::show (200, ' data return succeeded ', $arr, ' array '); $file =newfile ();//echo $file->cachedata (' Index_cache ', $arr); Write Operation//var_dump ($file->cachedata (' Index_cache ')); Read operation Echo $file->cachedata (' Index_cache ', null);//delete operation 2.MemCache,redis 

Setting up cache operations

Get Cache operation

Delete Cache operation

Both 1.Memcache and Redis are used to manage data.

2. All of their data is stored in memory.

3.Redis can periodically back up data to disk (persistent)

4.Memchache is just a simple key/value cache

5.Redis not only supports simple k/v types of data, but also provides storage of data structures such as list, set, hash, etc.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.