What can WebServices do?
WebServices can convert applications into web applications.
By using WebServices, your application can publish information to the world, or provide a certain function.
Okay, there is a lot of information about WebServices online, so I won’t introduce too much and go directly to the topic.
PHP has two extension libraries that can implement WebServices, one is NuSoap, and the other is the official Soap extension that comes with PHP. They are roughly the same in use. Take the official Soap extension as an example.
The three classes SoapClient, SoapServer, and SoapFault are mainly used in writing WebServices in Soap.
SoapClient: The class that users access, that is, the client, uses WebServices classes
SoapServer: Provides WebServices class, server side
SoapFault: Exception handling class
As an example, let’s write a super simple WebServices to get a feel for it, let’s talk directly in code
1. Myself.class.php --Business logic class, function implementation class
<span>1</span> <?<span>php </span><span>2</span> <span>//</span><span>业务逻辑类</span> <span>3</span> <span>class</span><span> Myself{ </span><span>4</span> <span>public</span> <span>function</span><span> info(){ </span><span>5</span> <span>return</span> "新浪微博:Balla_兔子,求关注啦~"<span>; </span><span>6</span> <span> } </span><span>7</span> <span> } </span><span>8</span> ?>
It’s very simple, no need for extra explanation, it returns a string.
2. soapServer.php - Server class, providing services
<span> 1</span> <?<span>php </span><span> 2</span> <span>//</span><span>服务端</span> <span> 3</span> <span>require_once</span>('Myself.class.php'<span>); </span><span> 4</span> <span>$parameter</span>=<span>array</span><span>( </span><span> 5</span> 'uri'=>'http://localhost/', <span> 6</span> 'location'=>'http://localhost/soap/soapServer.php' <span> 7</span> <span> ); </span><span> 8</span> <span>$soapServer</span>=<span>new</span> SoapServer(<span>null</span>,<span>$parameter</span><span>); </span><span> 9</span> <span>$soapServer</span>->setClass('Myself'<span>); </span><span>10</span> <span>$soapServer</span>-><span>handle(); </span><span>11</span> ?>
SoapServer has two operating modes:
The above example is the non-WSDL mode. When instantiating the SoapServer class, a parameter is to put the WSDL file. In the non-WSDL mode, it can be Empty, writes the configuration parameters in the form of an array in the second parameter .
If you are using the WSDL mode, you can directly use the WSDL file to let the server read the configuration parameters. In this case, can omit the second array parameter .
There are many configuration parameters, and only 2 are listed above for simple examples. You can check the details online
uri --namespace
location --service address
1. WSDL mode In WSDL mode, the constructor can use the WSDL file name as a parameter and extract the information used by the service from the WSDL.
2. Non-WSDL mode In the non-WSDL mode, parameters are used to pass the information to be used to manage the behavior of the service.
Among the many methods of the SoapServer class, three methods are more important. They are SoapServer::setClass(), SoapServer::addFunction(), SoapServer::handle().
Special attention should be paid to the fact that no parameters can be output before or after the handle method, otherwise an error will occur.
3. soapClient.php --Client class, using service
<span> 1</span> <?<span>php </span><span> 2</span> <span>//</span><span>客户端</span> <span> 3</span> <span>$parameter</span>=<span>array</span><span>( </span><span> 4</span> 'uri'=>'http://localhost/', <span> 5</span> 'location'=>'http://localhost/soap/soapServer.php' <span> 6</span> <span> ); </span><span> 7</span> <span>try</span><span>{ </span><span> 8</span> <span>$soapClient</span>=<span>new</span> SoapClient(<span>null</span>,<span>$parameter</span><span>); </span><span> 9</span> <span>echo</span> <span>$soapClient</span>-><span>info(); </span><span>10</span> <span>11</span> }<span>catch</span>(<span>Exception</span> <span>$e</span><span>){ </span><span>12</span> <span>echo</span> <span>$e</span>-><span>getMessage(); </span><span>13</span> <span> } </span><span>14</span> <span>15</span> ?>
The SoapClient class can serve as a client for given WebServices.
It has two modes of operation: (similar to the two modes of SoapServer)
1. WSDL mode
2. Non-WSDL mode
The above is a simple WebServices implemented using PHP SOAP extension. Let’s try it by visiting soapClient.php
Easy to do~
Attachment:
The return value of most WebServices interfaces now is JSON or XML, and we can define the assignment internally.
Commonly used functions file_get_contents, json_encode, json_decode, etc.
Here is an article about things to pay attention to when operating json: http://www.cnblogs.com/lichenwei/p/3888586.html
Just read the file in binary and pass it as an xml entity. View original post>>
baike.baidu.com/view/2099744.htm