There are a lot of posts on the web about how to invoke Soap/webservice in PHP. But the main story is how to use PHP to develop the server-side, the client and associated with, and rarely touch the call of the existing webservice in PHP. In this article we do a simple demonstration.
First, looking for webservice source
WebService can write on its own, but it can also look for ready-made networks. I'm using the US Zip Validator in www.xmethods.net. Its WSDL file location is in: Http://www.webservicemart.com/uszip.asmx?WSDL. Its role is based on the input ZIP code, return the code corresponding to the United States place names, state names, latitude and longitude and so on.
Ii. creation of SoapClient
The second step is to create the soapclient and call the method in WebService and get the return value. The PHP code is as follows:
$objSoapClient New SoapClient ("Http://www.webservicemart.com/uszip.asmx?WSDL"); $param=array("ZipCode" = =$zip); $out=$objSoapClient->validatezip ($param); $data=$out
There are many ways to create soapclient, and we use the most standard (and simplest) WSDL method. Since the method of querying the zip must require a parameter, we must create an array to be assigned with "parameter name = = value".
Perhaps the reader will have some interest in the creation of this array. For example, how do we know that "parameter name" should be "ZipCode" instead of something else? Why are there no more parameters, and only one? OK, we'll explain the problem later. Because it involves the interpretation of WSDL.
After creating the parameters, again, we call the SoapClient method Validatezip and pass the parameters in; for the returned result, we use the $data variable to take out what we are really interested in. Similarly, there are problems with how the method name is determined. We'll introduce you later.
If you also use phped for PHP development and debugging, then from the Debug window below, you can clear the relationship between $data and $out:
Third, analytical data
The data in the $data obtained above is the standard XML structure of the data. So in PHP, we need to create an XML parser to parse this data. The code is as follows:
$ParsedData=Array(); functionStartelement ($parser,$name,$attribs) { Global $ParsedData; Echo"<<font color="#0000cc "> $name </font>"; if(Count($attribs)) {foreach($attribs as $k=$v){
$ParsedData[$k]=$v; Echo"<font color="#009900 "> $k </font>=" <font color= "#990000" > $v </font> ""; }}Echo">"; } functionEndElement ($parser,$name) {Echo"</<font color="#0000cc "> $name </font>>"; } $xml _parser=xml_parser_create(); xml_parser_set_option($xml _parser, xml_option_case_folding, 1); Xml_set_element_handler($xml _parser, "Startelement", "EndElement"); Echo"<pre>"; if(!Xml_parse($xml _parser,$data)) { die(sprintf("XML error:%s at line%d",xml_error_string(Xml_get_error_code($xml _parser)),Xml_get_current_line_number($xml _parser))); } Echo"</pre>"; Xml_parser_free($xml _parser);
The detailed operations here refer to the chapter on XML functions in the PHP function manual. Don't repeat it here. Once the data has been parsed successfully, we can do further processing. For example, the following code iterates over the array and then outputs:
foreach ($ParsedDataas$k=$v) { echo$k. " = ". $v. " <br/> "; }
Iv. Interpretation of WSDL
We left two questions: How do I know the method provided by a webservice, and its parameters? All the answers are in the WSDL description. For the WSDL used in this article, we take a section to analyze. Since we're calling through soap, I've made an excerpt of the full WSDL, listing only the part about the SOAP call (the inverted display):
First we notice that <wsdl:message name= "Validatezipsoapin" > This section, which points out that in a SOAP call, the entry parameter should refer to Validatezip, so we go to the point above the file, See the definition of the Validatezip method:
<s:elementname= "Validatezip">
<S:complextype>
<s:sequence>
<s:elementminOccurs= "0"maxOccurs= "1"name= "ZipCode"type= "S:string"/>
</s:sequence>
</S:complextype>
</s:element>
Obviously, Validatezip requires a parameter with a name of ZipCode and a type of string.
Again, we look at <wsdl:message name= "Validatezipsoapout" > This section, which states that the export parameter of the SOAP call is Validatezipresponse. The outgoing parameter name of the latter is validatezipresult. We then explained the questions raised in the first two sections: