Home
PHP
Tech Tube
MySQL
Linux
CSS&HTML
JavaScript

SOAP requests class

A simple PHP class that gives the means to send SOAP requests via cURL or PHP SoapClient.
<?php
/**
 * @package Sami's SOAP class
 * @version $Id: samis_soap.class.php v.1.0 2018-03-01 17:51:00 $
 * @author Samuil Banti
 * @copyright (C) 2018 - Samuil Banti
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
**/
class samis_soap {

    private $error = false;
    private $trace_data = false;
    
    /**
    * Send SOAP request via PHP SoapClient.
    * @param string $wsdl_file - The WSDL file.
    * @param array $soap_params - The parameters to be used as SOAP data.
    * @param string $soap_action - The SOAP method to be executed.
    * @param array $headers - Headers. Usualy used for authentication.
    * @param array $settings - The SOAP options.
    * Settings options:
    * 'trace' => true, 'local_cert' => 'Sequrity certificate', 'passphrase' => 'Certificate password', 'connection_timeout' => 3, 'verifypeer' => false, 'verifyhost' => false
    * @return mixed - The result of the request.
    */
    public function soap_request($wsdl_file, $soap_params, $soap_action, $headers = [], $settings = [])
    {
	$client = new SoapClient($wsdl_file, $settings);
	$header = new SoapHeader(WSDL, 'Auth', $headers, false);
	$client->__setSoapHeaders($header);

	try {
	    $result = $client->{$soap_action}($soap_params);
	} catch (\Exception $e) {
	    $result = false;
	    $this->error = $e->getMessage();
	}

	if ($settings->trace) {
	    $this->trace_data = array(
		'soap_params' 		=> $soap_params,
		'LastRequestHeaders' 	=> $client->__getLastRequestHeaders(),
		'LastRequest' 		=> $client->__getLastRequest(),
		'LastResponse' 		=> $client->__getLastResponse(),
	    );
	}
	return $result;
    }
    
    /**
    * Do SOAP request via cURL request.
    * Useful in case of corrupt or missing WSDL file.
    * Tools like SoapUI could be used to generate the SOAP XML (https://www.soapui.org/).
    * @param string $soap_url - The URL that should be used.
    * @param string $soap_xml - The XML of the SOAP request.
    * @param string $soap_action - The SOAP action.
    * @return mixed - The result of the request.
    */
    public function soap_curl_request($soap_url, $soap_xml, $soap_action)
    {
	$headers = [
	    'Content-type: text/xml;charset=\"utf-8\"',
	    'Accept: text/xml',
	    'Cache-Control: no-cache',
	    'Pragma: no-cache',
	    'SOAPAction: "'.$soap_action.'"',
	    'Content-length: '.strlen($soap_request),
	];

	$soap_curl = curl_init();
	curl_setopt($soap_curl, CURLOPT_URL, $soap_url);
	curl_setopt($soap_curl, CURLOPT_CONNECTTIMEOUT, 10);
	curl_setopt($soap_curl, CURLOPT_TIMEOUT, 10);
	curl_setopt($soap_curl, CURLOPT_RETURNTRANSFER, true );
	curl_setopt($soap_curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($soap_curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($soap_curl, CURLOPT_POST, true);
	curl_setopt($soap_curl, CURLOPT_HEADER, 1);
	curl_setopt($soap_curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
	curl_setopt($soap_curl, CURLOPT_POSTFIELDS, $soap_xml);
	curl_setopt($soap_curl, CURLOPT_HTTPHEADER, $headers);
	
	$this->trace_data = curl_getinfo($soap_curl, CURLINFO_HTTP_CODE);
	$result = curl_exec($soap_curl);
	
	if($result === false) {
	    $this->error = curl_error($soap_curl);
	    return false;
	}
	curl_close($soap_curl);
	return $result;
    }
    
    /**
    * Get the class error status.
    * @return mixed - The class error status. 
    */
    public function get_error()
    {
	return $this->error;
    }
    
    /**
    * Get the debug data.
    * @return mixed - Debug data. 
    */
    public function get_debug_data()
    {
	return $this->error;
    }
    
}
Download...