如果您必须执行特定查询,将数据存储到数据库中绝对是最好的方法。我还在使用 TheTVDB API(集成在 Wordpress 中),我可以给你一些 PHP 代码。首先,您必须使用以下代码创建数据库,然后您拥有从电视节目的 XML url 插入数据的 PHP 脚本。
SQL查询:
CREATE TABLE IF NOT EXISTS `tvshowsinfo` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`showname` varchar(255) CHARACTER SET utf8 NOT NULL,
`lang` varchar(255) CHARACTER SET utf8 NOT NULL,
`episodeid` int(9) NOT NULL,
`episodenumber` int(5) NOT NULL,
`season` int(5) NOT NULL,
`director` varchar(255) CHARACTER SET utf8 NOT NULL,
`episodename` varchar(255) CHARACTER SET utf8 NOT NULL,
`firstaired` varchar(255) CHARACTER SET utf8 NOT NULL,
`gueststars` text CHARACTER SET utf8 NOT NULL,
`overview` text CHARACTER SET utf8 NOT NULL,
`rating` varchar(255) CHARACTER SET utf8 NOT NULL,
`ratingcnt` int(9) NOT NULL,
`writer` varchar(255) CHARACTER SET utf8 NOT NULL,
`episodeimg` varchar(255) CHARACTER SET utf8 NOT NULL,
`seasonid` int(9) NOT NULL,
`seriesid` int(9) NOT NULL,
`thumbht` int(4) NOT NULL,
`thumbwd` int(4) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
PHP/Wordpress 脚本:
<?php
$url = "Your URL";
$xmlstr = file_get_contents($url);
$elements = json_decode(json_encode((array)simplexml_load_string($xmlstr)),1);
global $wpdb;
$mquery = "INSERT INTO tvshowsinfo (showname,lang,episodeid,episodenumber,season,director,episodename,firstaired,gueststars,overview,rating,ratingcnt,writer,episodeimg,seasonid,seriesid,thumbht,thumbwd) VALUES ";
$i = 0;
foreach ($elements['Episode'] as $element) {
if ($i !== 0) { $mquery .= ", "; }
if ($element["Combined_season"] !== "0") {
$mquery .= "('".mysql_real_escape_string($_POST["seriesname"])."', '".$element["Language"]."', '".$element["id"]."', '".$element["Combined_episodenumber"]."', '".$element["Combined_season"]."', '".mysql_real_escape_string($element["Director"])."', '".mysql_real_escape_string($element["EpisodeName"])."', '".$element["FirstAired"]."', '".mysql_real_escape_string($element["GuestStars"])."', '".mysql_real_escape_string($element["Overview"])."', '".$element["Rating"]."', '".$element["RatingCount"]."', '".mysql_real_escape_string($element["Writer"])."', 'http://thetvdb.com/banners/".$element["filename"]."', '".$element["seasonid"]."', '".$element["seriesid"]."', '".$element["thumb_height"]."', '".$element["thumb_width"]."')";
$i++;
}
}
$wpdb->query($mquery);
$wpdb->print_error();
/* DEBUG */
?>
<pre><?php print_r($elements); ?></pre>