When doing data interfaces, we typically get Third-party data interfaces or provide data interfaces to third parties, which are typically transmitted in XML or JSON format, and this article describes how to use PHP to generate XML format data for Third-party calls and how to obtain XML data from third parties.
Generate XML Format data
We hypothesized that there is a student information table student in the system, need to provide to the third party call, and have id,name,sex,age record the student's name, gender, age and so on separately.
CREATE TABLE ' student ' (
' id ' int () NOT NULL auto_increment,
' name ' varchar ' is not NULL,
' sex ' varchar ' NOT NULL, ' age
' smallint (3) NOT null default ' 0 ',
PRIMARY KEY (' id ')
engine=myisam DE FAULT Charset=utf8;
First, establish the createxml.php file, connect the database first, obtain the data.
include_once ("connect.php");//Connection 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 ']
);
}
This time, the data is stored in the $arr, you can use Print_r print the data test.
Next, create XML, loop arrays, and write data to the corresponding nodes in the XML.
$doc = new domdocument (' 1.0 ', ' utf-8 '); // declares version and encoding $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 PHP's built-in class DOMDocument to process and generate XML. The resulting XML format
<?xml version= "1.0" encoding= "Utf-8"?>
<root>
<data>
<name> Li Wanghao </na me>
<sex> men </sex>
<age>21</age>
</data>
...
</root>
Get XML Format data
Now we're going to assume that to get student information from a third party, the data format is XML, we need to parse the XML using PHP, and then display the parsed data or write it to the local database. And the key step here is parsing XML.
PHP has a number of ways to parse XML, where PHP provides a built-in XmlReader class that allows you to sequentially browse through the XML files, and you can imagine a cursor walking through the entire file node and grabbing the desired content. Using XmlReader is efficient, especially when reading very large XML data, and compared to other methods, using 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) { //decision node type $m = $i%3; if ($m ==1) $name = $reader- >value; //Read 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);
In order to separate the data name,sex from the age, we use $i%3 to determine the modulo, because the information under node data is present in the acquired XML as 3 subnodes.
Finally, you can write data to the local database by outputting or executing SQL statements, and this step skips