PHP soap WebService is successfully called.

Source: Internet
Author: User
The class used is soapclient and PhP5 comes with it. Of course, you can also use nusoap. php, a class written in pure PHP. Code The number of lines is more than 7 kb, which is definitely less efficient than soapclient.

Here we will call http://www.webxml.com.cn/webservices/chinazipsearchwebservice.asmx.
Call the getsupportcity method as an example.
Http://www.webxml.com.cn/WebServices/ChinaZipSearchWebService.asmx? OP = getsupportcity
Only one optional parameter byprovincename is required, and the returned value is a string array. This is better.

Header ("Content-Type: text/html; charset = UTF-8 ");
$ Client = new soapclient ("http://www.webxml.com.cn/WebServices/ChinaZipSearchWebService.asmx? WSDL ");
// Call the _ call method. The first parameter is the WebService method name, and the second parameter is the method parameter. Here, the byprovincename value is not passed. If you want to pass it, it is:
// $ P = $ client->__ call ('getortortprovince ', array ('byprovincename' => 'region name '));
$ P = $ client->__ call ('getortortprovince ', array ());
Print_r ($ P); // output the variable $ P first to see what type it is.

Stdclass object
(
[Getsupportprovinceresult] => stdclass object
(
[String] => Array
(
[0] => Anhui
[1] => Beijing
[2] => Fujian
[3] => Gansu
[4] => Guangdong
[5] => Guangxi
[6] => Guizhou
[7] => Hainan
[8] => Hebei
[9] => Henan
[10] => Heilongjiang
[11] => Hubei
[12] => Hunan
[13] => Jilin
[14] => Jiangsu
[15] => Jiangxi
[16] => Liaoning
[17] => Inner Mongolia
[18] => Ningxia
[19] => Qinghai
[20] => Shandong
[21] => Shanxi
[22] => Shaanxi
[23] => Shanghai
[24] => Sichuan
[25] => Tianjin
[26] => Tibet
[27] => Xinjiang
[28] => Yunnan
[29] => Zhejiang
[30] => Chongqing
)

)

) We can see that the returned object is an object named stdclass with the property getsupportprovinceresult. The String Array under the Property getsupportprovinceresult is the result we need.

header ("Content-Type: text/html; charset = UTF-8");
$ client = new soapclient ("http://www.webxml.com.cn/WebServices/ChinaZipSearchWebService.asmx? WSDL ");
$ P = $ client->__ call ('getsupportprovince ', array ());
$ P = $ p-> getsupportprovinceresult-> string;
print_r ($ P);
unset ($ P);
?>

// output

Array
(
[0] => Anhui
[1] => Beijing
[2] => Fujian
[3] => Gansu
[4] => Guangdong
[5] => Guangxi
[6] => Guizhou
[7] => Hainan
[8] => Hebei
[9] => Henan
[10] => Heilongjiang
[11] => Hubei
[12] => Hunan
[13] => Jilin
[14] => Jiangsu
[15] => Jiangxi
[16] => Liaoning
[17] => Inner Mongolia
[18] => Ningxia
[19] => Qinghai
[20] => Shandong
[21] => Shanxi
[22] => Shaanxi
[23] => Shanghai
[24] => Sichuan
[25] => Tianjin
[26] => Tibet
[27] => Xinjiang
[28] => Yunnan
[29] => Zhejiang
[30] => Chongqing
) Google has not been successful for a long time. The above is my one-sided understanding and understanding.
However, we do not know how to process data with the returned data type dataset. For example, getaddressbyzipcode.

Continued:

The previous article mentioned how to call a WebService and obtain its return value.
Http://www.yibin001.com/Archives/soapclient.aspx
Yesterday, I encountered some problems when calling the WebService on the server side as Asp.net. Here I want to help my friends who encountered the same problem.
Specific performance:
The client uses soapclient to initiate a request and PASS Parameters normally. However, WebService cannot correctly obtain the parameter value. Each value is null!
We usually do this when passing parameters:

$ Client = new soapclient ('HTTP: // xxxxxxxxxxx. asmx? WSDL ');
$ Res = $ client->__ call ('getweatherbycityname', array ('parameter 1' => 'value', 'parameter 2' => 'value '));

However, for ws compiled using. net, the above Code is a little powerless and parameter passing fails.
I had a long head when I encountered this problem yesterday and then found a solution.

If your service is a. Net DOC/parameters, which means the input message has a single part named 'parameters 'That is a structure that wraps the parameters.
Your call shoshould look like this:
<? PHP
$ Params = array ('Param _ name_1 '=> $ val_1, 'Param _ name_2' => $ val_2 );
$ Client-> call ('methodname', array ('parameters '=> $ Params ));
?>

Original article: http://cn.php.net/manual/en/function.soap-soapclient-soapcall.php
Knowing the cause will solve the problem.
Looking back at the example below, it's quite easy.

header ('content-type: text/html; charset = UTF-8 ');
require_once 'city. PHP '; // The cache file for city and city code is also obtained from webxml.
if ($ _ post ['submit '] ===' OK ')
{< br> $ client = new soapclient ('HTTP: // www.webxml.com.cn/webservices/weatherwebservice.asmx? WSDL ');
$ code =$ _ post ['city'];
$ para = array ('thecityname' => $ Code ); // getweatherbycityname only requires one parameter. The parameter name thecityname
// each time a method is called, a corresponding response is returned. The result name is: method name + result, the following getweatherbycitynameresult returns an object
$ res = $ client->__ call ('getweatherbycityname', array ('paramters '=> $ para )) -> getweatherbycitynameresult-> string;
echo "

"; 
echo "City :". $ res [1];
echo "
temperature :". $ res [5];
EC Ho "
weather :". $ res [6];
echo "
Wind Power :". $ res [7];
echo "
";
}< BR >?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.