How to send SOAP request from PHP

Does anyone know how I can send a SOAP request from PHP?

+42
soap php web-services
Jan 22 '09 at 22:32
source share
6 answers

In my experience, this is not so simple. The built-in PHP SOAP client did not work with the .NET SOAP server that we had to use. He complained about the wrong definition of the scheme. Although the .NET client worked with this server just fine. By the way, let me say that SOAP compatibility is a myth.

The next step was NuSOAP . This has worked for quite some time. By the way, for God's sake, don't forget to cache WSDL! But even with the help of cached users, WSDL complained that it was pretty darn slow.

Then we decided to deal with HTTP, collect the requests and read the answers using SimpleXMLElemnt , for example:

 $request_info = array(); $full_response = @http_post_data( 'http://example.com/OTA_WS.asmx', $REQUEST_BODY, array( 'headers' => array( 'Content-Type' => 'text/xml; charset=UTF-8', 'SOAPAction' => 'HotelAvail', ), 'timeout' => 60, ), $request_info ); $response_xml = new SimpleXMLElement(strstr($full_response, '<?xml')); foreach ($response_xml->xpath('//@HotelName') as $HotelName) { echo strval($HotelName) . "\n"; } 

Note that in PHP 5.2 you will need pecl_http, since (surprise-surprise!) There is no HTTP client.

Switching to bare HTTP helped us get over 30% of SOAP requests. And from now on, we redirect all performance complaints to the server guys.

In the end, I would recommend this latter approach, and not because of performance. I think that in general, in a dynamic language such as PHP, there is no benefit from all this WSDL / type-control. You don't need a fantastic library for reading and writing XML, with all this stub creation and dynamic proxies. Your language is already dynamic, and SimpleXMLElement works just fine and is so easy to use. In addition, you will have less code, which is always good.

+34
Jan 14 '10 at
source share

PHP supports SOAP. Just call

 $client = new SoapClient($url); 

to connect to SoapServer, and then you can get a list of call functions and functions by simply doing ...

 $client->__getTypes(); $client->__getFunctions(); $result = $client->functionName(); 

for more http://www.php.net/manual/en/soapclient.soapclient.php

+24
Nov 15 '10 at 1:08
source share

I needed to make very simple XML queries, and after reading @Ivan Krechetov's comment on SOAP fast hit, I tried its code and found that http_post_data () was not built into PHP 5.2. I don’t really want to install it, I tried cURL, which is located on all my servers. Although I don't know how fast cURL compares to SOAP, it would be easy to do what I needed. Below is a sample with cURL for those who need it.

 $xml_data = '<?xml version="1.0" encoding="UTF-8" ?> <priceRequest><customerNo>123</customerNo><password>abc</password><skuList><SKU>99999</SKU><lineNumber>1</lineNumber></skuList></priceRequest>'; $URL = "https://test.testserver.com/PriceAvailability"; $ch = curl_init($URL); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml')); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); curl_close($ch); print_r($output); 
+7
Apr 15 '13 at 21:24
source share

You might want to look here and here .

A small code example from the first link:

 <?php // include the SOAP classes require_once('nusoap.php'); // define parameter array (ISBN number) $param = array('isbn'=>'0385503954'); // define path to server application $serverpath ='http://services.xmethods.net:80/soap/servlet/rpcrouter'; //define method namespace $namespace="urn:xmethods-BNPriceCheck"; // create client object $client = new soapclient($serverpath); // make the call $price = $client->call('getPrice',$param,$namespace); // if a fault occurred, output error info if (isset($fault)) { print "Error: ". $fault; } else if ($price == -1) { print "The book is not in the database."; } else { // otherwise output the result print "The price of book number ". $param[isbn] ." is $". $price; } // kill object unset($client); ?> 
+3
Jan 22 '09 at 22:36
source share

Below is a brief example of how to do this (which best explains the question to me), which I essentially found on this website . This link to the site also explains WSDL, which is important for working with SOAP services.

However, I don’t think the API they used in the example below is still working, so just switch to one of your own options.

 $wsdl = 'http://terraservice.net/TerraService.asmx?WSDL'; $trace = true; $exceptions = false; $xml_array['placeName'] = 'Pomona'; $xml_array['MaxItems'] = 3; $xml_array['imagePresence'] = true; $client = new SoapClient($wsdl, array('trace' => $trace, 'exceptions' => $exceptions)); $response = $client->GetPlaceList($xml_array); var_dump($response); 
+2
Oct 23 '15 at 10:20
source share

We can use the PHP cURL library to generate a simple HTTP POST request. The following example shows how to create a simple SOAP request using cURL.

Create soap-server.php that writes the SOAP request to soap-request.xml to the web folder.

 We can use the PHP cURL library to generate simple HTTP POST request. The following example shows you how to create a simple SOAP request using cURL. Create the soap-server.php which write the SOAP request into soap-request.xml in web folder. <?php $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; $f = fopen("./soap-request.xml", "w"); fwrite($f, $HTTP_RAW_POST_DATA); fclose($f); ?> The next step is creating the soap-client.php which generate the SOAP request using the cURL library and send it to the soap-server.php URL. <?php $soap_request = "<?xml version=\"1.0\"?>\n"; $soap_request .= "<soap:Envelope xmlns:soap=\"http://www.w3.org/2001/12/soap-envelope\" soap:encodingStyle=\"http://www.w3.org/2001/12/soap-encoding\">\n"; $soap_request .= " <soap:Body xmlns:m=\"http://www.example.org/stock\">\n"; $soap_request .= " <m:GetStockPrice>\n"; $soap_request .= " <m:StockName>IBM</m:StockName>\n"; $soap_request .= " </m:GetStockPrice>\n"; $soap_request .= " </soap:Body>\n"; $soap_request .= "</soap:Envelope>"; $header = array( "Content-type: text/xml;charset=\"utf-8\"", "Accept: text/xml", "Cache-Control: no-cache", "Pragma: no-cache", "SOAPAction: \"run\"", "Content-length: ".strlen($soap_request), ); $soap_do = curl_init(); curl_setopt($soap_do, CURLOPT_URL, "http://localhost/php-soap-curl/soap-server.php" ); curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($soap_do, CURLOPT_TIMEOUT, 10); curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true ); curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($soap_do, CURLOPT_POST, true ); curl_setopt($soap_do, CURLOPT_POSTFIELDS, $soap_request); curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header); if(curl_exec($soap_do) === false) { $err = 'Curl error: ' . curl_error($soap_do); curl_close($soap_do); print $err; } else { curl_close($soap_do); print 'Operation completed without any errors'; } ?> Enter the soap-client.php URL in browser to send the SOAP message. If success, Operation completed without any errors will be shown and the soap-request.xml will be created. <?xml version="1.0"?> <soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soap:Body xmlns:m="http://www.example.org/stock"> <m:GetStockPrice> <m:StockName>IBM</m:StockName> </m:GetStockPrice> </soap:Body> </soap:Envelope> Original - http://eureka.ykyuen.info/2011/05/05/php-send-a-soap-request-by-curl/ 
+2
Feb 17 '16 at 19:38
source share



All Articles