PHP transmits XML data via interface

Source: Internet
Author: User

The interface here requests data by passing parameters:
Request form: xxx.php?data=20140101&page=1

Parameter Description: Data represents the date of the request

Page represents the number of pages requested

<?php    header ("Content-type: text/xml;charset=utf-8");       date_default_timezone_set (' PRC ');          include  dirname (__file__). " /.. /.. /.. /inc/mysql.php ";     $link  = mysql_connect ($f _servername, $f _username, $f _password) ;     $d  = mysql_select_db ($f _htnewsdate, $link)  or die ("Server internal failure, please contact customer service!!! ");     mysql_query (' Set names utf8 ');          $data  = $_get[' data '];     $page  = $_get[' page '];      $list  = 200;     $time  = strtotime ($data);      $top  = array (         ' 189 '  =>  ' country ',      ' 1854 '  =>  ' Anhui ',  ' 1831 '  =>  ' Beijing ',  ' 1899 ' &NBsp;=>  ' Chongqing ',  ' 1877 '  =>  ' Fujian ', //a-f     ' 1882 '  =>   ' Gansu ',  ' 1891 '  =>  ' Guangdong ',  ' 1843 '  =>  ' Guangxi ',  ' 1870 '  =>  ' Guizhou ',    //g                                       ' 1879 '  =>  ' Hainan ',  ' 1858 '  =>  ' Hebei ',  ' 1849 '   =>  ' Henan ',  ' 1890 '  =>  ' Hubei ',     ' 1866 '  =>  ' Hunan ',  ' 1864 '  =>  ' Heilongjiang ', //h     ' 1857 '  =>  ' Jilin ',  ' 1855 '  = >  ' Jiangsu ',  ' 1893 '  =>  ' Jiangxi ',  ' 1845 '  =>  ' Liaoning ',  ' 1897 '  =>   ' Ningxia ',  ' 1892 '  =>  ' Inner Mongolia ', //j-n     ' 1873 '  =>  ' Qinghai ',   ' 1883 '  =>&nbsP; ' Shanghai ',  ' 1840 '  =>  ' Shandong ',  ' 1862 '  =>  ' Shanxi ',  ' 1874 '  =>  ' Shaanxi ',   ' 1887 '  =>  ' Sichuan ',    //q-s     ' 1839 '  =>  ' Tianjin ',  ' 1884 '  =>  ' Tibet ',  ' 1868 '  =>  ' Xinjiang ',  ' 1865 '  =>  ' Yunnan ',   ' 1851 '  =>  ' Zhejiang ',    //t-z          ' 1993 '  =>  ' info ',  ' 2043 '  =>  ' news ',     ';         foreach ($top  as  $key + $value) {          $arr []= $key;    }     $arr =implode (', ', $arr);          $limit  =  ($page-1)  *  $list;      $sql  = "Select id,typeid,title,filename,senddate as pubdate from dede_ Archives where arcrank=0  AND      (flag like  '%h% '  or  flag like   '%c% ')  and senddate>= ' 1388505600 '  and pubdate>= ' $time '  and typeid  in  ($arr)       order by pubdate desc limit $ Limit, $list ";     $result =mysql_query ($sql);     $data =array ();     while ($row =mysql_fetch_assoc ($result)) {        if ($ row[' typeid ']==189) {             $categoryId = 201;  //National         }else if ($row [' typeid ']==2043) {              $categoryId =203; //News          }else if ($row [' typeid ']==1993) {              $categoryid=299;    //Information         }else{              $categoryId =202; //Place          }                 //determine if the address          $file  = "http://". $_server[' Http_host ']. " /news/". Date (" Y ", $row [' pubdate ']). Date (" MD ", $row [' pubdate ']);         if ($row [' filename ']) {             $file  =   $file  . " /". $row [' filename '].". HTML ";        }else{              $file  =  $file  . " /". $row [' id '].". HTML ";        }         $data [] =array (             ' id ' = $row [' ID '],              ' categoryId ' = $categoryId,             //  ' title '  =>iconv (' GBK ', ' Utf-8//ignore ', $row [' title ']),              ' title '  => $row [' title '],              ' url ' = $file,              ' updatetime ' = $row [' pubdate '],         );        }          $sql _num =  "Select count (ID)  as num from dede_archives  WHERE arcrank=0  AND  (flag like  '%h% '  or  flag like   '%c% ')  AND senddate>= ' 1388505600 '  and pubdate>= ' $time '  AND typeid in  ($arr) ";      $result _num=mysql_query ($sql _num);     $row 1=mysql_fetch_assoc ($result _num);      $num  =  $row 1[' num '];     $xml  =  ' <?xml  Version= "1.0"  encoding= "UTF-8"?> ';     $xml  .=  ' <data> ';      $xml  .=  ' <apiVersion>1.0</apiVersion> ';     $xml  .=  ' <sourceId>102</sourceId> ';     $xml  .=  ' <totalnum > '. $num. ' </totalNum> ';     foreach ($data  as  $key = $val) {          $xml  .=  ' <item> ';          $xml  .=  ' <id> ' $val [' id ']. ' </id> ';         $xml  .=  ' <title> <! [cdata['. $val [' title ']. '] ></title> ';         $xml  .=  ' <url> '. $val [' url ' ].‘ </url> ';         $xml  .=  ' <categoryId> '. $val [' CategoryId ']. ' </categoryId> ';         $xml  .=  ' <updateTime> '. $ val[' UpdateTime ']. ' </updateTime> ';         $xml  .=  ' </item> ';     }     $xml  .=  ' </data> ';     echo   $xml;        ?>


PHP transmits XML data via interface

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.