When using data interfaces, we usually need to obtain third-party data interfaces or provide third-party data interfaces. these data formats are usually transmitted in XML or JSON format, this article describes how to use PHP to generate XML format data for third-party calls and how to obtain XML data provided by a third-party. ... "> <LINKhref =" http://www.php100.com//statics/style
When using data interfaces, we usually need to obtain third-party data interfaces or provide third-party data interfaces. these data formats are usually transmitted in XML or JSON format, this article describes how to use PHP to generate XML format data for third-party calls and how to obtain XML data provided by a third-party.
Generate XML format data
Assume that the system has a student info table student, which must be provided to a third party for calling. the student name, gender, age, and other information are recorded by id, name, sex, and age.
CREATE TABLE `student` ( `id` int(11) NOT NULL auto_increment, `name` varchar(50) NOT NULL, `sex` varchar(10) NOT NULL, `age` smallint(3) NOT NULL default '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
First, create the createXML. php file, connect to the database, and obtain data.
Include_once ("connect. php "); // connect to the database $ SQL =" select * from student "; $ result = mysql_query ($ SQL) or die (" Invalid query :". mysql_error (); while ($ row = mysql_fetch_array ($ result) {$ arr [] = array ('name' => $ row ['name'], 'Sex '=> $ row ['Sex'], 'age' => $ row ['age']);}
At this time, the data is saved in $ arr. you can use print_r to print the data for testing.
Then, create an xml and circular array to write data to the corresponding xml node.
$ Doc = new DOMDocument ('1. 0 ', 'utf-8'); // declare the version and code $ doc-> formatOutput = true; $ r = $ doc-> createElement ("root "); $ doc-> appendChild ($ r); foreach ($ arr as $ dat) {$ B = $ doc-> createElement ("data "); $ name = $ doc-> createElement ("name"); $ name-> appendChild ($ doc-> createTextNode ($ dat ['name']); $ B-> appendChild ($ name); $ sex = $ doc-> createElement ("sex "); $ sex-> appendChild ($ doc-> createTextNode ($ dat ['sex']); $ B-> appendChild ($ sex ); $ age = $ doc-> createElement ("age"); $ age-> appendChild ($ doc-> createTextNode ($ dat ['age']); $ B-> appendChild ($ age); $ r-> appendChild ($ B);} echo $ doc-> saveXML ();
We call the PHP built-in class DOMDocument to process and generate xml. The final generated xml format
Li Wang Hao
Male
21...
Get XML format data
Now we assume that we want to obtain student information from a third party in XML format. we need to use PHP to parse XML and then display or write the parsed data to the local database. The key step here is to parse XML.
PHP has many ways to parse XML. among them, PHP provides the built-in XMLReader class to browse xml file nodes in sequence. you can imagine that the cursor goes through the entire file node, and capture the required content. XMLReader is efficient, especially for reading very large xml data. compared with other methods, XMLReader consumes very little memory.
Header ("Content-type: text/html; Charset = utf-8"); $ url =" http://www.helloweba.com/demo/importXML/createXML.php "; $ Reader = new XMLReader (); // instantiate XMLReader $ reader-> open ($ url); // get xml $ I = 1; while ($ reader-> read () {if ($ reader-> nodeType = XMLReader: TEXT) {// judge node type $ m = $ I % 3; if ($ m = 1) $ name = $ reader-> value; // read the node value if ($ m = 2) $ sex = $ reader-> value; if ($ m = 0) {$ age = $ reader-> value; $ arr [] = array ('name' => $ name, 'Sex '=> $ sex, 'age' => $ age);} $ I ++;} // print_r ($ arr );
To separate the data name, sex, and age, we use $ I % 3 to determine the modulo, because in the obtained xml, the information under the node data exists in three subnodes.
Finally, you can output the obtained data or execute SQL statements to write the data to the local database. This step is skipped.