繁体   English   中英

使用PHP发出SOAP请求

[英]Make SOAP request using PHP

我已经完成了大量的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>

谢谢!

我使用下面的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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM