📄 class.soapclient.php
字号:
<?php
class soapclient extends nusoap_base
{
var $username = "";
var $password = "";
var $authtype = "";
var $requestHeaders = false;
var $responseHeaders = "";
var $document = "";
var $endpoint;
var $error_str = false;
var $proxyhost = "";
var $proxyport = "";
var $proxyusername = "";
var $proxypassword = "";
var $xml_encoding = "";
var $http_encoding = false;
var $timeout = 0;
var $response_timeout = 30;
var $endpointType = "";
var $persistentConnection = false;
var $defaultRpcParams = false;
var $request = "";
var $response = "";
var $responseData = "";
var $decode_utf8 = true;
var $fault;
var $faultcode;
var $faultstring;
var $faultdetail;
function soapclient( $endpoint, $wsdl = false, $proxyhost = false, $proxyport = false, $proxyusername = false, $proxypassword = false, $timeout = 0, $response_timeout = 30 )
{
$this->endpoint = $endpoint;
$this->proxyhost = $proxyhost;
$this->proxyport = $proxyport;
$this->proxyusername = $proxyusername;
$this->proxypassword = $proxypassword;
$this->timeout = $timeout;
$this->response_timeout = $response_timeout;
if ( $wsdl )
{
$this->endpointType = "wsdl";
if ( is_object( $endpoint ) && is_a( $endpoint, "wsdl" ) )
{
$this->wsdl = $endpoint;
$this->endpoint = $this->wsdl->wsdl;
$this->wsdlFile = $this->endpoint;
$this->debug( "existing wsdl instance created from ".$this->endpoint );
}
else
{
$this->wsdlFile = $this->endpoint;
$this->debug( "instantiating wsdl class with doc: ".$endpoint );
$this->wsdl =& new wsdl( $this->wsdlFile, $this->proxyhost, $this->proxyport, $this->proxyusername, $this->proxypassword, $this->timeout, $this->response_timeout );
}
$this->debug( "wsdl debug...\n".$this->wsdl->debug_str );
$this->wsdl->debug_str = "";
if ( $errstr = $this->wsdl->geterror( ) )
{
$this->debug( "got wsdl error: ".$errstr );
$this->seterror( "wsdl error: ".$errstr );
}
else if ( $this->operations = $this->wsdl->getoperations( ) )
{
$this->debug( "got ".count( $this->operations )." operations from wsdl ".$this->wsdlFile );
}
else
{
$this->debug( "getOperations returned false" );
$this->seterror( "no operations defined in the WSDL document!" );
}
}
}
function call( $operation, $params = array( ), $namespace = "", $soapAction = "", $headers = false, $rpcParams = null, $style = "rpc", $use = "encoded" )
{
$this->operation = $operation;
$this->fault = false;
$this->error_str = "";
$this->request = "";
$this->response = "";
$this->responseData = "";
$this->faultstring = "";
$this->faultcode = "";
$this->opData = array( );
$this->debug( "call: {$operation}, {$params}, {$namespace}, {$soapAction}, {$headers}, {$style}, {$use}; endpointType: {$this->endpointType}" );
if ( $headers )
{
$this->requestHeaders = $headers;
}
if ( $this->endpointType == "wsdl" && ( $opData = $this->getoperationdata( $operation ) ) )
{
$this->opData = $opData;
foreach ( $opData as $key => $value )
{
$this->debug( "{$key} -> {$value}" );
}
if ( isset( $opData['soapAction'] ) )
{
$soapAction = $opData['soapAction'];
}
$this->endpoint = $opData['endpoint'];
$namespace = isset( $opData['input']['namespace'] ) ? $opData['input']['namespace'] : $namespace != "" ? $namespace : "http://testuri.org";
$style = $opData['style'];
$use = $opData['input']['use'];
if ( $namespace != "" && !isset( $this->wsdl->namespaces[$namespace] ) )
{
$this->wsdl->namespaces['nu'] = $namespace;
}
$nsPrefix = $this->wsdl->getprefixfromnamespace( $namespace );
if ( is_string( $params ) )
{
$this->debug( "serializing param string for WSDL operation {$operation}" );
$payload = $params;
}
else if ( is_array( $params ) )
{
$this->debug( "serializing param array for WSDL operation {$operation}" );
$payload = $this->wsdl->serializerpcparameters( $operation, "input", $params );
}
else
{
$this->debug( "params must be array or string" );
$this->seterror( "params must be array or string" );
return false;
}
$usedNamespaces = $this->wsdl->usedNamespaces;
$encodingStyle = "http://schemas.xmlsoap.org/soap/encoding/";
if ( isset( $opData['output']['encodingStyle'] ) && $encodingStyle != $opData['output']['encodingStyle'] )
{
$methodEncodingStyle = " SOAP-ENV:encodingStyle=\"".$opData['output']['encodingStyle']."\"";
}
else
{
$methodEncodingStyle = "";
}
$this->debug( "wsdl debug: \n".$this->wsdl->debug_str );
$this->wsdl->debug_str = "";
if ( $errstr = $this->wsdl->geterror( ) )
{
$this->debug( "got wsdl error: ".$errstr );
$this->seterror( "wsdl error: ".$errstr );
return false;
}
}
else
{
if ( $this->endpointType == "wsdl" )
{
$this->seterror( "operation ".$operation." not present." );
$this->debug( "operation '{$operation}' not present." );
$this->debug( "wsdl debug: \n".$this->wsdl->debug_str );
$this->wsdl->debug_str = "";
return false;
}
else
{
if ( $namespace == "" )
{
$namespace = "http://testuri.org";
}
$nsPrefix = "ns1";
$payload = "";
if ( is_string( $params ) )
{
$this->debug( "serializing param string for operation {$operation}" );
$payload = $params;
}
else if ( is_array( $params ) )
{
$this->debug( "serializing param array for operation {$operation}" );
foreach ( $params as $k => $v )
{
$payload .= $this->serialize_val( $v, $k, false, false, false, false, $use );
}
}
else
{
$this->debug( "params must be array or string" );
$this->seterror( "params must be array or string" );
return false;
}
$usedNamespaces = array( );
$methodEncodingStyle = "";
}
}
if ( $style == "rpc" )
{
if ( $use == "literal" )
{
$this->debug( "wrapping RPC request with literal method element" );
$payload = "<{$operation} xmlns=\"{$namespace}\">".$payload."</{$operation}>";
}
else
{
$this->debug( "wrapping RPC request with encoded method element" );
$payload = "<{$nsPrefix}:{$operation}{$methodEncodingStyle} xmlns:{$nsPrefix}=\"{$namespace}\">".$payload."</{$nsPrefix}:{$operation}>";
}
}
$soapmsg = $this->serializeenvelope( $payload, $this->requestHeaders, $usedNamespaces, $style, $use );
$this->debug( "endpoint: {$this->endpoint}, soapAction: {$soapAction}, namespace: {$namespace}, style: {$style}, use: {$use}" );
$this->debug( "SOAP message length: ".strlen( $soapmsg )." contents: ".substr( $soapmsg, 0, 1000 ) );
$return = $this->send( $this->gethttpbody( $soapmsg ), $soapAction, $this->timeout, $this->response_timeout );
if ( $errstr = $this->geterror( ) )
{
$this->debug( "Error: ".$errstr );
return false;
}
else
{
$this->return = $return;
$this->debug( "sent message successfully and got a(n) ".gettype( $return )." back" );
if ( is_array( $return ) && isset( $return['faultcode'] ) )
{
$this->debug( "got fault" );
$this->seterror( $return['faultcode'].": ".$return['faultstring'] );
$this->fault = true;
foreach ( $return as $k => $v )
{
$this->$k = $v;
$this->debug( "{$k} = {$v}<br>" );
}
return $return;
}
else if ( is_array( $return ) )
{
if ( 1 < sizeof( $return ) )
{
return $return;
}
return array_shift( $return );
}
else
{
return "";
}
}
}
function getoperationdata( $operation )
{
if ( isset( $this->operations[$operation] ) )
{
return $this->operations[$operation];
}
$this->debug( "No data for operation: {$operation}" );
}
function send( $msg, $soapaction = "", $timeout = 0, $response_timeout = 30 )
{
switch ( true )
{
case ereg( "^http", $this->endpoint ) :
$this->debug( "transporting via HTTP" );
if ( $this->persistentConnection == true && is_object( $this->persistentConnection ) )
{
$http =& $this->persistentConnection;
}
else
{
$http = new soap_transport_http( $this->endpoint );
if ( $this->persistentConnection )
{
$http->usepersistentconnection( );
}
}
$http->setcontenttype( $this->gethttpcontenttype( ), $this->gethttpcontenttypecharset( ) );
$http->setsoapaction( $soapaction );
if ( $this->proxyhost && $this->proxyport )
{
$http->setproxy( $this->proxyhost, $this->proxyport, $this->proxyusername, $this->proxypassword );
}
if ( $this->username != "" && $this->password != "" )
{
$http->setcredentials( $this->username, $this->password, $this->authtype );
}
if ( $this->http_encoding != "" )
{
$http->setencoding( $this->http_encoding );
}
$this->debug( "sending message, length: ".strlen( $msg ) );
if ( ereg( "^http:", $this->endpoint ) )
{
$this->responseData = $http->send( $msg, $timeout, $response_timeout );
}
else if ( ereg( "^https", $this->endpoint ) )
{
if ( extension_loaded( "curl" ) )
{
$this->responseData = $http->sendhttps( $msg, $timeout, $response_timeout );
}
else
{
$this->seterror( "CURL Extension, or OpenSSL extension w/ PHP version >= 4.3 is required for HTTPS" );
}
}
else
{
$this->seterror( "no http/s in endpoint url" );
}
$this->request = $http->outgoing_payload;
$this->response = $http->incoming_payload;
$this->debug( "transport debug data...\n".$http->debug_str );
if ( $this->persistentConnection )
{
$http->debug_str = "";
if ( !is_object( $this->persistentConnection ) )
{
$this->persistentConnection = $http;
}
}
if ( $err = $http->geterror( ) )
{
$this->seterror( "HTTP Error: ".$err );
return false;
}
else if ( $this->geterror( ) )
{
return false;
}
else
{
$this->debug( "got response, length: ".strlen( $this->responseData )." type: ".$http->incoming_headers['content-type'] );
return $this->parseresponse( $http->incoming_headers, $this->responseData );
}
break;
default :
$this->seterror( "no transport found, or selected transport is not yet supported!" );
return false;
break;
}
}
function parseresponse( $headers, $data )
{
$this->debug( "Entering parseResponse() for data of length ".strlen( $data )." and type ".$headers['content-type'] );
if ( !strstr( $headers['content-type'], "text/xml" ) )
{
$this->seterror( "Response not of type text/xml" );
return false;
}
if ( strpos( $headers['content-type'], "=" ) )
{
$enc = str_replace( "\"", "", substr( strstr( $headers['content-type'], "=" ), 1 ) );
$this->debug( "Got response encoding: ".$enc );
if ( eregi( "^(ISO-8859-1|US-ASCII|UTF-8)\$", $enc ) )
{
$this->xml_encoding = strtoupper( $enc );
}
else
{
$this->xml_encoding = "US-ASCII";
}
}
else
{
$this->xml_encoding = "UTF-8";
}
$this->debug( "Use encoding: ".$this->xml_encoding." when creating soap_parser" );
$parser = new soap_parser( $data, $this->xml_encoding, $this->operation, $this->decode_utf8 );
$this->debug( $parser->debug_str );
if ( $errstr = $parser->geterror( ) )
{
$this->seterror( $errstr );
unset( $parser );
return false;
}
else
{
$this->responseHeaders = $parser->getheaders( );
$return = $parser->get_response( );
$this->document = $parser->document;
unset( $parser );
return $return;
}
}
function setheaders( $headers )
{
$this->requestHeaders = $headers;
}
function getheaders( )
{
if ( $this->responseHeaders != "" )
{
return $this->responseHeaders;
}
}
function sethttpproxy( $proxyhost, $proxyport, $proxyusername = "", $proxypassword = "" )
{
$this->proxyhost = $proxyhost;
$this->proxyport = $proxyport;
$this->proxyusername = $proxyusername;
$this->proxypassword = $proxypassword;
}
function setcredentials( $username, $password, $authtype = "basic" )
{
$this->username = $username;
$this->password = $password;
$this->authtype = $authtype;
}
function sethttpencoding( $enc = "gzip, deflate" )
{
$this->http_encoding = $enc;
}
function usehttppersistentconnection( )
{
$this->persistentConnection = true;
}
function getdefaultrpcparams( )
{
return $this->defaultRpcParams;
}
function setdefaultrpcparams( $rpcParams )
{
$this->defaultRpcParams = $rpcParams;
}
function getproxy( )
{
$evalStr = "";
foreach ( $this->operations as $operation => $opData )
{
if ( $operation != "" )
{
$paramStr = "";
if ( 0 < sizeof( $opData['input']['parts'] ) )
{
foreach ( $opData['input']['parts'] as $name => $type )
{
$paramStr .= "\${$name},";
}
$paramStr = substr( $paramStr, 0, strlen( $paramStr ) - 1 );
}
$opData['namespace'] = !isset( $opData['namespace'] ) ? "http://testuri.com" : $opData['namespace'];
$evalStr .= "function {$operation} ({$paramStr}){\n\t\t\t\t\t// load params into array\n\t\t\t\t\t\$params = array({$paramStr});\n\t\t\t\t\treturn \$this->call('{$operation}',\$params,'".$opData['namespace']."','".( isset( $opData['soapAction'] ) ? $opData['soapAction'] : "" )."');\n\t\t\t\t}";
unset( $paramStr );
}
}
$r = rand( );
$evalStr = "class soap_proxy_".$r." extends soapclient {\n\t\t\t\t".$evalStr."\n\t\t\t}";
eval( $evalStr );
eval( "\$proxy = new soap_proxy_{$r}('');" );
$proxy->endpointType = "wsdl";
$proxy->wsdlFile = $this->wsdlFile;
$proxy->wsdl = $this->wsdl;
$proxy->operations = $this->operations;
$proxy->defaultRpcParams = $this->defaultRpcParams;
$proxy->username = $this->username;
$proxy->password = $this->password;
$proxy->proxyhost = $this->proxyhost;
$proxy->proxyport = $this->proxyport;
$proxy->proxyusername = $this->proxyusername;
$proxy->proxypassword = $this->proxypassword;
$proxy->timeout = $this->timeout;
$proxy->response_timeout = $this->response_timeout;
$proxy->http_encoding = $this->http_encoding;
$proxy->persistentConnection = $this->persistentConnection;
return $proxy;
}
function gethttpbody( $soapmsg )
{
return $soapmsg;
}
function gethttpcontenttype( )
{
return "text/xml";
}
function gethttpcontenttypecharset( )
{
return $this->soap_defencoding;
}
function decodeutf8( $bool )
{
$this->decode_utf8 = $bool;
return true;
}
}
?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -