Skip to content

Instantly share code, notes, and snippets.

@iampava
Created May 19, 2018 11:52
Show Gist options
  • Save iampava/6709e1a643f30fb6639fe6c89efb9dd0 to your computer and use it in GitHub Desktop.
Save iampava/6709e1a643f30fb6639fe6c89efb9dd0 to your computer and use it in GitHub Desktop.
SOAP Web Service in PHP | Author: Teodor Proca
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>LAb10</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" media="screen" href="main.css" />
<script src="main.js"></script>
</head>
<body>
<form action="./script.php" method="POST">
<input type="date" name="date" id="date">
<button type="submit">Submit</button>
</form>
</body>
</html>
<?php
if (isset($_POST['date'])) {
$date = $_POST['date'];
define ('WS_URL', 'http://localhost:8888/lab10/weather.php');
try {
$client = new SoapClient(null, // nu furnizam niciun WSDL
array('location' => WS_URL, // adresa serviciului Web
'uri' => 'http://web.info/porto', // spatiul de nume corespunzator serviciului Web apelat
'trace' => 1 // se vor furniza informatii de depanare
)
);
$res = $client->getWeather($date);
$doc = new DOMDocument();
$doc->loadXML($res);
echo $doc->saveXML();
} catch (SOAPFault $exception) { // eroare :(
echo 'An exception occurred: ' . $exception->faultstring;
}
}
?>
<?php
try {
// nu oferim nicio descriere WSDL, stabilim URI-ul serviciului
$server = new SoapServer(null,
array('uri' => 'http://web.info/porto') // spatiul de nume folosit
);
// adaugam metodele implementate
$server->addFunction('getWeather');
// asteptam cereri SOAP
$server->handle();
} catch (SOAPFault $exception) {
echo 'An exception occurred: ' . $exception;
}
function createXml($weather, $precipitation, $umidity) {
$domtree = new DOMDocument('1.0', 'UTF-8');
$xmlRoot = $domtree->createElement("xml");
$xmlRoot = $domtree->appendChild($xmlRoot);
$currentTrack = $domtree->createElement("WeatherData");
$currentTrack = $xmlRoot->appendChild($currentTrack);
$currentTrack->appendChild($domtree->createElement('Weather', $weather));
$currentTrack->appendChild($domtree->createElement('Precipitation', $precipitation));
$currentTrack->appendChild($domtree->createElement('Umidity', $umidity));
return $domtree->saveXML();
}
function getWeather ($date) {
// uzual, vom efectua o interogare SQL, o procesare de date (CSV, JSON, XML,...),
// o invocare a altui serviciu Web etc.
$day = date("l", strtotime($date));
if($day === 'Monday') {
$rez = createXml('Sunny!', '30%', '65%');
}
if($day === 'Tuesday') {
$rez = createXml('Rainy!', '90%', '75%');
}
if($day === 'Wednesday') {
$rez = createXml('Happy day!', '0%', '45%');
}
if($day === 'Thursday') {
$rez = createXml('Sunny!', '10%', '75%');
}
if($day === 'Friday') {
$rez = createXml('Sunny!', '30%', '35%');
}
if($day === 'Saturday') {
$rez = createXml('Sunny!', '30%', '35%');
}
if($day === 'Sunday') {
$rez = createXml('Sunny!', '30%', '35%');
}
return $rez;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment