stefanvangastel / SoapClientCurl.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
require_once ( ‘ SoapClientCurl.php’ ); |
//Set absolute path to wsdl file |
$ wsdlfile = ‘/path/to/wsdlfile.wsdl’ ; // Edit this |
//Define options |
$ options = array ( |
‘certfile’ => $ certfile , // e.g. ‘/path/to/certfile.crt’ |
‘keyfile’ => $ keyfile , // e.g. ‘/path/to/keyfile.key’ |
‘url’ => $ url , // e.g. ‘https://webservice.example.com/’ |
‘passphrase’ => $ passphrase // e.g. ‘superSecretPassphrase’ |
); |
//Init client |
$ client = new SoapClientCurl ( $ wsdlfile , $ options ); |
//Request: |
$ client -> Method ( $ data ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
/** |
* Class SoapClientCurl extends SoapClient __doRequest method with curl powered method |
*/ |
class SoapClientCurl extends SoapClient |
//Required variables |
public $url = null; |
public $certfile = null; |
public $keyfile = null; |
public $passphrase = null; |
//Overwrite constructor and add our variables |
public function __construct($wsdl, $options = array()) |
parent::__construct($wsdl, $options); |
foreach($options as $field = > $value) |
if(!isset($this- > $field)) |
$this- > $field = $value; |
> |
> |
> |
/* |
* Overwrite __doRequest and replace with cURL. Return XML body to be parsed with SoapClient |
*/ |
public function __doRequest ($request, $location, $action, $version, $one_way = 0) |
//Basic curl setup for SOAP call |
$ch = curl_init(); |
curl_setopt($ch, CURLOPT_URL, $this- > url); //Load from datasource |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
//curl_setopt($ch, CURLOPT_HEADER, 1); |
curl_setopt($ch, CURLINFO_HEADER_OUT, 1); |
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/xml’, ‘SOAPAction: «»‘)); |
curl_setopt($ch, CURLOPT_POSTFIELDS, $request); |
curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); |
//SSL |
curl_setopt($ch, CURLOPT_SSLVERSION, 3); //=3 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
curl_setopt($ch, CURLOPT_SSLCERT, $this- > certfile); |
curl_setopt($ch, CURLOPT_SSLKEY, $this- > keyfile); |
curl_setopt($ch, CURLOPT_SSLKEYTYPE, ‘PEM’); |
curl_setopt($ch, CURLOPT_SSLKEYPASSWD, $this- > passphrase); //Load from datasource |
//Parse cURL response |
$response = curl_exec ($ch); |
$this- > curl_errorno = curl_errno($ch); |
if ($this- > curl_errorno == CURLE_OK) |
$this- > curl_statuscode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
> |
$this- > curl_errormsg = curl_error($ch); |
//Close connection |
curl_close($ch); |
//Return response info |
return $response; |
> |
> |
3 Ways to consume SOAP Web Service in PHP
Here I am going to show you how to consume SOAP web service in PHP in 3 ways. To consume SOAP webservice you must have a SOAP web service deployed onto a server. Here I am going to consume or call the SOAP service which is ready made available on the internet. I am calling the CelsiusToFahrenheit which converts temperature from celsius to fahrenheit.
You may also create your own service and write a SOAP client to consume the service. The method of consumption is similar to what I am going to show you here. The WSDL is available at the link https://www.w3schools.com/xml/tempconvert.asmx?WSDL.
Related Posts:
Prerequisites
Apache HTTP Server 2.4, PHP 7.4.3, NUSOAP Library, CURL
Consume SOAP Service
I will show you here 3 different ways of calling or consuming SOAP web service. The first method is using SoapClient, the second method is using NUSOAP library and the third method is using CURL.
Using SoapClient
Using SoapClient you do not need to use any third party library because SoapClient is already available in PHP engine.
The following PHP code using SoapClient calls the celsius to fahrenheit converter method and will give you the temperature in fahrenheit for the given input in celsius.
$wsdl = 'https://www.w3schools.com/xml/tempconvert.asmx?WSDL'; $client = new SoapClient($wsdl, array('trace'=>1)); // The trace param will show you errors $input_celsius = 36; // web service input param $request_param = array( 'Celsius' => $input_celsius ); try < $responce_param = $client->CelsiusToFahrenheit($request_param); echo $input_celsius . ' Celsius => ' . $responce_param->CelsiusToFahrenheitResult . ' Fahrenheit'; > catch (Exception $e) < echo "Exception Error
"; echo $e->getMessage(); >
Using NUSOAP
This is a third party library and using it you can consume SOAP service. The library is shipped with the source code for this example and you can download from the bottom of this tutorial in source code section.
require_once('lib/nusoap.php'); $wsdl = "https://www.w3schools.com/xml/tempconvert.asmx?WSDL"; $client = new nusoap_client($wsdl, 'wsdl'); $action = "CelsiusToFahrenheit"; // webservice method name $result = array(); $input_celsius = 36; $input = '' . $input_celsius . ' '; if (isset($action)) < $result['response'] = $client->call($action, $input); > //echo $result['response']['CelsiusToFahrenheitResult']; echo $input_celsius . ' Celsius => ' . $result['response']['CelsiusToFahrenheitResult'] . ' Fahrenheit';
Using CURL
Using CURL command you can also call SOAP service as shown in the following example.
In the following example notice I have used only web service URL instead of WSDL URL but in the above two examples I had used WSDL URL.
$webservice_url = "https://www.w3schools.com/xml/tempconvert.asmx"; $input_celsius = 36; $request_param = ' ' . $input_celsius . ' '; $headers = array( 'Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($request_param) ); $ch = curl_init($webservice_url); curl_setopt ($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt ($ch, CURLOPT_POSTFIELDS, $request_param); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $data = curl_exec ($ch); $result = $data; if ($result === FALSE) < printf("CURL error (#%d): %s
\n", curl_errno($ch), htmlspecialchars(curl_error($ch))); > curl_close ($ch); echo $input_celsius . ' Celsius => ' . $data . ' Fahrenheit';
Testing the Application
Make sure your Apache HTTP Server is up and running. Now you can execute any of the above code and for each example you will see the following example on the browser.
That’s all, hope you got an idea how to call or consume SOAP web service using PHP programming language.
PHP cURL отправить в среду WSDL SOAP
У меня никогда не было возможности отправиться в веб-службу WSDL SOAP, и я сталкиваюсь с некоторыми проблемами. Я использую PHP cURL для отправки формы в известную серверную часть, а затем во вторую службу WSDL SOAP. Первая часть работает нормально, поэтому я пропущу это. Большую часть трех дней я потратил, пытаясь найти разные решения, найденные в Интернете, и свое собственное после прочтения документации по SOAP, но безуспешно.
Вот то, что я использую, чтобы представить в WSDL
G0!=@%fut40 OFLDemo OFLUser Indirect 276 1 '.$FName.' '.$LName.' '.$Email.' '.$Phone.' Futures OFL webservice '.$hostname.' '; $headers = array( "Content-type: text/xml;charset=\"utf-8\"", "Accept: text/xml", "Cache-Control: no-cache", "Pragma: no-cache", //IS SOAPAction the same as the endpoint "$soapURL"?// "SOAPAction: https://something.com/IBWeb/IBDemoManager/IBDemoManager.asmx?wsdl", "Content-length: ".strlen($xml_post_string), ); $url2 = $soapURL; $soap_do = curl_init(); curl_setopt($soap_do, CURLOPT_URL, $url2 ); curl_setopt($soap_do, CURLOPT_HEADER, false); curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 100); curl_setopt($soap_do, CURLOPT_TIMEOUT, 100); 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, $xml_post_string); curl_setopt($soap_do, CURLOPT_HTTPHEADER, $headers); if(curl_exec($soap_do) === false) < $err = 'Curl error: ' . curl_error($soap_do); curl_close($soap_do); print $err; >else < $result = curl_exec($soap_do); echo ''; print_r($result); curl_close($soap_do); //print 'Operation completed without any errors'; >
Решение
Вот только некоторые комментарии:
Попробуйте отключить проверку SSL (только для тестирования):
curl_setopt($ch2, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, 0);
Вам следует позвонить curl_close($ch2); как в прошлом Пример:
$output2 = curl_exec($ch2); if(curl_errno($ch2)) echo curl_error($ch2); > else < echo $output2; >curl_close($ch2); //
Вы также можете попробовать Zend SOAP библиотека .
Если тебе не нравится CURL, попробуй пропивать сделать HTTP-запрос.