📄 class.soap_transport_http.php
字号:
<?php
class soap_transport_http extends nusoap_base
{
var $url = "";
var $uri = "";
var $scheme = "";
var $host = "";
var $port = "";
var $path = "";
var $request_method = "POST";
var $protocol_version = "1.0";
var $encoding = "";
var $outgoing_headers = array( );
var $incoming_headers = array( );
var $outgoing_payload = "";
var $incoming_payload = "";
var $useSOAPAction = true;
var $persistentConnection = false;
var $ch = false;
var $username;
var $password;
function soap_transport_http( $url )
{
$this->url = $url;
$u = parse_url( $url );
foreach ( $u as $k => $v )
{
$this->debug( "{$k} = {$v}" );
$this->$k = $v;
}
if ( isset( $u['query'] ) && $u['query'] != "" )
{
$this->path .= "?".$u['query'];
}
if ( !isset( $u['port'] ) )
{
if ( $u['scheme'] == "https" )
{
$this->port = 443;
}
else
{
$this->port = 80;
}
}
$this->uri = $this->path;
ereg( "\\\$Revisio"."n: ([^ ]+)", $this->revision, $rev );
$this->outgoing_headers['User-Agent'] = $this->title."/".$this->version." (".$rev[1].")";
if ( !isset( $u['port'] ) )
{
$this->outgoing_headers['Host'] = $this->host;
}
else
{
$this->outgoing_headers['Host'] = $this->host.":".$this->port;
}
if ( isset( $u['user'] ) && $u['user'] != "" )
{
$this->setcredentials( $u['user'], isset( $u['pass'] ) ? $u['pass'] : "" );
}
}
function connect( $connection_timeout = 0, $response_timeout = 30 )
{
$this->debug( "connect connection_timeout {$connection_timeout}, response_timeout {$response_timeout}, scheme {$this->scheme}, host {$this->host}, port {$this->port}" );
if ( $this->scheme == "http" || $this->scheme == "ssl" )
{
if ( $this->persistentConnection && isset( $this->fp ) && is_resource( $this->fp ) )
{
if ( !feof( $this->fp ) )
{
$this->debug( "Re-use persistent connection" );
return true;
}
fclose( $this->fp );
$this->debug( "Closed persistent connection at EOF" );
}
if ( $this->scheme == "ssl" )
{
$host = "ssl://".$this->host;
}
else
{
$host = $this->host;
}
$this->debug( "calling fsockopen with host ".$host );
if ( 0 < $connection_timeout )
{
$this->fp = @fsockopen( $host, $this->port, $this->errno, $this->error_str, $connection_timeout );
}
else
{
$this->fp = @fsockopen( $host, $this->port, $this->errno, $this->error_str );
}
if ( !$this->fp )
{
$msg = "Couldn't open socket connection to server ".$this->url;
if ( $this->errno )
{
$msg .= ", Error (".$this->errno."): ".$this->error_str;
}
else
{
$msg .= " prior to connect(). This is often a problem looking up the host name.";
}
$this->debug( $msg );
$this->seterror( $msg );
return false;
}
socket_set_timeout( $this->fp, $response_timeout );
$this->debug( "socket connected" );
return true;
}
else if ( $this->scheme == "https" )
{
if ( !extension_loaded( "curl" ) )
{
$this->seterror( "CURL Extension, or OpenSSL extension w/ PHP version >= 4.3 is required for HTTPS" );
return false;
}
$this->debug( "connect using https" );
$this->ch = curl_init( );
$hostURL = $this->port != "" ? "https://{$this->host}:{$this->port}" : "https://{$this->host}";
$hostURL .= $this->path;
curl_setopt( $this->ch, CURLOPT_URL, $hostURL );
curl_setopt( $this->ch, CURLOPT_HEADER, 1 );
curl_setopt( $this->ch, CURLOPT_RETURNTRANSFER, 1 );
if ( $this->persistentConnection )
{
$this->persistentConnection = false;
$this->outgoing_headers['Connection'] = "close";
}
if ( $connection_timeout != 0 )
{
curl_setopt( $this->ch, CURLOPT_TIMEOUT, $connection_timeout );
}
curl_setopt( $this->ch, CURLOPT_SSL_VERIFYPEER, 0 );
curl_setopt( $this->ch, CURLOPT_SSL_VERIFYHOST, 0 );
$this->debug( "cURL connection set up" );
return true;
}
else
{
$this->seterror( "Unknown scheme ".$this->scheme );
$this->debug( "Unknown scheme ".$this->scheme );
return false;
}
}
function send( $data, $timeout = 0, $response_timeout = 30 )
{
$this->debug( "entered send() with data of length: ".strlen( $data ) );
$this->tryagain = true;
$tries = 0;
while ( $this->tryagain )
{
$this->tryagain = false;
if ( $tries++ < 2 )
{
if ( !$this->connect( $timeout, $response_timeout ) )
{
return false;
}
if ( !$this->sendrequest( $data ) )
{
return false;
}
$respdata = $this->getresponse( );
}
else
{
$this->seterror( "Too many tries to get an OK response" );
}
}
$this->debug( "end of send()" );
return $respdata;
}
function sendhttps( $data, $timeout = 0, $response_timeout = 30 )
{
return $this->send( $data, $timeout, $response_timeout );
}
function setcredentials( $username, $password, $authtype = "basic", $digestRequest = array( ) )
{
global $_SERVER;
$this->debug( "Set credentials for authtype {$authtype}" );
if ( $authtype == "basic" )
{
$this->outgoing_headers['Authorization'] = "Basic ".base64_encode( $username.":".$password );
}
else if ( $authtype == "digest" )
{
if ( isset( $digestRequest['nonce'] ) )
{
$digestRequest['nc'] = isset( $digestRequest['nc'] ) ? $digestRequest['nc']++ : 1;
$A1 = $username.":".$digestRequest['realm'].":".$password;
$HA1 = md5( $A1 );
$A2 = "POST:".$this->uri;
$HA2 = md5( $A2 );
$unhashedDigest = "";
$nonce = isset( $digestRequest['nonce'] ) ? $digestRequest['nonce'] : "";
$cnonce = $nonce;
if ( $digestRequest['qop'] != "" )
{
$unhashedDigest = $HA1.":".$nonce.":".sprintf( "%08d", $digestRequest['nc'] ).":".$cnonce.":".$digestRequest['qop'].":".$HA2;
}
else
{
$unhashedDigest = $HA1.":".$nonce.":".$HA2;
}
$hashedDigest = md5( $unhashedDigest );
$this->outgoing_headers['Authorization'] = "Digest username=\"".$username."\", realm=\"".$digestRequest['realm']."\", nonce=\"".$nonce."\", uri=\"".$this->uri."\", cnonce=\"".$cnonce."\", nc=".sprintf( "%08x", $digestRequest['nc'] ).", qop=\"".$digestRequest['qop']."\", response=\"".$hashedDigest."\"";
}
}
$this->username = $username;
$this->password = $password;
$this->authtype = $authtype;
$this->digestRequest = $digestRequest;
if ( isset( $this->outgoing_headers['Authorization'] ) )
{
$this->debug( "Authorization header set: ".substr( $this->outgoing_headers['Authorization'], 0, 12 )."..." );
}
else
{
$this->debug( "Authorization header not set" );
}
}
function setsoapaction( $soapaction )
{
$this->outgoing_headers['SOAPAction'] = "\"".$soapaction."\"";
}
function setencoding( $enc = "gzip, deflate" )
{
$this->protocol_version = "1.1";
$this->outgoing_headers['Accept-Encoding'] = $enc;
$this->outgoing_headers['Connection'] = "close";
$this->persistentConnection = false;
set_magic_quotes_runtime( 0 );
$this->encoding = $enc;
}
function setproxy( $proxyhost, $proxyport, $proxyusername = "", $proxypassword = "" )
{
$this->uri = $this->url;
$this->host = $proxyhost;
$this->port = $proxyport;
if ( $proxyusername != "" && $proxypassword != "" )
{
$this->outgoing_headers['Proxy-Authorization'] = " Basic ".base64_encode( $proxyusername.":".$proxypassword );
}
}
function decodechunked( $buffer, $lb )
{
$length = 0;
$new = "";
$chunkend = strpos( $buffer, $lb );
if ( $chunkend == FALSE )
{
$this->debug( "no linebreak found in decodeChunked" );
return $new;
}
$temp = substr( $buffer, 0, $chunkend );
$chunk_size = hexdec( trim( $temp ) );
$chunkstart = $chunkend + strlen( $lb );
while ( 0 < $chunk_size )
{
$this->debug( "chunkstart: {$chunkstart} chunk_size: {$chunk_size}" );
$chunkend = strpos( $buffer, $lb, $chunkstart + $chunk_size );
if ( $chunkend == FALSE )
{
$chunk = substr( $buffer, $chunkstart );
$new .= $chunk;
$length += strlen( $chunk );
break;
}
$chunk = substr( $buffer, $chunkstart, $chunkend - $chunkstart );
$new .= $chunk;
$length += strlen( $chunk );
$chunkstart = $chunkend + strlen( $lb );
$chunkend = strpos( $buffer, $lb, $chunkstart ) + strlen( $lb );
if ( $chunkend == FALSE )
{
break;
}
$temp = substr( $buffer, $chunkstart, $chunkend - $chunkstart );
$chunk_size = hexdec( trim( $temp ) );
$chunkstart = $chunkend;
}
return $new;
}
function buildpayload( $data )
{
$this->outgoing_headers['Content-Length'] = strlen( $data );
$this->outgoing_payload = "{$this->request_method} {$this->uri} HTTP/{$this->protocol_version}\r\n";
foreach ( $this->outgoing_headers as $k => $v )
{
$this->outgoing_payload .= $k.": ".$v."\r\n";
}
$this->outgoing_payload .= "\r\n";
$this->outgoing_payload .= $data;
}
function sendrequest( $data )
{
$this->buildpayload( $data );
if ( $this->scheme == "http" || $this->scheme == "ssl" )
{
if ( !fputs( $this->fp, $this->outgoing_payload, strlen( $this->outgoing_payload ) ) )
{
$this->seterror( "couldn't write message data to socket" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -