使用PHP发出SOAP请求

时间:2016-02-10 02:28:48

标签: php xml web-services api soap

我已经完成了大量的REST集成,但对SOAP没有任何经验。这是一个示例SOAP v1.1请求...如何在PHP中执行此操作?此外,我们可以选择使用SOAP v1.1或v1.2 - 我应该使用哪些?

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>

这是SOAP v1.2示例请求:

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>

谢谢!

1 个答案:

答案 0 :(得分:1)

我使用下面的PHP函数连接SOAP Web服务。我希望它有所帮助。

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;
}