TimNguyenBSM
TimNguyenBSM

Reputation: 827

Make SOAP request using PHP

I've done plenty of REST integrations, but have zero experience with SOAP. Here's a sample SOAP v1.1 request... how do I execute this in PHP? Furthermore, we are given an option of using SOAP v1.1 or v1.2 - which should I use?

POST /l/webservice/employee.asmx HTTP/1.1
Host: webservices.domain.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://www.domain.com/l/webservices/ExportEmployeeInformation"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ExportEmployeeInformation xmlns="http://www.domain.com/l/webservices/">
      <sTicket>string</sTicket>
    </ExportEmployeeInformation>
  </soap:Body>
</soap:Envelope>

Here's the SOAP v1.2 sample request:

POST /l/webservice/employee.asmx HTTP/1.1
Host: webservices.domain.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <ExportEmployeeInformation xmlns="http://www.domain.com/l/webservices/">
      <sTicket>string</sTicket>
    </ExportEmployeeInformation>
  </soap12:Body>
</soap12:Envelope>

Thanks!

Upvotes: 0

Views: 70

Answers (1)

Anderson Ferreira
Anderson Ferreira

Reputation: 122

I connect with a SOAP webservice using the PHP function below. I hope it helps.

public $credentials = array('login'=>'my_login', 'pass'=>'my_pass');

/**
 * @param string $url URL E.g.: http://domain.com/webservice/page.asmx
 * @param string $method E.g.: findEmployees
 * @param string $parameters E.g.: array('employed_id'=>100)
 * @return stdClass|SoapFault
 */
public function soapFunction($url = null, $method = null, $parameters = array(), $debug = false) {
    $configs = array(
        'soap_version' => SOAP_1_2,
        'cache_wsdl' => WSDL_CACHE_NONE,
        'exceptions' => false,
        'compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP
    );
    if($debug) $configs['trace'] = true;

    if(substr($url, -5) != '?WSDL') $url.= '?WSDL';
    @$webService = new SoapClient($url, $configs);


    $parameters = array_merge($parameters, array('credential'=>$this->credentials));
    $response = $webService->__soapCall($method, array($method=>$parameters));

    if($debug) { // Return debug in XML
        header('Content-type: text/xml');
        echo $webService->__getLastRequest();
        exit();
    }
    else return $response;
}

Upvotes: 1

Related Questions