📄 http.php
字号:
<?php
/**
* This file contains the code for a HTTP transport layer.
*
* PHP versions 4 and 5
*
* LICENSE: This source file is subject to version 2.02 of the PHP license,
* that is bundled with this package in the file LICENSE, and is available at
* through the world-wide-web at http://www.php.net/license/2_02.txt. If you
* did not receive a copy of the PHP license and are unable to obtain it
* through the world-wide-web, please send a note to license@php.net so we can
* mail you a copy immediately.
*
* @category Web Services
* @package SOAP
* @author Shane Caraveo <Shane@Caraveo.com>
* @copyright 2003-2005 The PHP Group
* @license http://www.php.net/license/2_02.txt PHP License 2.02
* @link http://pear.php.net/package/SOAP
*/
/**
* HTTP Transport class
*
* @package SOAP
* @category Web_Services
*/
/**
* Needed Classes
*/
require_once 'SOAP/Base.php';
/**
* HTTP Transport for SOAP
*
* @access public
* @package SOAP
* @author Shane Caraveo <shane@php.net>
*/
class SOAP_Transport_HTTP extends SOAP_Base
{
/**
* Basic Auth string.
*
* @var array
*/
var $headers = array();
/**
* Cookies.
*
* @var array
*/
var $cookies;
/**
* Connection timeout in seconds. 0 = none.
*
* @var integer
*/
var $timeout = 4;
/**
* Array containing urlparts - parse_url().
*
* @var mixed
*/
var $urlparts = null;
/**
* Connection endpoint - URL.
*
* @var string
*/
var $url = '';
/**
* Incoming payload.
*
* @var string
*/
var $incoming_payload = '';
/**
* HTTP-Request User-Agent.
*
* @var string
*/
var $_userAgent = SOAP_LIBRARY_NAME;
/**
* HTTP encoding.
*
* @var string
*/
var $encoding = SOAP_DEFAULT_ENCODING;
/**
* HTTP-Response Content-Type encoding.
* We assume UTF-8 if no encoding is set.
*
* @var string
*/
var $result_encoding = 'UTF-8';
/**
* HTTP-Response Content-Type.
*/
var $result_content_type;
var $result_headers = array();
var $result_cookies = array();
/**
* SOAP_Transport_HTTP Constructor
*
* @access public
*
* @param string $url HTTP url to SOAP endpoint.
* @param string $encoding Encoding to use.
*/
function SOAP_Transport_HTTP($url, $encoding = SOAP_DEFAULT_ENCODING)
{
parent::SOAP_Base('HTTP');
$this->urlparts = @parse_url($url);
$this->url = $url;
$this->encoding = $encoding;
}
/**
* Sends and receives SOAP data.
*
* @param string Outgoing POST data.
* @param array Options.
*
* @return string|SOAP_Fault
* @access public
*/
function send($msg, $options = null)
{
if (!$this->_validateUrl()) {
return $this->fault;
}
if (isset($options['timeout'])) {
$this->timeout = (int)$options['timeout'];
}
if (strcasecmp($this->urlparts['scheme'], 'HTTP') == 0) {
return $this->_sendHTTP($msg, $options);
} elseif (strcasecmp($this->urlparts['scheme'], 'HTTPS') == 0) {
return $this->_sendHTTPS($msg, $options);
}
return $this->_raiseSoapFault('Invalid url scheme ' . $this->url);
}
/**
* Sets data for HTTP authentication, creates authorization header.
*
* @param string $username Username.
* @param string $password Response data, minus HTTP headers.
*
* @access public
*/
function setCredentials($username, $password)
{
$this->headers['Authorization'] = 'Basic ' . base64_encode($username . ':' . $password);
}
/**
* Adds a cookie.
*
* @access public
* @param string $name Cookie name.
* @param mixed $value Cookie value.
*/
function addCookie($name, $value)
{
$this->cookies[$name] = $value;
}
/**
* Generates the correct headers for the cookies.
*
* @access private
*/
function _genCookieHeader()
{
foreach ($this->cookies as $name=>$value) {
$cookies = (isset($cookies) ? $cookies. '; ' : '') .
urlencode($name) . '=' . urlencode($value);
}
return $cookies;
}
/**
* Validate url data passed to constructor.
*
* @access private
* @return boolean
*/
function _validateUrl()
{
if (!is_array($this->urlparts) ) {
$this->_raiseSoapFault('Unable to parse URL ' . $this->url);
return false;
}
if (!isset($this->urlparts['host'])) {
$this->_raiseSoapFault('No host in URL ' . $this->url);
return false;
}
if (!isset($this->urlparts['port'])) {
if (strcasecmp($this->urlparts['scheme'], 'HTTP') == 0) {
$this->urlparts['port'] = 80;
} elseif (strcasecmp($this->urlparts['scheme'], 'HTTPS') == 0) {
$this->urlparts['port'] = 443;
}
}
if (isset($this->urlparts['user'])) {
$this->setCredentials(urldecode($this->urlparts['user']),
urldecode($this->urlparts['pass']));
}
if (!isset($this->urlparts['path']) || !$this->urlparts['path']) {
$this->urlparts['path'] = '/';
}
return true;
}
/**
* Finds out what the encoding is.
* Sets the object property accordingly.
*
* @access private
* @param array $headers Headers.
*/
function _parseEncoding($headers)
{
$h = stristr($headers, 'Content-Type');
preg_match_all('/^Content-Type:\s*(.*)$/im', $h, $ct, PREG_SET_ORDER);
$n = count($ct);
$ct = $ct[$n - 1];
// Strip the string of \r.
$this->result_content_type = str_replace("\r", '', $ct[1]);
if (preg_match('/(.*?)(?:;\s?charset=)(.*)/i',
$this->result_content_type,
$m)) {
$this->result_content_type = $m[1];
if (count($m) > 2) {
$enc = strtoupper(str_replace('"', '', $m[2]));
if (in_array($enc, $this->_encodings)) {
$this->result_encoding = $enc;
}
}
}
// Deal with broken servers that don't set content type on faults.
if (!$this->result_content_type) {
$this->result_content_type = 'text/xml';
}
}
/**
* Parses the headers.
*
* @param array $headers The headers.
*/
function _parseHeaders($headers)
{
/* Largely borrowed from HTTP_Request. */
$this->result_headers = array();
$headers = split("\r?\n", $headers);
foreach ($headers as $value) {
if (strpos($value,':') === false) {
$this->result_headers[0] = $value;
continue;
}
list($name, $value) = split(':', $value);
$headername = strtolower($name);
$headervalue = trim($value);
$this->result_headers[$headername] = $headervalue;
if ($headername == 'set-cookie') {
// Parse a SetCookie header to fill _cookies array.
$cookie = array('expires' => null,
'domain' => $this->urlparts['host'],
'path' => null,
'secure' => false);
if (!strpos($headervalue, ';')) {
// Only a name=value pair.
list($cookie['name'], $cookie['value']) = array_map('trim', explode('=', $headervalue));
$cookie['name'] = urldecode($cookie['name']);
$cookie['value'] = urldecode($cookie['value']);
} else {
// Some optional parameters are supplied.
$elements = explode(';', $headervalue);
list($cookie['name'], $cookie['value']) = array_map('trim', explode('=', $elements[0]));
$cookie['name'] = urldecode($cookie['name']);
$cookie['value'] = urldecode($cookie['value']);
for ($i = 1; $i < count($elements);$i++) {
list($elName, $elValue) = array_map('trim', explode('=', $elements[$i]));
if ('secure' == $elName) {
$cookie['secure'] = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -