This is an old revision of the document!


Cours WebServices

<?php
// The weather
    // Chose your method, with or without user info
    $wsdl = 'http://www.w3schools.com/xml/tempconvert.asmx?WSDL';
 
    // Make the connection
    $client = new SoapClient($wsdl);
 
    // Use this to see what services are available
    //var_dump($client->__getFunctions());
	$listeFonctions = $client->__getFunctions();
 
	//afficher
	foreach($listeFonctions as $k => $v) {
		echo $k."--".$v."\n" ;
	}
 
    // Actually call the service
    $result = $client->CelsiusToFahrenheit(array("Celsius"=>"25"));
    //var_dump($result);
 
	//afficher
	echo "les données\n" ;	
	foreach($result as $k => $v) {
		echo $k."\n" ;
		print_r($v);
	}
?>
  • Accéder à un objet PHP
stdClass Object
      (
         [MorningLow] => 
         [DaytimeHigh] => 83
       )
 $monOnject->MorningLow ;  
  • Python
from SOAPpy import WSDL
import zeep
 
client = zeep.Client('http://www.webservicex.net/ConvertSpeed.asmx?WSDL')
result = client.service.ConvertSpeed(
   102, 'kilometersPerhour', 'milesPerhour')
print result
  • Conversion de température fahrenheit → Celsius
  • Réalisez le même exercice que précédemment en utilisant la fonction inverse pour convertir une température de fahrenheit → Celsius.
  • Utilisez Eclipse pour vous connecter à ce webservice : http://www.webservicex.net/ConvertSpeed.asmx?WSDL
public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("test");
 
		try {
			TempConvertSoapProxy temp = new TempConvertSoapProxy();
			System.out.println(temp.fahrenheitToCelsius("123"));
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
public class TestSpeed {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ConvertSpeedsSoapProxy c = new ConvertSpeedsSoapProxy();
 
		SpeedUnit s1 = new SpeedUnit(SpeedUnit._kilometersPerhour);
		SpeedUnit s2 = new SpeedUnit(SpeedUnit._milesPerhour);
		try {
			System.out.println(c.convertSpeed(102, s1, s2));
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
  • Créer le webservice “quel jour est il ?” en PHP (partie cliente et server)
    • Le formulaire devra prendre en entrée une date et en sortie renvoyer le jour de la semaine
    • Consommer le WS avec des clients PHP / Java / Python / Ruby
    • Créez votre propre fichier wsdl avec eclipse
  • Exemple serveur php
<?php
// serv.php
class MyService
{
    public function add($x, $y)
    {
        return $x + $y;
    }
 
	public function multi($x, $y)
    {
        return $x * $y;
    }
}
 
//à adapter en fonction de votre configuration
$options = array(
    'uri' => 'http://localhost/LPRO/2016/serv.php',
    'location' => 'http://localhost/LPRO/2016/serv.php',
);
 
$server = new SOAPServer(null, $options);
$server->setObject(new MyService());
$server->handle();
 
?>
  • Exemple client php
<?php
// cli.php
// à adapter en fonction de votre configuration
$options = array(
    'uri' => 'http://localhost/LPRO/2016/serv.php',
    'location' => 'http://localhost/LPRO/2016/serv.php',
);
$client = new SOAPClient(null, $options);
echo $client->multi(10, 15);
 
?>

Exercice 5 : Manipuler le format JSON (open data Nantes)

<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
 
var_dump(json_decode($json));
var_dump(json_decode($json, true));
?>
{
    "response": {
        "version": "0.9"
        ,"termsofService": "http://www.actualitix.com"
        ,"features": {
        "date": "24/09/2013"
        }
    },
    "cotation_bourse": [
        {
        "bourse": {
        "heure": "15",
        "minute": "04"
        },
        "total": {
        "compagnie": "Total", 
        "cotation": "43,15", 
        "tendance": "+ 1,04"},    
        "michelin": {
        "compagnie": "Michelin", 
        "cotation": "82,42", 
        "tendance": "+ 1,20"}
        },
        {
        "bourse": {
        "heure": "09",
        "minute": "04"
        },
        "yahoo": {
        "compagnie": "Yahoo",
        "cotation": "30,26", 
        "tendance": "- 1,06"},
        "google": {
        "compagnie": "Google",
        "cotation": "886,50", 
        "tendance": "- 3,89"}
        }
        ]
}
  • Opendata Nantes
<?php
//$json = file_get_contents("cotation_bourse.json");
$json = file_get_contents("http://data.nantes.fr/api/getDisponibiliteParkingsPublics/1.0/39W9VSNCSASEOGV/?output=json");
 
$p_json = json_decode($json);
$test = $p_json->{'opendata'}->{'request'};
//var_dump($p_json->{'opendata'}->{'answer'}->{'data'}->{'Groupes_Parking'}->{'Groupe_Parking'});
foreach($p_json->{'opendata'}->{'answer'}->{'data'}->{'Groupes_Parking'}->{'Groupe_Parking'} as $k=>$v) {
	foreach($v as $k2=>$v2) {
		echo $k2."=>".$v2."<br/>" ;
	} 
	echo "-------------------------</br>" ;
}
 
//var_dump(json_decode($json, true));
//print_r($parsed_json->{'opendata'});
 
//echo prettyPrint(prettyPrint($json));
//echo json_encode($json, JSON_PRETTY_PRINT);
 
?>

Manipuler le format XML (parsing simple)

  $data = file_get_contents("http://?????.???");
  $dom = new DomDocument();
  //$dom->load('fichier.xml');
  $dom->loadXML($data);
  $racine = $dom->documentElement;
  echo $racine->nodeName;
  $title=$dom->getElementsByTagName('title');
  foreach($title as $v) {
  	echo $v->firstChild->nodeValue."<br/>";
  }
  • Recherchez un webservice permettant de lister les villes en fonction d'un code postal
  • SOA, Architectures Orientés Services, REST
  • SOAP, WSDL, UDDI
  • Exercices :

utilisez les API XML et JSON de php pour afficher le résultat

Yahoo

Google

  • Exercice1 : Créer un webservice en PHP et consommez ce webservice avec un client PHP et Ruby
  • Exercice 2 : Créer ce même webservice “Restful”
  • Exercice 3 : Créer un forumaire pour utiliser un moteur de recherche
  • Exercice 4 : Formulaire interrogation XML API search de twitter
  • Exercice 5 : Formulaire interrogation JSON API search de twitter
  • Exercice 6 : Creer un WebService RESTful