php发布webservice

webservice有很多优点经常被用来做数据交换一类的工作,php也不甘示弱新版本的php也可以用来开发webservice了。虽然比起java,asp.net开发起来要麻烦一些,下面分享下我用php开发的第一个webservice

1.开发webservice要先打开php_soap.dll扩展库,编辑php.ini文件去前面的分号,之后看看设置生效没有

php发布webservice_第1张图片

看到有这个就说明配置成功了,下面就可以接着开发了


2.php发布webservice和java不同,php需要生成一个wsdl文件才能把接口发布出去,如果是zendstudio会方便点。如果没有zendstudio也没关系,下面这个工具类可以生成wsdl文件

class_name = $class_name;
		$this->service_name = $service_name;
	}
	
	/**
	 * SoapDiscovery::getWSDL() Returns the WSDL of a class if the class is instantiable.
	 * 
	 * @return string
	 **/
	public function getWSDL() {
		if (empty($this->service_name)) {
			throw new Exception('No service name.');
		}
		$headerWSDL = "\n";
		$headerWSDL.= "service_name\" targetNamespace=\"urn:$this->service_name\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tns=\"urn:$this->service_name\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns=\"http://schemas.xmlsoap.org/wsdl/\">\n";
		$headerWSDL.= "\n";

		if (empty($this->class_name)) {
			throw new Exception('No class name.');
		}
		
		$class = new ReflectionClass($this->class_name);
		
		if (!$class->isInstantiable()) {
			throw new Exception('Class is not instantiable.');
		}
		
		$methods = $class->getMethods();
		
		$portTypeWSDL = '';
		$bindingWSDL = '\n\n";
		$serviceWSDL = '\n\nservice_name.'Port" binding="tns:'.$this->service_name."Binding\">\n\n\n";
		$messageWSDL = '';
		foreach ($methods as $method) {
			if ($method->isPublic() && !$method->isConstructor()) {
				$portTypeWSDL.= '\n".'\ngetName()."Response\" />\n\n";
				$bindingWSDL.= '\n".'\nservice_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n\n\nservice_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n\n\n";
			    $messageWSDL.= '\n";
				$parameters = $method->getParameters();
				foreach ($parameters as $parameter) {
					$messageWSDL.= '\n";
				}
				$messageWSDL.= "\n";
				$messageWSDL.= '\n";
				$messageWSDL.= '\n";
				$messageWSDL.= "\n";
			}
		}
		$portTypeWSDL.= "\n";
		$bindingWSDL.= "\n";
		//return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '');
	    $fso = fopen($this->class_name . ".wsdl" , "w"); 
        fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '')); 
        exit; 
	}
	
	/**
	 * SoapDiscovery::getDiscovery() Returns discovery of WSDL.
	 * 
	 * @return string
	 **/
	public function getDiscovery() {
		return "\n\n\n";
	}
}

?>

3.编写webservice的核心类StudentService



正式的开发肯定比这个复杂,我这里只能算是演示

4.借助生成wsdl的工具类和第三步生成的类来生成wsdl,生成wsdl的类也是一个php,命名可以随意我这里命名为StudentWebService,使用下面的代码就能生成wsdl

getWSDL();

?>

Student就是上面那个类的类名,第二个参数是接口的名称可以自己定义,在浏览器输入地址生成一个wsdl,名称为Student.wsdl.如果出现异常wsdl就不会生成成功


5.接下来删除第四步里的php文件里面的内容,改为下面这样

setClass("Student");
//处理请求 
$objSoapServer->handle();
?>

到这里接口已经开发完毕,可以在浏览器地址栏输入http://localhost/webservice/StudentWebService.php?wsdl看wsdl是否能显示出来


6.编写一个客户端来测试webservice接口,如果不会写也可以偷个懒直接使用soapui就行,下面是php的客户端测试类test.php

outputName('蔡依林')); 
echo "
"; ?>

来看看期待已久的结果

php发布webservice_第2张图片

平时的开发可能是返回一个xml或者json,要真正学好webservice这两块的知识也要很熟练才可以。


你可能感兴趣的:(PHP,webservice)