Background
This article demonstrates how to create an SOAP client using PHP and consume a SOAP-based web service
[wp_ad_camp_1]
Hardware Environment
n/a
Software Environment
- Windows 7 Professional SP1
- PHP 5.5.9 / Zend Engine v2.5.0
Find a free Web Service Online
First, find a free web service online to test. For this post, the StockQuote service is used which is available on www.webservicex.net.
Open the WSDL on the browser.
[wp_ad_camp_2]
A new tab will open showing the content of WSDL we chose.
[wp_ad_camp_3]
Next, we look for a SOAP-based operation and look at its input and output messages. There are 3 operations in this WSDL but we are only interested “GetQuote” under the “StockQuoteSoap” portType.
GetQuote operation
Using PHP
[wp_ad_camp_4]
Then, we look for the element “GetOute” used in the “GetQuoteSoapIn”. It represents the “parameters” that we need to pass to get some meaningful output. Also, take note of the “GetQuoteResult” within the “GetQuoteResponse” element. We will use that to read the value from the web service response in PHP.
With those information, we can know code our PHP client to execute a SOAP operation.
1 2 3 4 5 6 7 8 9 10 11 12 | try { $client = new SoapClient('http://www.webservicex.net/stockquote.asmx?WSDL'); $val = array('symbol' =-->'ORCL'); $results = $client->GetQuote($val); /* GetQuoteResult is from the GetQuoteResponse element in the WSDL */ $output = $results->GetQuoteResult; echo $output; } catch (SoapFault $e) { echo $e->getMessage(); } |
Sample Output
[wp_ad_camp_5]