📄 class.soap_server.php
字号:
<?php
/**
*
* nusoap_server allows the user to create a SOAP server
* that is capable of receiving messages and returning responses
*
* @author Dietrich Ayala <dietrich@ganx4.com>
* @author Scott Nichol <snichol@users.sourceforge.net>
* @version $Id: class.soap_server.php,v 1.58 2007/10/30 18:50:30 snichol Exp $
* @access public
*/
class nusoap_server extends nusoap_base {
/**
* HTTP headers of request
* @var array
* @access private
*/
var $headers = array();
/**
* HTTP request
* @var string
* @access private
*/
var $request = '';
/**
* SOAP headers from request (incomplete namespace resolution; special characters not escaped) (text)
* @var string
* @access public
*/
var $requestHeaders = '';
/**
* SOAP Headers from request (parsed)
* @var mixed
* @access public
*/
var $requestHeader = NULL;
/**
* SOAP body request portion (incomplete namespace resolution; special characters not escaped) (text)
* @var string
* @access public
*/
var $document = '';
/**
* SOAP payload for request (text)
* @var string
* @access public
*/
var $requestSOAP = '';
/**
* requested method namespace URI
* @var string
* @access private
*/
var $methodURI = '';
/**
* name of method requested
* @var string
* @access private
*/
var $methodname = '';
/**
* method parameters from request
* @var array
* @access private
*/
var $methodparams = array();
/**
* SOAP Action from request
* @var string
* @access private
*/
var $SOAPAction = '';
/**
* character set encoding of incoming (request) messages
* @var string
* @access public
*/
var $xml_encoding = '';
/**
* toggles whether the parser decodes element content w/ utf8_decode()
* @var boolean
* @access public
*/
var $decode_utf8 = true;
/**
* HTTP headers of response
* @var array
* @access public
*/
var $outgoing_headers = array();
/**
* HTTP response
* @var string
* @access private
*/
var $response = '';
/**
* SOAP headers for response (text or array of soapval or associative array)
* @var mixed
* @access public
*/
var $responseHeaders = '';
/**
* SOAP payload for response (text)
* @var string
* @access private
*/
var $responseSOAP = '';
/**
* method return value to place in response
* @var mixed
* @access private
*/
var $methodreturn = false;
/**
* whether $methodreturn is a string of literal XML
* @var boolean
* @access public
*/
var $methodreturnisliteralxml = false;
/**
* SOAP fault for response (or false)
* @var mixed
* @access private
*/
var $fault = false;
/**
* text indication of result (for debugging)
* @var string
* @access private
*/
var $result = 'successful';
/**
* assoc array of operations => opData; operations are added by the register()
* method or by parsing an external WSDL definition
* @var array
* @access private
*/
var $operations = array();
/**
* wsdl instance (if one)
* @var mixed
* @access private
*/
var $wsdl = false;
/**
* URL for WSDL (if one)
* @var mixed
* @access private
*/
var $externalWSDLURL = false;
/**
* whether to append debug to response as XML comment
* @var boolean
* @access public
*/
var $debug_flag = false;
/**
* constructor
* the optional parameter is a path to a WSDL file that you'd like to bind the server instance to.
*
* @param mixed $wsdl file path or URL (string), or wsdl instance (object)
* @access public
*/
function nusoap_server($wsdl=false){
parent::nusoap_base();
// turn on debugging?
global $debug;
global $HTTP_SERVER_VARS;
if (isset($_SERVER)) {
$this->debug("_SERVER is defined:");
$this->appendDebug($this->varDump($_SERVER));
} elseif (isset($HTTP_SERVER_VARS)) {
$this->debug("HTTP_SERVER_VARS is defined:");
$this->appendDebug($this->varDump($HTTP_SERVER_VARS));
} else {
$this->debug("Neither _SERVER nor HTTP_SERVER_VARS is defined.");
}
if (isset($debug)) {
$this->debug("In nusoap_server, set debug_flag=$debug based on global flag");
$this->debug_flag = $debug;
} elseif (isset($_SERVER['QUERY_STRING'])) {
$qs = explode('&', $_SERVER['QUERY_STRING']);
foreach ($qs as $v) {
if (substr($v, 0, 6) == 'debug=') {
$this->debug("In nusoap_server, set debug_flag=" . substr($v, 6) . " based on query string #1");
$this->debug_flag = substr($v, 6);
}
}
} elseif (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
$qs = explode('&', $HTTP_SERVER_VARS['QUERY_STRING']);
foreach ($qs as $v) {
if (substr($v, 0, 6) == 'debug=') {
$this->debug("In nusoap_server, set debug_flag=" . substr($v, 6) . " based on query string #2");
$this->debug_flag = substr($v, 6);
}
}
}
// wsdl
if($wsdl){
$this->debug("In nusoap_server, WSDL is specified");
if (is_object($wsdl) && (get_class($wsdl) == 'wsdl')) {
$this->wsdl = $wsdl;
$this->externalWSDLURL = $this->wsdl->wsdl;
$this->debug('Use existing wsdl instance from ' . $this->externalWSDLURL);
} else {
$this->debug('Create wsdl from ' . $wsdl);
$this->wsdl = new wsdl($wsdl);
$this->externalWSDLURL = $wsdl;
}
$this->appendDebug($this->wsdl->getDebug());
$this->wsdl->clearDebug();
if($err = $this->wsdl->getError()){
die('WSDL ERROR: '.$err);
}
}
}
/**
* processes request and returns response
*
* @param string $data usually is the value of $HTTP_RAW_POST_DATA
* @access public
*/
function service($data){
global $HTTP_SERVER_VARS;
if (isset($_SERVER['QUERY_STRING'])) {
$qs = $_SERVER['QUERY_STRING'];
} elseif (isset($HTTP_SERVER_VARS['QUERY_STRING'])) {
$qs = $HTTP_SERVER_VARS['QUERY_STRING'];
} else {
$qs = '';
}
$this->debug("In service, query string=$qs");
if (ereg('wsdl', $qs) ){
$this->debug("In service, this is a request for WSDL");
if($this->externalWSDLURL){
if (strpos($this->externalWSDLURL,"://")!==false) { // assume URL
header('Location: '.$this->externalWSDLURL);
} else { // assume file
header("Content-Type: text/xml\r\n");
$fp = fopen($this->externalWSDLURL, 'r');
fpassthru($fp);
}
} elseif ($this->wsdl) {
header("Content-Type: text/xml; charset=ISO-8859-1\r\n");
print $this->wsdl->serialize($this->debug_flag);
if ($this->debug_flag) {
$this->debug('wsdl:');
$this->appendDebug($this->varDump($this->wsdl));
print $this->getDebugAsXMLComment();
}
} else {
header("Content-Type: text/html; charset=ISO-8859-1\r\n");
print "This service does not provide WSDL";
}
} elseif ($data == '' && $this->wsdl) {
$this->debug("In service, there is no data, so return Web description");
print $this->wsdl->webDescription();
} else {
$this->debug("In service, invoke the request");
$this->parse_request($data);
if (! $this->fault) {
$this->invoke_method();
}
if (! $this->fault) {
$this->serialize_return();
}
$this->send_response();
}
}
/**
* parses HTTP request headers.
*
* The following fields are set by this function (when successful)
*
* headers
* request
* xml_encoding
* SOAPAction
*
* @access private
*/
function parse_http_headers() {
global $HTTP_SERVER_VARS;
$this->request = '';
$this->SOAPAction = '';
if(function_exists('getallheaders')){
$this->debug("In parse_http_headers, use getallheaders");
$headers = getallheaders();
foreach($headers as $k=>$v){
$k = strtolower($k);
$this->headers[$k] = $v;
$this->request .= "$k: $v\r\n";
$this->debug("$k: $v");
}
// get SOAPAction header
if(isset($this->headers['soapaction'])){
$this->SOAPAction = str_replace('"','',$this->headers['soapaction']);
}
// get the character encoding of the incoming request
if(isset($this->headers['content-type']) && strpos($this->headers['content-type'],'=')){
$enc = str_replace('"','',substr(strstr($this->headers["content-type"],'='),1));
if(eregi('^(ISO-8859-1|US-ASCII|UTF-8)$',$enc)){
$this->xml_encoding = strtoupper($enc);
} else {
$this->xml_encoding = 'US-ASCII';
}
} else {
// should be US-ASCII for HTTP 1.0 or ISO-8859-1 for HTTP 1.1
$this->xml_encoding = 'ISO-8859-1';
}
} elseif(isset($_SERVER) && is_array($_SERVER)){
$this->debug("In parse_http_headers, use _SERVER");
foreach ($_SERVER as $k => $v) {
if (substr($k, 0, 5) == 'HTTP_') {
$k = str_replace(' ', '-', strtolower(str_replace('_', ' ', substr($k, 5))));
} else {
$k = str_replace(' ', '-', strtolower(str_replace('_', ' ', $k)));
}
if ($k == 'soapaction') {
// get SOAPAction header
$k = 'SOAPAction';
$v = str_replace('"', '', $v);
$v = str_replace('\\', '', $v);
$this->SOAPAction = $v;
} else if ($k == 'content-type') {
// get the character encoding of the incoming request
if (strpos($v, '=')) {
$enc = substr(strstr($v, '='), 1);
$enc = str_replace('"', '', $enc);
$enc = str_replace('\\', '', $enc);
if (eregi('^(ISO-8859-1|US-ASCII|UTF-8)$', $enc)) {
$this->xml_encoding = strtoupper($enc);
} else {
$this->xml_encoding = 'US-ASCII';
}
} else {
// should be US-ASCII for HTTP 1.0 or ISO-8859-1 for HTTP 1.1
$this->xml_encoding = 'ISO-8859-1';
}
}
$this->headers[$k] = $v;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -