PHP呼び出しwebserviceインタフェースノート
10931 ワード
1、【php_soap.dll拡張を開く】
phpが見つかりました.iniファイル;extension=php_soap.dll
「;」を削除し、apacheサーバの再起動
2、【SoapDiscovery.class.phpクラスをダウンロード】
このクラスライブラリファイルは直接ネットで検索すればあります.コードは以下の通りです.
3、【wsdlファイルの生成】
ファイルserverを作成します.php、コードは以下の通りです.
serverを実行します.phpファイルは、Testを生成することができる.wsdlファイルは、次のとおりです.
完了します.
phpが見つかりました.iniファイル;extension=php_soap.dll
「;」を削除し、apacheサーバの再起動
2、【SoapDiscovery.class.phpクラスをダウンロード】
このクラスライブラリファイルは直接ネットで検索すればあります.コードは以下の通りです.
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 = "
";
$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/\">
";
$headerWSDL.= "
";
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 = '
";
$serviceWSDL = '
service_name.'Port" binding="tns:'.$this->service_name."Binding\">
";
$messageWSDL = '';
foreach ($methods as $method) {
if ($method->isPublic() && !$method->isConstructor()) {
$portTypeWSDL.= '
".'
";
$bindingWSDL.= '
".'
service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />
";
$messageWSDL.= '
";
$parameters = $method->getParameters();
foreach ($parameters as $parameter) {
$messageWSDL.= '
";
}
$messageWSDL.= "
";
$messageWSDL.= '
";
$messageWSDL.= '
";
$messageWSDL.= "
";
}
}
$portTypeWSDL.= "
";
$bindingWSDL.= "
";
// 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, ''));
}
/**
* SoapDiscovery::getDiscovery() Returns discovery of WSDL.
*
* @return string
**/
public function getDiscovery() {
return "
";
}
}
?>
3、【wsdlファイルの生成】
ファイルserverを作成します.php、コードは以下の通りです.
getWSDL();
}
//SOAP Client
$server = new SoapServer(WSDL_URL);
$server->setClass('Test');
$server->handle();
/* */
class Test {
public function hello($name = '')
{
return 'Hello '.$name.'.';
}
}
serverを実行します.phpファイルは、Testを生成することができる.wsdlファイルは、次のとおりです.
hello"> helloRequest" /> domainName#Test#hello" />
helloRequest"> name" type="xsd:string" /> helloResponse"> hello" type="xsd:string" />
Test.wsdl文件中比较关键的地方用颜色标出来了,建议大家自己生成下,不要直接复制。
4、【调用接口】
创建client.php,代码如下:
1));/* soapclient */
try {
$result = $client->hello('man');/* */
var_dump($result);
} catch (SoapFault $f){
echo '__getFunctions = ';
var_dump($client->__getFunctions());
echo '
-----------------------
';
echo '__getLastRequest = '.$client->__getLastRequest();
echo '
-----------------------
';
echo '__getLastResponse = '.$client->__getLastResponse();
echo '
-----------------------
';
echo "Error Message: {$f->getMessage()}";
}
?>
完了します.