PHP automatically identifies the types of returned content of RESTful API

Source: Internet
Author: User
This article mainly introduces how PHP can automatically identify the types of returned content of RestfulAPI, and automatically render the content into json, xml, html, serialize, csv, php, and other data formats for output, for more information, see the following question: how does PHP automatically recognize the content of a third-party Restful API and render it into json, xml, html, serialize, csv, php, and other data?

In fact, this is not difficult, because the Rest API is based on the http protocol, as long as we follow the protocol, we can automatically identify the API content, the method is as follows:

1. the API server must return clear http Content-Type header information, for example:

Content-Type: application/json; charset=utf-8Content-Type: application/xml; charset=utf-8Content-Type: text/html; charset=utf-8

2. after receiving the above header information, the PHP client automatically processes it as needed. the reference code is as follows:

 'Xml', 'text/XML' => 'xml', 'application/json' => 'json', 'text/json' => 'json ', 'text/csv' => 'csv', 'application/csv' => 'csv', 'application/vnd. php. serialized '=> 'serialize'); if (strpos ($ contentType,'; ') {list ($ contentType) = explode ('; ', $ contentType );} $ contentType = trim ($ contentType); if (array_key_exists ($ contentType, $ autoDetectFormats) {echo '_'. $ autoDetectFormats [$ contentType] ($ re Else se );} // ++ ++ // common formatting method // ++ ++/ * ** format xml output */function _ xml ($ string) {return $ string? (Array) simplexml_load_string ($ string, 'simplexmlelement', LIBXML_NOCDATA): array () ;}/ *** format csv output */function _ csv ($ string) {$ data = array (); $ rows = explode ("\ n", trim ($ string); $ headings = explode (',', array_shift ($ rows); foreach ($ rows as $ row) {// use substr to remove "$ data_fields = explode ('","', trim (substr ($ row, 1,-1); if (count ($ data_fields) = count ($ headings )) {$ data [] = array_combine ($ headings, $ data_fields) ;}return $ data ;}/ *** format json output */function _ json ($ string) {return json_decode (trim ($ string), true);}/*** deserialization output */function _ serialize ($ string) {return unserialize (trim ($ string);}/*** execute PHP script output */function _ php ($ string) {$ string = trim ($ string ); $ populated = array (); eval ("\ $ populated = \" $ string \ ";"); return $ populated ;}

For more articles about PHP's automatic identification of Restful API return content types, refer to PHP's Chinese website!

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.