Created
May 17, 2018 08:37
-
-
Save iampava/ab67407002880631c1be5a3de9a542e0 to your computer and use it in GitHub Desktop.
SOAP Web Service in PHP | Sergiu Iacob
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define ('WS_URL', 'http://localhost:1234/lab12/ws.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->__soapCall('getWeather', array($_POST['date'])); | |
showWeather ($res); | |
} | |
catch (SOAPFault $exception) { // eroare :( | |
echo 'An exception occurred: ' . $exception->faultstring; | |
} | |
function showWeather($res){ | |
$doc = DomDOCUMENT::loadXML ($res); | |
$date = $doc->getElementsByTagName("date")[0]; | |
$day = $date->getElementsByTagName("day")[0]; | |
echo 'Ziua ' . $day->nodeValue . ' si vremea: ' . $date->getAttribute("weather"); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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"> | |
<table width="50%"> | |
<tr> | |
<td>Introdu data</td> | |
<td><input type="date" name="date"></td> | |
</tr> | |
</table> | |
<input type="submit" value="OK" name="send"> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$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; | |
} | |
// functie care furnizeaza cantitatea dintr-un sortiment de portocale | |
// (rezultatul intors va fi eterogen) | |
function getWeather ($payload) { | |
$domtree = new DOMDocument('1.0', 'UTF-8'); | |
$xmlRoot = $domtree->createElement("xml"); | |
$xmlRoot = $domtree->appendChild($xmlRoot); | |
$dateElement = $domtree->createElement ("date"); | |
$dateElement = $xmlRoot->appendChild ($dateElement); | |
$timestamp = strtotime($payload); | |
$dateWeather = getWeatherOnDay (date('l', $timestamp)); | |
$dateElement->setAttribute ("weather", $dateWeather); // am pus vremea ca atribut | |
$dayElement = $domtree->createElement ("day"); | |
$dayElement->nodeValue = date ('l', $timestamp); | |
$dateElement->appendChild($dayElement); | |
return $domtree->saveXML(); | |
} | |
function getWeatherOnDay ($day){ | |
$servername = "localhost:3306"; // mysql asculta la portul 3306 | |
$username = "root"; | |
$password = "sql"; | |
$dbname = "mysql"; // schema | |
// Create connection | |
$conn = new mysqli($servername, $username, $password, $dbname); | |
// Check connection | |
if ($conn->connect_error) { | |
die("Connection failed: " . $conn->connect_error); | |
} | |
$sql = "SELECT weather_value from weather where day = '" . $day . "'"; | |
// tabela mea e weather, cu 2 campuri: day si weather_value | |
$result = $conn->query($sql); | |
if ($result->num_rows == 1) { | |
while($row = $result->fetch_assoc()) { | |
return $row["weather_value"]; | |
} | |
} else { | |
return "nu stiu vremea pentru ziua asta"; | |
} | |
$conn->close(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment