Add mirror of xmlrpc ssl send call

This commit is contained in:
Miles Lott 2001-08-15 14:23:30 +00:00
parent 51a572a9f5
commit 2247e8c99c

View File

@ -1,160 +1,218 @@
<?php <?php
/* /*
SOAPx4 SOAPx4
by Dietrich Ayala (C) 2001 dietrich@ganx4.com by Dietrich Ayala (C) 2001 dietrich@ganx4.com
This project began based on code from the 2 projects below, This project began based on code from the 2 projects below,
and still contains some original code. The licenses of both must be respected. and still contains some original code. The licenses of both must be respected.
XML-RPC for PHP XML-RPC for PHP
originally by Edd Dumbill (C) 1999-2000 originally by Edd Dumbill (C) 1999-2000
SOAP for PHP SOAP for PHP
by Victor Zou (C) 2000-2001 <victor@gigaideas.com.cn> by Victor Zou (C) 2000-2001 <victor@gigaideas.com.cn>
*/ */
/* changelog: /* changelog:
2001-07-04 2001-07-04
- abstract type system to support either 1999 or 2001 schema (arg, typing still needs much - abstract type system to support either 1999 or 2001 schema (arg, typing still needs much
solidification.) solidification.)
- implemented proxy support, based on sample code from miles lott <milos@speakeasy.net> - implemented proxy support, based on sample code from miles lott <milos@speakeasy.net>
- much general cleanup of code & cleaned out what was left of original xml-rpc/gigaideas code - much general cleanup of code & cleaned out what was left of original xml-rpc/gigaideas code
- implemented a transport argument into send() that allows you to specify different transports - implemented a transport argument into send() that allows you to specify different transports
(assuming you have implemented the function, and added it to the conditional statement in send() (assuming you have implemented the function, and added it to the conditional statement in send()
- abstracted the determination of charset in Content-type header - abstracted the determination of charset in Content-type header
2001-07-5 2001-07-5
- fixed more weird type/namespace issues - fixed more weird type/namespace issues
*/ */
// $path can be a complete endpoint url, with the other parameters left blank: // $path can be a complete endpoint url, with the other parameters left blank:
// $soap_client = new soap_client("http://path/to/soap/server"); // $soap_client = new soap_client("http://path/to/soap/server");
class soap_client class soap_client
{ {
function soap_client($path,$server=False,$port=False) function soap_client($path,$server=False,$port=False)
{ {
$this->port = 80; $this->port = 80;
$this->path = $path; $this->path = $path;
$this->server = $server; $this->server = $server;
$this->errno; $this->errno;
$this->errstring; $this->errstring;
$this->debug_flag = True; $this->debug_flag = False;
$this->debug_str = ""; $this->debug_str = "";
$this->username = ""; $this->username = "";
$this->password = ""; $this->password = "";
$this->action = ""; $this->action = "";
$this->incoming_payload = ""; $this->incoming_payload = "";
$this->outgoing_payload = ""; $this->outgoing_payload = "";
$this->response = ""; $this->response = "";
$this->action = ""; $this->action = "";
// endpoint mangling // endpoint mangling
if(ereg("^http://",$path)) if(ereg("^http://",$path))
{
$path = str_replace("http://","",$path);
$this->path = strstr($path,"/");
$this->debug("path = $this->path");
if(ereg(":",$path))
{ {
$this->server = substr($path,0,strpos($path,":")); $path = str_replace("http://","",$path);
$this->port = substr(strstr($path,":"),1); $this->path = strstr($path,"/");
$this->port = substr($this->port,0,strpos($this->port,"/")); $this->debug("path = $this->path");
if(ereg(":",$path))
{
$this->server = substr($path,0,strpos($path,":"));
$this->port = substr(strstr($path,":"),1);
$this->port = substr($this->port,0,strpos($this->port,"/"));
}
else
{
$this->server = substr($path,0,strpos($path,"/"));
}
}
if($port)
{
$this->port = $port;
}
}
function setCredentials($u, $p)
{
$this->username = $u;
$this->password = $p;
}
function send($msg, $action, $timeout=0, $ssl=False)
{
// where msg is an soapmsg
$msg->debug_flag = $this->debug_flag;
$this->action = $action;
if($ssl)
{
return $this->ssl_sendPayloadHTTP10(
$msg,
$this->server,
$this->port,
$timeout,
$this->username,
$this->password
);
} }
else else
{ {
$this->server = substr($path,0,strpos($path,"/")); return $this->sendPayloadHTTP10(
$msg,
$this->server,
$this->port,
$timeout,
$this->username,
$this->password
);
} }
} }
if($port)
{
$this->port = $port;
}
}
function setCredentials($u, $p) function sendPayloadHTTP10($msg, $server, $port, $timeout=0, $username='', $password='')
{ {
$this->username = $u; if($timeout > 0)
$this->password = $p; {
} $fp = fsockopen($server, $port,&$this->errno, &$this->errstr, $timeout);
}
else
{
$fp = fsockopen($server, $port,&$this->errno, &$this->errstr);
}
if (!$fp)
{
$this->debug("Couldn't open socket connection to server!");
$this->debug("Server: $this->server");
return 0;
}
function send($msg, $action, $timeout=0) // thanks to Grant Rauscher <grant7@firstworld.net> for this
{ $credentials = '';
// where msg is an soapmsg if ($username != '')
$msg->debug_flag = $this->debug_flag; {
$this->action = $action; $credentials = "Authorization: Basic " . base64_encode($username . ":" . $password) . "\r\n";
return $this->sendPayloadHTTP10( }
$msg,
$this->server,
$this->port,
$timeout,
$this->username,
$this->password
);
}
function sendPayloadHTTP10($msg, $server, $port, $timeout=0, $username="", $password="") $soap_data = $msg->serialize();
{ $this->outgoing_payload = "POST ".
if($timeout > 0) $this->path.
{ " HTTP/1.0\r\n".
$fp = fsockopen($server, $port,&$this->errno, &$this->errstr, $timeout); "User-Agent: SOAPx4 v0.13492\r\n".
} "Host: ".$this->server . "\r\n".
else $credentials.
{ "Content-Type: text/xml\r\nContent-Length: ".strlen($soap_data)."\r\n".
$fp = fsockopen($server, $port,&$this->errno, &$this->errstr); "SOAPAction: \"$this->action\""."\r\n\r\n".
} $soap_data;
if (!$fp) // send
{ if(!fputs($fp, $this->outgoing_payload, strlen($this->outgoing_payload)))
$this->debug("Couldn't open socket connection to server!"); {
$this->debug("Server: $this->server"); $this->debug("Write error");
return 0; }
}
// get reponse
// thanks to Grant Rauscher <grant7@firstworld.net> for this while($data = fread($fp, 32768))
$credentials = ""; {
if ($username != "") $incoming_payload .= $data;
{ }
$credentials = "Authorization: Basic "
. base64_encode($username . ":" . $password) . "\r\n"; fclose($fp);
$this->incoming_payload = $incoming_payload;
// $response is a soapmsg object
$this->response = $msg->parseResponse($incoming_payload);
$this->debug($msg->debug_str);
return $this->response;
} }
$soap_data = $msg->serialize(); function ssl_sendPayloadHTTP10($msg, $server, $port, $timeout=0,$username='', $password='')
$this->outgoing_payload = "POST ".
$this->path.
" HTTP/1.0\r\n".
"User-Agent: SOAPx4 v0.13492\r\n".
"Host: ".$this->server . "\r\n".
$credentials.
"Content-Type: text/xml\r\nContent-Length: ".strlen($soap_data)."\r\n".
"SOAPAction: \"$this->action\""."\r\n\r\n".
$soap_data;
// send
if(!fputs($fp, $this->outgoing_payload, strlen($this->outgoing_payload)))
{ {
$this->debug("Write error"); if(!function_exists(curl_init))
{
$this->errstr = 'No curl functions available - use of ssl is invalid';
return False;
}
/* curl Method borrowed from:
http://sourceforge.net/tracker/index.php?func=detail&aid=427359&group_id=23199&atid=377731
*/
// thanks to Grant Rauscher <grant7@firstworld.net>
// for this
$credentials = '';
if ($username!='')
{
$credentials = "Authorization: Basic " . base64_encode($username . ':' . $password) . "\r\n";
}
$soap_data = $msg->serialize();
$this->outgoing_payload = "POST ".
$this->path.
" HTTP/1.0\r\n".
"User-Agent: SOAPx4 v0.13492\r\n".
"Host: ".$this->server . "\r\n".
$credentials.
"Content-Type: text/xml\r\nContent-Length: ".strlen($soap_data)."\r\n".
"SOAPAction: \"$this->action\""."\r\n\r\n".
$soap_data;
// send
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$this->server);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->outgoing_payload);
curl_setopt($ch, CURLOPT_HEADER, 0);
$incoming_payload = curl_exec($ch);
curl_close($ch);
$this->incoming_payload = $incoming_payload;
// $response is a soapmsg object
$this->response = $msg->parseResponse($incoming_payload);
$this->debug($msg->debug_str);
return $this->response;
} }
// get reponse function debug($string)
while($data = fread($fp, 32768))
{ {
$incoming_payload .= $data; if($this->debug_flag)
{
$this->debug_str .= "$string\n";
}
} }
} // end class soap_client
fclose($fp);
$this->incoming_payload = $incoming_payload;
// $response is a soapmsg object
$this->response = $msg->parseResponse($incoming_payload);
$this->debug($msg->debug_str);
return $this->response;
}
function debug($string)
{
if($this->debug_flag)
{
$this->debug_str .= "$string\n";
}
}
} // end class soap_client
?> ?>