我有一个XML Feed,其中包含以下网址,其中包含要检索的行程ID。
http://www.expeditiontrips.com/xml/triplist.xml
根据每次旅行,可以从以下URL中检索信息,其中ID成为XML名称
http://www.expeditiontrips.com/xml/trips/2945.xml
我需要使用PHP在我的网站上显示此Feed。我使用以下代码检索了旅行ID,但后来我不知道如何使用该信息来获取各个旅行详细信息并在我的网站上显示。
<?php
$ch = curl_init('http://www.expeditiontrips.com/xml/triplist.xml');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml_raw = curl_exec($ch);
curl_close($ch);
$trips = simplexml_load_string($xml_raw);
foreach ($trips as $trip):
echo '<li><div class="title">'.$trip.'</div></li>';
endforeach;
?>
答案 0 :(得分:1)
我仍然不确定你的布局如何,但这会让你开始。我刚刚向您展示了如何获取单个值以及如何从数组中获取值。
<?php
$ch = curl_init('http://www.expeditiontrips.com/xml/triplist.xml');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml_raw = curl_exec($ch);
curl_close($ch);
$trips = simplexml_load_string($xml_raw);
//uncomment next 2 lines, to view the array $trips
// echo "<pre>";
// print_r($trips);
//pick one id, for example the first one.
$ch = curl_init('http://www.expeditiontrips.com/xml/trips/' . $trips->trip[0] . '.xml');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml_raw = curl_exec($ch);
curl_close($ch);
$info = simplexml_load_string($xml_raw);
//uncomment next 2 lines, to view the array $info
// echo "<pre>";
// print_r($info);
//single value
echo '<a href="' . $info->url . '">' . $info->url . '</a><br />';
//multiple valeus in an array
foreach($info->images->image as $images){
echo '<img src="' . $images->url . '">';
}
?>
答案 1 :(得分:0)
最后结束以下代码。 :)希望它可以帮助某人
<?php
$ch = curl_init('http://www.expeditiontrips.com/xml/triplist.xml');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml_raw = curl_exec($ch);
curl_close($ch);
$trips = simplexml_load_string($xml_raw);
$totalTrips = count($trips);
$perPage = 10;
$page = isset($_GET['trip']) && ($page = intval($_GET['trip'])) > 0 ? $page : 1;
$start = ($page - 1) * $perPage;
$end = $start + $perPage;
for ($a=$start; $a<$end; ++$a) {
if (isset($trips->trip[$a])) {
$ch = curl_init('http://www.expeditiontrips.com/xml/trips/' . $trips->trip[$a] . '.xml');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml_raw = curl_exec($ch);
curl_close($ch);
$info = simplexml_load_string($xml_raw);
$ran = array(1,2,3,4,5,6);
$randomElement = $ran[array_rand($ran, 1)];
echo '<div id="trip-item">';
echo '<div class="trip-title">'.$info->name.'</div>';
echo '<div class="trip-body">';
echo '<div class="col span_3">';
echo '<img src="'.$info->images->image[$randomElement]->url.'" />';
echo '</div>';
echo '<div class="col span_9 col_last">';
echo '<span class="mini-des">'.$info->description.'</span>';
echo '<table>';
echo '<tr>';
echo '<td>Prices: '.$info->prices->price->value.'</td>';
echo '<td>Days:</td>';
echo '</tr>';
echo '<tr>';
echo '<td>Ship: '.$info->additions->addition[0]->body.'</td>';
echo '<td><a href="">Click here for Departure Dates</a></td>';
echo '</tr></table></div></div></div>';
}
}
$pages = ceil($totalTrips / $perPage);
$low = $page - 3;
$high = $page + 3;
echo '<div class="paginator">';
if ($page > 3) {
echo '<a href="?trip=1">«</a>';
}
for ($a=$low; $a<=$high; ++$a) {
if($a > $pages) {
break;
}
if($a > 0 && $a != $page) {
echo '<a href="?trip=' . $a . '">' . $a . '</a>';
} else if($a > 0) {
echo '<span>' . $a . '</span>';
}
}
if ($page != $pages) {
echo '<a href="?t='.$pages.'">»</a>';
}
echo '</div>';
?>