Skip to content

Instantly share code, notes, and snippets.

@iampava
Created May 17, 2018 08:31
Show Gist options
  • Save iampava/de4a2694b71c0e8522132839730ca28e to your computer and use it in GitHub Desktop.
Save iampava/de4a2694b71c0e8522132839730ca28e to your computer and use it in GitHub Desktop.
SOAP Web Service in PHP | Author: Lucian Cochior
<?php
define ('WS_URL', 'http://localhost/weather.php');
try {
$client = new SoapClient(null,
array('location' => WS_URL,
'uri' => 'http://web.info/porto', //
'trace' => 1
)
);
$date = $_POST['date'];
$res = $client->__soapCall('getWeather', array($date));
echo $res;
} catch (SOAPFault $exception) { // eroare :(
echo 'An exception occurred: ' . $exception->faultstring;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="app.php" method="POST">
<input type="date" name="date">
<input type="submit" name="submit">
</form>
</body>
</html>
<?php
try {
$server = new SoapServer(null,
array('uri' => 'http://web.info/porto')
);
$server->addFunction('getWeather');
$server->handle();
} catch (SOAPFault $exception) {
echo 'An exception occurred: ' . $exception;
}
function getWeather ($product) {
$date = new DateTime($product);
$day = $date->format('l');
$res = '';
switch ($day){
case 'Monday': $res = 'insorit';
break;
case 'Tuesday': $res = 'fulgere';
break;
case 'Wednesday': $res = 'tunete';
break;
case 'Thursday': $res = 'ceata';
break;
case 'Friday': $res = 'ploua';
break;
case 'Saturnday': $res = 'ninge';
break;
case 'Sunday': $res = 'canicula';
break;
}
$dom = new DomDocument("1.0", "UTF-8");
$root = $dom->createElement('xml');
$dom->appendChild($root);
$node= $dom -> createElement('Zi', $day);
$root->appendChild($node);
$node=$dom->createElement('prognoza', $res);
$root->appendChild($node);
return $dom->saveXML();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment