WSDL
WSDL (Web Services Description Language) is an XML-based language used to describe Web Services and how to access them. This document describes a Web service. It specifies the location of the service and the operations (or methods) provided by this service.
The main structure of a WSDL document is similar to this:
<definitions> <types> definition of types........ </types> <message> definition of a message.... </message> <portType> definition of a port....... </portType> <binding> definition of a binding.... </binding> </definitions>
The WSDL document can contain other elements, such as extension elements, and a service element, which can combine the definitions of several web services into a single WSDL document.
PHP generates WSDL
Class code (SoapDiscovery.class.php):
<?php class SoapDiscovery { private $class_name = ''; private $service_name = ''; /** * SoapDiscovery::__construct() SoapDiscovery class Constructor. * * @param string $class_name * @param string $service_name **/ public function __construct($class_name = '', $service_name = '') { $this->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 = "<?xml version="1.0" ?>n"; $headerWSDL.= "<definitions name="$this->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.= "<types xmlns="http://schemas.xmlsoap.org/wsdl/" />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 = '<portType name="'.$this->service_name.'Port">'; $bindingWSDL = '<binding name="'.$this->service_name.'Binding" type="tns:'.$this->service_name."Port">n<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />n"; $serviceWSDL = '<service name="'.$this->service_name."">n<documentation />n<port name="".$this->service_name.'Port" binding="tns:'.$this->service_name."Binding"><soap:address location="http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF']."" />n</port>n</service>n"; $messageWSDL = ''; foreach ($methods as $method) { if ($method->isPublic() && !$method->isConstructor()) { $portTypeWSDL.= '<operation name="'.$method->getName()."">n".'<input message="tns:'.$method->getName()."Request" />n<output message="tns:".$method->getName()."Response" />n</operation>n"; $bindingWSDL.= '<operation name="'.$method->getName()."">n".'<soap:operation soapAction="urn:'.$this->service_name.'#'.$this->class_name.'#'.$method->getName()."" />n<input><soap:body use="encoded" namespace="urn:$this->service_name" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />n</input>n<output>n<soap:body use="encoded" namespace="urn:$this->service_name" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />n</output>n</operation>n"; $messageWSDL.= '<message name="'.$method->getName()."Request">n"; $parameters = $method->getParameters(); foreach ($parameters as $parameter) { $messageWSDL.= '<part name="'.$parameter->getName()."" type="xsd:string" />n"; } $messageWSDL.= "</message>n"; $messageWSDL.= '<message name="'.$method->getName()."Response">n"; $messageWSDL.= '<part name="'.$method->getName()."" type="xsd:string" />n"; $messageWSDL.= "</message>n"; } } $portTypeWSDL.= "</portType>n"; $bindingWSDL.= "</binding>n"; return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'); } /** * SoapDiscovery::getDiscovery() Returns discovery of WSDL. * * @return string **/ public function getDiscovery() { return "<?xml version="1.0" ?>n<disco:discovery xmlns:disco="http://schemas.xmlsoap.org/disco/" xmlns:scl="http://schemas.xmlsoap.org/disco/scl/">n<scl:contractRef ref="http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF']."?wsdl" />n</disco:discovery>"; } } ?>
Usage (server server.php):
<?php define('WSDL_URL','hello.wsdl'); //定义WSDL文件路径 ini_set('soap.wsdl_cache_enabled','0'); //关闭WSDL缓存 //WSDL文件不存在时自动创建 if(!file_exists(WSDL_URL)) { require_once 'SoapDiscovery.class.php'; $disco = new SoapDiscovery('HelloWorld','IELIWB_HelloWorld'); $str = $disco->getWSDL(); file_put_contents(WSDL_URL,$str); } //SOAP开启并接收Client传入的参数响应 $server = new SoapServer(WSDL_URL); $server->setClass('HelloWorld'); $server->handle(); //测试定义公开的类 class HelloWorld { private $nombre = ''; public function __construct($name = 'World') {$this->name = $name;} public function greet($name = '') {$name = $name?$name:$this->name;return 'Hello '.$name.'.';} public function serverTimestamp() {return time();} } ?>
Client client.php:
<?php $client = new SoapClient("http://127.0.0.1/createsoap/hello.wsdl"); try { $result = $client->greet('ieliwb'); var_dump($result); echo "The answer isresult"; } catch (SoapFault $f){ echo "Error Message: {$f->getMessage()}"; } ?>
Create Webservice
1. Create wsdl
- Non-standard webservice may only be accessible by PHP
- For standard webservice, you must use wsdl (webservice description language, which is to use XML syntax standards to describe your service content. This is how I understand it)
Here I only introduce standard webservice. So how to create wsdl? This is indeed not easy for PHP. Some people say that it is very convenient to create with zend studio. This is one method. But for those who don't like to use zend studio, they may find it too difficult to create a webservice and install zend studio. I just, hehe.
Here I will introduce a simple method. Go to the Internet to download the SoapDiscovery.class.php class. There is a public method in it: getWSDL. The end of this method is return. Then, you can modify this method. This is what I do. of:
//return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'); //生成wsdl文件,将上面的return注释 $fso = fopen($this->class_name . ".wsdl" , "w"); fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'));
Now there is a class to generate wsdl, SoapDiscovery.class.php.
I only need to prepare a class or function that provides services to create wsdl. For example, I have a class: person, and the file name is: person.class.php★. There are two methods in it, one is say and the other is run. Very simple.
<?php class person { public function say() { return("i'm speaking."); } public function run() { return("i'm running,don't disturb me please."); } } ?>
There are two classes here: SoapDiscovery.class.php and person.class.php.
Start the formal generation of wsdl: create the file server.php. Copy the following content into it and run it to generate a person.wsdl file
<?php include("person.class.php"); include("SoapDiscovery.class.php"); //第一个参数是类名(生成的wsdl文件就是以它来命名的),即person类,第二个参数是服务的名字(这个可以随便写)。 $disco = new SoapDiscovery('person','Person'); $disco->getWSDL(); ?>
2. Create webservice server program
Clear the contents of the server.php file and copy the following code into it:
<?php include("person.class.php"); $objSoapServer = new SoapServer("person.wsdl");//person.wsdl是刚创建的wsdl文件 //$objSoapServer = new SoapServer("server.php?wsdl");//这样也行 $objSoapServer->setClass("person");//注册person类的所有方法 $objSoapServer->handle();//处理请求 ?>
3. Create a webservice client program to test whether the webservice is valid. The file name is: client.php
<?php $client = new SoapClient("person.wsdl"); //$client = new SoapClient("server.php?wsdl");//这样也行 echo($client->say()); echo "<br />"; echo($client->run()); echo "<br />"; ?>
OK, it’s over. If you want to use .NET, you just need to provide a URL to it.
How to get the URL: You can first search for
in the person.wsdl file. The URL here (the specific URL is based on your directory OK) is what you want to provide to .NET developers. But don’t be too happy too early. You need to add: “?wsdl” after it, http://xxxxxxxxxxxxxxxxxxxx/server.php?wsdl. This is the right way. If you don’t believe me, you can copy the url to the address bar of the browser and take a look. .After a .NET developer obtains the URL you gave him, he can add a service reference or web reference to his project, and then complete the relevant operations according to the prompts. This is very convenient for developers using .NET Simple.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment