PHP generate Baidu News source XML

Source: Internet
Author: User

<?php

/*

Http://baike.baidu.com/view/125547.htm#2

Baidu Network News Open Agreement

*/

mysql_connect ($CFG [' Db_host '], $CFG [' Db_user '], $CFG [' Db_pass ']) or Die (' Database connection Error! ‘);

mysql_select_db ($CFG [' db_name ']);

mysql_query ("Set names UTF8");

$p = $CFG [' Tb_pre '];

$ids =array (a);

foreach ($ids as $k = + $v) {//begin foreach

$query = "Select ' A '. *, ' C '. *, ' d '. *, ' a '. ' Linkurl ' as ' alinkurl ' from ' {$p}category ' as ' C ', ' {$p}article_21 ' as ' a ', ' {$p}ar Ticle_data_21 ' as ' d ' where ' a '. ' CatID ' = ' c '. ' CatID ' and ' a '. ' Itemid ' = ' d '. ' Itemid ' and ' a '. ' Status ' =3 and ' a '. ' catid ' = ' {$ V} ' ORDER by ' a '. ' Itemid ' desc limit 100 ";

$result =mysql_query ($query);

$m = 0;

while ($info =mysql_fetch_array ($result, Mysql_assoc)) {

$data _array[$m [' title ']= ';

$data _array[$m [' link ']= ';

$data _array[$m] [' description ']= ';

$data _array[$m [' Text ']= ';

$data _array[$m [' Image ']= ';

$data _array[$m] [' keywords ']= ';

$data _array[$m [' Category ']= ';

$data _array[$m] [' Author ']= ';

$data _array[$m [' source ']= ';

$data _array[$m] [' PubDate ']= '

$m + +;

}

/* $data _array = Array (

Array

"Title" = "" ",

"Link" and "" ",

"description" and "" ",

"Text" = "" ",

"Image" = "123",

"keywords" = ",

"Category" = "" ",

"Author" and "" ",

"Source" = "",

"PubDate" = "2012-06-12 14:03"

)

);*/

Property array

/* $attribute _array = Array (

' title ' = = Array (

' Size ' = 1

)

);

*/

Create an XML document and set the XML version and encoding.

$dom =new DomDocument (' 1.0 ', ' GBK ');

Creating the root node document

$document = $dom->createelement (' document ');

$dom->appendchild ($document);

Create Document->website

$website = $dom->createelement (' website ');

$document->appendchild ($website);

$mytext = $dom->createtextnode (' http://www.abc.net/');

$website->appendchild ($mytext);

Create Document->webmaster

$webmaster = $dom->createelement (' webmaster ');

$document->appendchild ($webmaster);

$mytext = $dom->createtextnode (' [email protected] ');

$webmaster->appendchild ($mytext);

Create Document->updateperi

$updateperi = $dom->createelement (' Updateperi ');

$document->appendchild ($updateperi);

$mytext = $dom->createtextnode (' 60 ');

$updateperi->appendchild ($mytext);

Batch Create Document->item

foreach ($data _array as $data) {

$item = $dom->createelement (' item ');//Create an item

$document->appendchild ($item);

Create_item ($dom, $item, $data, $attribute _array);

}

echo $dom->savexml ();

$dom->save ("./".) {$v}.xml ");

}//end foreach

function Create_item ($dom, $item, $data, $attribute) {

if (Is_array ($data)) {

foreach ($data as $key = = $val) {

Creating elements

$ $key = $dom->createelement ($key);

$item->appendchild ($ $key);

Creating element values

if ($key! = "PubDate") {

Use CDATA to include

$cdatasection = $dom->createcdatasection ($val);

$ $key->appendchild ($cdatasection);

}else{

$mytext = $dom->createtextnode ($val);

$ $key->appendchild ($mytext);

}

if (Isset ($attribute [$key])) {

If there are related attributes in this field, you need to set

foreach ($attribute [$key] as $akey = = $row) {

To create an attribute node

$ $akey = $dom->createattribute ($akey);

$ $key->appendchild ($ $akey);

Creating attribute Value Nodes

$aval = $dom->createtextnode ($row);

$ $akey->appendchild ($aval);

}

}//End If

}

}//End If

}//End Function

?>

PHP generate Baidu News source XML

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.