我正在尝试使用PHP和YouTube数据API将我的所有视频和视频信息放入MySQL数据库。我正在使用simplexml_load_file
功能。我无法让它发挥作用。它没有任何结果。 (请原谅已弃用的PHP / MySQL。)
这是代码:
<?php
mysql_connect('localhost', '*NOT NEEDED*', '*NOT NEEDED*');
mysql_select_db('demoScript');
mysql_query("TRUNCATE videos");
$url = "http://gdata.youtube.com/feeds/api/users/demoScript/uploads";
$xml = simplexml_load_file($url);
$targeti = substr_count($url, "<entry>");
for($i=0; $i<$targeti; $i++){
$title = $xml->entry->title;
$id = $xml->entry->id;
$date = $xml->entry->published;
$views = $xml->entry->yt['viewCount'];
$rating = $xml->entry->gd['average'];
$faves = $xml->entry->yt['favoriteCount'];
$desc = $xml->entry->content;
mysql_query("INSERT INTO `videos` VALUES ('".$id."','".$title."','".$date."','".$views."','".$rating."','".$faves."','".$desc."')");
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel='stylesheet' href='css/bootstrap.css' />
<link rel='stylesheet' href='css/bootstrap-responsive.css' />
</head>
<body>
<ul class='nav nav-tabs'>
<div class='brand' style='float: right; margin-right: 10px; margin-top: 8px; font-size: 20px;'>demoScript</div>
<li class='active'><a href='#'>Home</a></li>
<li><a href='#'>About</a></li>
<li><a href='#'>Videos</a></li>
<li><a href='#'>Contact</a></li>
</ul>
<div class='hero-unit'>
<center><h2>Most Viewed Video</h2></center><p />
</div>
<script src='js/bootstrap.js'></script>
</body>
答案 0 :(得分:1)
您忘了在循环中使用$ i。
您可以执行$xml->entry[$i];
之类的操作,但simplexml_load_file已经为您提供了一个对象。您可以使用以下内容循环其子项:
foreach($xml->nameOfParentTag as $entry){
$title = $entry->title;
$id = $entry->id;
$date = $entry->published;
$views = $entry->yt['viewCount'];
$rating = $entry->gd['average'];
$faves = $entry->yt['favoriteCount'];
$desc = $entry->content;
}
而不是使用子字符串计数器。