An entry-level introduction to phpwebservice

Source: Internet
Author: User
Tags soap client
Preface: at noon one day, I had dinner with my colleagues. during this period, we talked about the XXX game. it was undeniable that it was really commendable. then I asked JJ, how many levels do you have?

Preface: at noon one day, I had dinner with my colleagues. during this period, we talked about the XXX game. it was undeniable that it was really commendable. then I asked JJ, how many levels do you have? He went back to grade 8, and another colleague was already grade 13 (high-performance). then I am joking. JJ, you can say it in single digits, and then ask me how many levels, i'm 6, hahaha, everyone laughed... At this time, the master said, "Is it meaningful to smile in 50 steps ?", Of course, this is just a joke. Mr. Lu Xun once said that "the document is simple". from my experience in Forum creation for so long, our programmers also love it. many times, I think it is incredible, technology is a way to promote social progress and improve the living environment. everyone is a fellow. what do you think of each other?

Now, I plan to summarize the use of the soap class that comes with php.

1. obtain weather forecast data through the open webservice
Here, an open website that provides the soap webservice interface explains how to use the wsdl (what is wsdl ?) Use php to call the existing soap service!

/**
* Obtain data from the Open weather forecast service
*
**/
 
$ C = new SoapClient ('http: // www.webxml.com.cn/WebServices/WeatherWS.asmx? Wsdl ',
Array ('track' => true, 'exception' => true ));
 
// Var_dump ($ c->__ getFunctions ());
// Var_dump ($ c->__ getTypes ());
// No parameters are required.
$ Pr = $ c-> getRegionProvince ();
// Var_dump ($ pr-> getRegionProvinceResult-> string );
 
// With parameters
$ Scs = $ c-> getSupportCityString (array ('theregioncode' => 'Fujian '));
Var_dump ($ scs-> getSupportCityStringResult-> string );
 
// You can also do this.
$ We = $ c->__ call ('getwither', array ('thecitycode' => 2210 )));
Var_dump ($ we );
?> 2. how to implement soapServer and SoapClient of php
I still don't want to talk about the code. here is a simple example. Note that exceptions must be handled in actual applications. Otherwise, it is not convenient to communicate with programs other than php.
This is the server code:

$ Ss = new SoapServer (null, array ('uri '=> 'http: // zendstudio. net '));
$ Ss-> setClass ('t', 'parameter 1', 'parameter 2 ');
$ Ss-> handle ();
// $ Request = file_get_contents ('php: // input ');
// File_put_contents ('request.txt ', $ request );
Class t {
 
Function _ construct ($ p1, $ p2 ){
 
}
 
Function im8 ($ g ){
Return 'OK ----'. $ g;
}
 
}
?> Client code:

$ Ss = new SoapClient (null, array ('uri '=> 'http: // zendstudio.. Net ', 'location' => 'http: // localhost/soap. php '));
$ Header = new SoapHeader ('zendheader', 'mmcyy', '123 ');
$ Ss->__ setSoapHeaders ($ header );
Echo $ ss-> im8 ('cliently ');
 
?> 3. how to add the SoapHeader header to the soap client and server of php?
In fact, the above example has implemented adding SoapHeader to the client, while adding SoapHeader to the server is equally simple. instantiate a SoapHeader and add it using the addSoapHeader method of SoapServer, this header is generally a difficulty in learning php soap. what we usually do most is to use soap to communicate with other programs. it is not easy to use php to construct soapheader, the server that depends on this header always returns incorrect results.

4. how to obtain the SoapHeader from the php Server
This is probably one of the reasons why many people think that php does not fully support soap. Currently, the php version cannot directly obtain the SoapHeader. Therefore, I think of a stupid way to use php first: // input, get all the POST data, then use the regular expression to separate the XML part of the SoapHeader, and then sort out the desired format.

In addition, on the SoapServer side of PHP, you can obtain the "raw" communication content of soap through the $ _ SERVER and the php: // input mentioned above.

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.