PHP Web服務簡單應用

客戶端client.php

 

<?php
  ini_set("soap.wsdl_cache_enabled", 0);
  $client = new SoapClient('test.wsdl');
  $functions = $client->__getFunctions();
  $types = $client->__getTypes();
  print_r($functions);
  echo "<br>";
  print_r($types);
  //$client->hello();
  $str = "hello techie";
  echo $client->view($str);
  $x=2;
  $y=3;
  echo "<br>";
  echo $client->add($x,$y);
?>

 

服務端server.php

 

<?php
   require('data.php');
   ini_set("soap.wsdl_cache_enabled", 0);
   $server = new SoapServer('test.wsdl');
   $server->addFunction(SOAP_FUNCTIONS_ALL);
   if (isset($HTTP_RAW_POST_DATA)) {
           $request = $HTTP_RAW_POST_DATA;
   } else {
   $request = file_get_contents('php://input');
   }
   if ($_SERVER["REQUEST_METHOD"] == "POST")
   {
    $server->handle($request);
   }
   else
   {
     echo "This SOAP server can handle following functions: ";
     $functions = $server->getFunctions();
     foreach($functions as $k=>$func)
      {
       echo $k.". ".$func . "/n";
      }
   }

?>

 

服務端包含程序文件data.php

 

<?php
  function view($str)
  {
   return $str." you are the best";
  }
  function add($a,$b)
  {
   return $a+$b;
  }
?>

 

WSDL文件test.wsdl

 

<?xml version='1.0' encoding='UTF-8'?>

<!-- WSDL file generated by Zend Studio. -->

<definitions name="wps" targetNamespace="urn:wps" xmlns:typens="urn:wps" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">
 <message name="view">
  <part name="str"/>
 </message>
 <message name="viewResponse">
  <part name="viewReturn"/>
 </message>
 <message name="add">
  <part name="a"/>
  <part name="b"/>
 </message>
 <message name="addResponse">
  <part name="addReturn"/>
 </message>
 <portType name="dataPortType">
  <operation name="view">
   <input message="typens:view"/>
   <output message="typens:viewResponse"/>
  </operation>
  <operation name="add">
   <input message="typens:add"/>
   <output message="typens:addResponse"/>
  </operation>
 </portType>
 <binding name="dataBinding" type="typens:dataPortType">
  <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
  <operation name="view">
   <soap:operation soapAction="urn:dataAction"/>
   <input>
    <soap:body namespace="urn:wps" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
   </input>
   <output>
    <soap:body namespace="urn:wps" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
   </output>
  </operation>
  <operation name="add">
   <soap:operation soapAction="urn:dataAction"/>
   <input>
    <soap:body namespace="urn:wps" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
   </input>
   <output>
    <soap:body namespace="urn:wps" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
   </output>
  </operation>
 </binding>
 <service name="wpsService">
  <port name="dataPort" binding="typens:dataBinding">
   <soap:address location="http://localhost/webtest/server.php"/>
  </port>
 </service>
</definitions>

 

放到apache服務器下,即可實現測試!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章