<?php
/**
 * @author Pascal MARTIN
 * 
 * Copyright (c) 2008 Pascal MARTIN
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

// URL de notre "Web Service"
$url = 'http://tests/xml-ajax-prototype/test-server.php';

// Obtention des données XML sous forme d'une chaine de caractères
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$xmlString = curl_exec($ch);
curl_close($ch);

// Juste pour vérifier qu'on a bien reçu ce qu'on attendait :
//echo '<pre>' . htmlentities($xmlString) . '</pre>';

// Conversion de cette chaine de caractères en un objet DOMDocument
$doc = new DOMDocument();
$doc->loadXML($xmlString);

// Récupération de la racine du DOM
$nodesPersonne = $doc->firstChild->getElementsByTagName('personne');

// Parcours - exactement comme ce que nous avions fait en JS
$output = '';
for ($i=0 ; $i<$nodesPersonne->length ; $i++)
{
    $nodePersonne = $nodesPersonne->item($i);
    
    $attributes = $nodePersonne->attributes;
    $id = $attributes->getNamedItem('id')->nodeValue;
    
    $nodeNom = $nodePersonne->getElementsByTagName('nom')->item(0);
    $nom = $nodeNom->firstChild->nodeValue;
    
    $nodePrenom = $nodePersonne->getElementsByTagName('prenom')->item(0);
    $prenom = $nodePrenom->firstChild->nodeValue;
    
    $output .= $id . ' - ' 
               . strtoupper($nom) 
               . ' - ' . ucfirst($prenom)
               . '<br />';
}

// Et pour finir, affichage - le même que celui obtenu en JS !
echo $output;
