{"id":530,"date":"2010-10-11T15:12:40","date_gmt":"2010-10-11T15:12:40","guid":{"rendered":""},"modified":"2010-10-11T15:12:40","modified_gmt":"2010-10-11T15:12:40","slug":"530","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=530","title":{"rendered":"\u7b80\u5355 php webservice\u5b9e\u4f8b"},"content":{"rendered":"<p>\u9996\u5148\u5927\u5bb6\u8981\u7b80\u5355\u4e86\u89e3\u4e86\u4f55\u8c13webservice\uff0c\u63a5\u4e0b\u6765\u5c31\u505a\u4e24\u4e2a\u975e\u5e38\u7b80\u5355\u7684\u4f8b\u5b50\uff0cwebservice\u8fd8\u662f\u9003\u4e0d\u5f00server\u7aef\u4e0eclient\u7aef\u3002<\/p>\n<p>\u6211\u6d4b\u8bd5\u7684\u73af\u5883\u4e3a\uff1aapache2.2.11 php5.2.10<\/p>\n<p>\u505a\u8fd9\u4e2a\u6d4b\u8bd5\u4e4b\u524d\uff0c\u8981\u786e\u8ba4\u4f60\u7684php\u914d\u7f6e\u6587\u4ef6\u4e2d\u5df2\u7ecf\u5c06soap\u6269\u5c55\u6253\u5f00\uff0c\u5373extension=php_soap.dll;<br \/>OK \u73b0\u5728\u6211\u4eec\u6765\u4f53\u9a8cwebservice<\/p>\n<p>\/\/server\u7aef serverSoap.php<br \/><code><br \/>$soap = new SoapServer(null,array('uri'=&gt;\"<a href=\"http:\/\/192.168.0.101\/\" target=\"_blank\" rel=\"external\">http:\/\/192.168.0.101\/<\/a>\"));\/\/This uri is your SERVER ip.<br \/>$soap-&gt;addFunction('minus_func');&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/Register the function<br \/>$soap-&gt;addFunction(SOAP_FUNCTIONS_ALL);<br \/>$soap-&gt;handle();<\/p>\n<p>function minus_func($i, $j){<br \/>&nbsp;&nbsp;&nbsp;&nbsp;$res = $i - $j;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;return $res;<br \/>}<br \/><\/code><\/p>\n<p>\/\/client\u7aef clientSoap.php<br \/>try {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;$client = new SoapClient(null,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array(&#8216;location&#8217; =&gt;&#8221;<a href=\"http:\/\/192.168.0.101\/\" target=\"_blank\" rel=\"external\">http:\/\/192.168.0.101\/<\/a>serverSoap.php&#8221;,&#8217;uri&#8217; =&gt; &#8220;<a href=\"http:\/\/127.0.0.1\/\" target=\"_blank\" rel=\"external\">http:\/\/127.0.0.1\/<\/a>&#8220;)<br \/>&nbsp;&nbsp;&nbsp;&nbsp;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo $client-&gt;minus_func(100,99);<\/p>\n<p>} catch (SoapFault $fault){<br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo &#8220;Error: &#8220;,$fault-&gt;faultcode,&#8221;, string: &#8220;,$fault-&gt;faultstring;<br \/>}<br \/>[\/code]<\/p>\n<p>\u8fd9\u662f\u5ba2\u6237\u7aef\u8c03\u7528\u670d\u52a1\u5668\u7aef\u51fd\u6570\u7684\u4f8b\u5b50\uff0c\u6211\u4eec\u518d\u641e\u4e2aclass\u7684\u3002<\/p>\n<p>\/\/server\u7aef serverSoap.php<br \/><code><br \/>$classExample = array();<\/p>\n<p>$soap = new SoapServer(null,array('uri'=&gt;\"<a href=\"http:\/\/192.168.0.101\/\" target=\"_blank\" rel=\"external\">http:\/\/192.168.0.101\/<\/a>\",'classExample'=&gt;$classExample));<br \/>$soap-&gt;setClass('chesterClass');<br \/>$soap-&gt;handle();<\/p>\n<p>class chesterClass {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;public $name = 'Chester';<\/p>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;function getName() {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $this-&gt;name;<br \/>&nbsp;&nbsp;&nbsp;&nbsp;}<br \/>}<br \/><\/code><br \/>\/\/client\u7aef clientSoap.php<br \/><code><br \/>try {<br \/>&nbsp;&nbsp;&nbsp;&nbsp;$client = new SoapClient(null,<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;array('location' =&gt;\"<a href=\"http:\/\/192.168.0.101\/\" target=\"_blank\" rel=\"external\">http:\/\/192.168.0.101\/<\/a>test\/serverSoap.php\",'uri' =&gt; \"<a href=\"http:\/\/127.0.0.1\/\" target=\"_blank\" rel=\"external\">http:\/\/127.0.0.1\/<\/a>\")<br \/>&nbsp;&nbsp;&nbsp;&nbsp;);<br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo $client-&gt;getName();<\/p>\n<p>} catch (SoapFault $fault){<br \/>&nbsp;&nbsp;&nbsp;&nbsp;echo \"Error: \",$fault-&gt;faultcode,\", string: \",$fault-&gt;faultstring;<br \/>}<br \/><\/code><\/p>\n<p>\u4ee5\u4e0a\u4ee3\u7801\u6211\u5df2\u7ecf\u6d4b\u8bd5\u901a\u8fc7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u5927\u5bb6\u8981\u7b80\u5355\u4e86\u89e3\u4e86\u4f55\u8c13webservice\uff0c\u63a5\u4e0b\u6765\u5c31\u505a\u4e24\u4e2a\u975e\u5e38\u7b80\u5355&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-530","post","type-post","status-publish","format-standard","hentry","category-Php"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=530"}],"version-history":[{"count":0,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/530\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}