LSN
LSN

Reputation: 328

Parse XML data with PHP

I have a XML feed with the following URL with IDs of trips to be retrieved.

http://www.expeditiontrips.com/xml/triplist.xml

Based on the each trip information can be retrieved from following URL where the ID becomes the XML name

http://www.expeditiontrips.com/xml/trips/2945.xml

I need to show this feed on my website using PHP. I retrieved trip IDs using the following code but then I have no clue how to use that information to get the individual trip details and show them in my site.

<?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;

?>

Upvotes: 1

Views: 76

Answers (2)

LSN
LSN

Reputation: 328

Finally Ended up with the following code. :) Hope it helps someone

    <?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">&laquo;</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.'">&raquo;</a>';
        }
        echo '</div>';  
    ?>

Upvotes: 0

PHPhil
PHPhil

Reputation: 1540

I still wasn't sure how you would like your layout, but this will get you started. I just showed you how to get a single value and how to get a values from an array.

<?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 . '">';
    }

?>

Upvotes: 1

Related Questions