chapter 3

<?php
//File: SoapClient.php
$wsdl = "http://localhost/WebServices/wsdl/po_mysql.wsdl";
$handle = fopen("purchaseOrder.xml", "r");               http://www.w3schools.com/php/func_filesystem_fopen.asp
$po= fread($handle, filesize("purchaseOrder.xml"));    http://www.w3schools.com/PHP/func_filesystem_fread.asp 
fclose($handle);
$client = new SoapClient($wsdl);    http://www.w3schools.com/php/php_exception.asp   http://us.php.net/manual/en/language.oop5.basic.php 
try {                                                        http://www.w3schools.com/php/php_exception.asp
print $result=$client->placeOrder($po);
}
catch (SoapFault $exp) {      http://us3.php.net/soap
print $exp->getMessage();    http://www.w3schools.com/php/php_exception.asp 
}
?>

purchaseorder.xml:

- <purchaseOrder>
  <pono>108128476</pono>
- <billTo>
  <name>Tony Jamison</name>
  <street>24 Johnson Road</street>
  <city>Big Valley</city>
  <state>VA</state>
  <zip>23032</zip>
  <country>US</country>
  </billTo>
- <shipTo>
  <name>Janet Thomson</name>
  <street>11 Maple Street</street>
  <city>Small Valley</city>
  <state>VA</state>
  <zip>23037</zip>
  <country>US</country>
  </shipTo>
- <items>
- <item>
  <partId>743</partId>
  <quantity>4</quantity>
  <price>15.5</price>
  </item>
- <item>
  <partId>235</partId>
  <quantity>7</quantity>
  <price>15.75</price>
  </item>
  </items>
  </purchaseOrder>

 

po_mysql.wsdl:

<?xml version="1.0" encoding="utf-8"?>
<definitions name ="poService"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://schemas.xmlsoap.org/wsdl/"
targetNamespace="http://localhost/WebServices/wsdl/po.wsdl">
<message name="getPlaceOrderInput">
<part name="body" element="xsd:string"/>
</message>
<message name="getPlaceOrderOutput">
<part name="body" element="xsd:string"/>
</message>
<portType name="poServicePortType">
<operation name="placeOrder">
<input message="tns:getPlaceOrderInput"/>
<output message="tns:getPlaceOrderOutput"/>
</operation>
</portType>
<binding name="poServiceBinding" type="tns:poServicePortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="placeOrder">
<soap:operation soapAction="http://localhost/WebServices/ch3/placeOrder"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="poService">
<port name="poServicePort" binding="tns:poServiceBinding">
<soap:address location="http://localhost/WebServices/ch3/SOAPServer.php"/>
</port>
</service>
</definitions>
 

<?php
//File: SoapServer.php
require_once "purchaseOrder.php";
$wsdl= "http://localhost/WebServices/wsdl/po_mysql.wsdl";
$srv= new SoapServer($wsdl);
$srv->setClass("purchaseOrder");
$srv->handle();
?>

<?php
//File purchaseOrder.php
class purchaseOrder {
function placeOrder($po) {
if(!$conn = mysql_connect('localhost', 'usr', 'pswd')){
throw new SoapFault("Server","Failed to connect to database");
};
if(!mysql_select_db('my_db')){
throw new SoapFault("Server","Failed to select database");
};
$sql = "INSERT INTO purchaseOrders SET doc='".$po."'";
if (!$result = mysql_query($sql)) {
throw new SoapFault("Server","Failed to insert PO");
};
mysql_close($conn);
$msg='<rsltMsg>PO inserted!</rsltMsg>';
return $msg;
}
}
?>