第一、修改php.ini文件, 打开下面的三个扩展库, 然后重新启动php服务
extension=php_curl.dll soap 会用到curl库
extension=php_openssl.dll
extension=php_soap.dll
第二、编写 server.php
<?php function greet($param) { $retval = 'Hello '.$param; //$result = array('greetReturn'=>$retval); return $retval; } $server = new SoapServer(null, array('uri'=>"http://127.0.0.1/")); //$server = new SoapServer('http://localhost/hello.wsdl'); $server->addFunction('greet'); //$server->addFunction(SOAP_FUNCTIONS_ALL); $server->handle(); ?>
第三、编写client.php
<?php try { //$client = new SoapClient('http://localhost/hello.wsdl'); $client = new SoapClient(null, array('location' =>"http://127.0.0.1/server.php", 'uri'=> "http://127.0.0.1/")); //$client = new SoapClient(null, array('location' =>"http://localhost/server.php", 'uri'=> "http://localhost/")); //$result = $client->__soapCall('greet', array('name' => 'Sam')); $result = $client->greet("zhang"); printf("Result = %s/n", $result); } catch (Exception $e) { printf("Exception Message = %s/n",$e->__toString()); } ?>
第四、用浏览器访问 http://127.0.0.1/client.php
输出 Hello zhang
使用中碰到问题
Fatal error: Maximum execution time of 30 seconds exceeded inD:\wamp\www\client.php on line19
原因是下面这行代码里面的localhost, 修改为 127.0.0.1的IP地址就可以了。
$client = new SoapClient(null, array('location' =>"http://localhost/server.php", 'uri'=> "http://localhost/"));
//$result = $client->__soapCall('greet', array('name' => 'Sam'));
$result = $client->greet("zhang");
上面这两行代码都可以达到调用服务端greet函数的目的