关于在PHP中扩展核心类(示例肥皂客户端)

时间:2016-05-20 16:29:28

标签: php web-services soap

我已经在PHP中开发了一段时间了,我从来没有遇到需要从PHP核心类扩展的情况。

我遇到一个问题,我有一个Soap Webservices Provider,他的SOAP服务写得非常好,所以PHP-CLIENT类给了我很多验证错误。无论如何,他们的SOAP"工作",不是应该但它确实如此。

我想要做的是从此类扩展并修改验证,例如,不控制targetNamespace具有该位置的不同名称。我知道我不应该这样做,但我不想从头开始编写SOAP类。

当我试图从这个类(SoapClient)扩展时,我意识到方法中根本没有代码,我找不到原因。我想也许是因为这些方法可以在" C"环境,或者它们已被编译以使其更快。如果不是这种情况,我很乐意看到这个类的代码并根据我的需要进行修改。

欢迎任何建议,提示或解释。我附上了SoapClient代码。

class SoapClient  {

/**
 * (PHP 5 &gt;= 5.0.1)<br/>
 * SoapClient constructor
 * @link http://php.net/manual/en/soapclient.soapclient.php
 * @param mixed $wsdl <p>
 * URI of the WSDL file or <b>NULL</b> if working in
 * non-WSDL mode.
 * </p>
 * <p>
 * During development, WSDL caching may be disabled by the
 * use of the soap.wsdl_cache_ttl <i>php.ini</i> setting
 * otherwise changes made to the WSDL file will have no effect until
 * soap.wsdl_cache_ttl is expired.
 * </p>
 * @param array $options [optional] <p>
 * An array of options. If working in WSDL mode, this parameter is optional.
 * If working in non-WSDL mode, the location and
 * uri options must be set, where location
 * is the URL of the SOAP server to send the request to, and uri
 * is the target namespace of the SOAP service.
 * </p>
 * <p>
 * The style and use options only work in
 * non-WSDL mode. In WSDL mode, they come from the WSDL file.
 * </p>
 * <p>
 * The soap_version option should be one of either
 * <b>SOAP_1_1</b> or <b>SOAP_1_2</b> to
 * select SOAP 1.1 or 1.2, respectively. If omitted, 1.1 is used.
 * </p>
 * <p>
 * For HTTP authentication, the login and
 * password options can be used to supply credentials.
 * For making an HTTP connection through
 * a proxy server, the options proxy_host,
 * proxy_port, proxy_login
 * and proxy_password are also available.
 * For HTTPS client certificate authentication use
 * local_cert and passphrase options. An
 * authentication may be supplied in the authentication
 * option. The authentication method may be either
 * <b>SOAP_AUTHENTICATION_BASIC</b> (default) or
 * <b>SOAP_AUTHENTICATION_DIGEST</b>.
 * </p>
 * <p>
 * The compression option allows to use compression
 * of HTTP SOAP requests and responses.
 * </p>
 * <p>
 * The encoding option defines internal character
 * encoding. This option does not change the encoding of SOAP requests (it is
 * always utf-8), but converts strings into it.
 * </p>
 * <p>
 * The trace option enables tracing of request so faults
 * can be backtraced. This defaults to <b>FALSE</b>
 * </p>
 * <p>
 * The classmap option can be used to map some WSDL
 * types to PHP classes. This option must be an array with WSDL types
 * as keys and names of PHP classes as values.
 * </p>
 * <p>
 * Setting the boolean trace option enables use of the
 * methods
 * SoapClient->__getLastRequest,
 * SoapClient->__getLastRequestHeaders,
 * SoapClient->__getLastResponse and
 * SoapClient->__getLastResponseHeaders.
 * </p>
 * <p>
 * The exceptions option is a boolean value defining whether
 * soap errors throw exceptions of type
 * SoapFault.
 * </p>
 * <p>
 * The connection_timeout option defines a timeout in seconds
 * for the connection to the SOAP service. This option does not define a timeout
 * for services with slow responses. To limit the time to wait for calls to finish the
 * default_socket_timeout setting
 * is available.
 * </p>
 * <p>
 * The typemap option is an array of type mappings.
 * Type mapping is an array with keys type_name,
 * type_ns (namespace URI), from_xml
 * (callback accepting one string parameter) and to_xml
 * (callback accepting one object parameter).
 * </p>
 * <p>
 * The cache_wsdl option is one of
 * <b>WSDL_CACHE_NONE</b>,
 * <b>WSDL_CACHE_DISK</b>,
 * <b>WSDL_CACHE_MEMORY</b> or
 * <b>WSDL_CACHE_BOTH</b>.
 * </p>
 * <p>
 * The user_agent option specifies string to use in
 * User-Agent header.
 * </p>
 * <p>
 * The stream_context option is a resource
 * for context.
 * </p>
 * <p>
 * The features option is a bitmask of
 * <b>SOAP_SINGLE_ELEMENT_ARRAYS</b>,
 * <b>SOAP_USE_XSI_ARRAY_TYPE</b>,
 * <b>SOAP_WAIT_ONE_WAY_CALLS</b>.
 * </p>
 * <p>
 * The keep_alive option is a boolean value defining whether
 * to send the Connection: Keep-Alive header or
 * Connection: close.
 * </p>
 * <p>
 * The ssl_method option is one of
 * <b>SOAP_SSL_METHOD_TLS</b>,
 * <b>SOAP_SSL_METHOD_SSLv2</b>,
 * <b>SOAP_SSL_METHOD_SSLv3</b> or
 * <b>SOAP_SSL_METHOD_SSLv23</b>.
 * </p>
 */
public function SoapClient ($wsdl, array $options = null) {}

/**
 * (PHP 5 &gt;= 5.0.1)<br/>
 * Calls a SOAP function (deprecated)
 * @link http://php.net/manual/en/soapclient.call.php
 * @param string $function_name
 * @param string $arguments
 * @return mixed
 */
public function __call ($function_name, $arguments) {}

/**
 * (PHP 5 &gt;= 5.0.1)<br/>
 * Calls a SOAP function
 * @link http://php.net/manual/en/soapclient.soapcall.php
 * @param string $function_name <p>
 * The name of the SOAP function to call.
 * </p>
 * @param array $arguments <p>
 * An array of the arguments to pass to the function. This can be either
 * an ordered or an associative array. Note that most SOAP servers require
 * parameter names to be provided, in which case this must be an
 * associative array.
 * </p>
 * @param array $options [optional] <p>
 * An associative array of options to pass to the client.
 * </p>
 * <p>
 * The location option is the URL of the remote Web service.
 * </p>
 * <p>
 * The uri option is the target namespace of the SOAP service.
 * </p>
 * <p>
 * The soapaction option is the action to call.
 * </p>
 * @param mixed $input_headers [optional] <p>
 * An array of headers to be sent along with the SOAP request.
 * </p>
 * @param array $output_headers [optional] <p>
 * If supplied, this array will be filled with the headers from the SOAP response.
 * </p>
 * @return mixed SOAP functions may return one, or multiple values. If only one value is returned
 * by the SOAP function, the return value of __soapCall will be
 * a simple value (e.g. an integer, a string, etc). If multiple values are
 * returned, __soapCall will return
 * an associative array of named output parameters.
 * </p>
 * <p>
 * On error, if the SoapClient object was constructed with the exceptions
 * option set to <b>FALSE</b>, a SoapFault object will be returned.
 */
public function __soapCall ($function_name, array $arguments, array $options = null, $input_headers = null, array &$output_headers = null) {}

/**
 * (PHP 5 &gt;= 5.0.1)<br/>
 * Returns last SOAP request
 * @link http://php.net/manual/en/soapclient.getlastrequest.php
 * @return string The last SOAP request, as an XML string.
 */
public function __getLastRequest () {}

/**
 * (PHP 5 &gt;= 5.0.1)<br/>
 * Returns last SOAP response
 * @link http://php.net/manual/en/soapclient.getlastresponse.php
 * @return string The last SOAP response, as an XML string.
 */
public function __getLastResponse () {}

/**
 * (PHP 5 &gt;= 5.0.1)<br/>
 * Returns the SOAP headers from the last request
 * @link http://php.net/manual/en/soapclient.getlastrequestheaders.php
 * @return string The last SOAP request headers.
 */
public function __getLastRequestHeaders () {}

/**
 * (PHP 5 &gt;= 5.0.1)<br/>
 * Returns the SOAP headers from the last response
 * @link http://php.net/manual/en/soapclient.getlastresponseheaders.php
 * @return string The last SOAP response headers.
 */
public function __getLastResponseHeaders () {}

/**
 * (PHP 5 &gt;= 5.0.1)<br/>
 * Returns list of available SOAP functions
 * @link http://php.net/manual/en/soapclient.getfunctions.php
 * @return array The array of SOAP function prototypes, detailing the return type,
 * the function name and type-hinted paramaters.
 */
public function __getFunctions () {}

/**
 * (PHP 5 &gt;= 5.0.1)<br/>
 * Returns a list of SOAP types
 * @link http://php.net/manual/en/soapclient.gettypes.php
 * @return array The array of SOAP types, detailing all structures and types.
 */
public function __getTypes () {}

/**
 * (PHP 5 &gt;= 5.0.1)<br/>
 * Performs a SOAP request
 * @link http://php.net/manual/en/soapclient.dorequest.php
 * @param string $request <p>
 * The XML SOAP request.
 * </p>
 * @param string $location <p>
 * The URL to request.
 * </p>
 * @param string $action <p>
 * The SOAP action.
 * </p>
 * @param int $version <p>
 * The SOAP version.
 * </p>
 * @param int $one_way [optional] <p>
 * If one_way is set to 1, this method returns nothing.
 * Use this where a response is not expected.
 * </p>
 * @return string The XML SOAP response.
 */
public function __doRequest ($request, $location, $action, $version, $one_way = 0) {}

/**
 * (PHP 5 &gt;= 5.0.4)<br/>
 * The __setCookie purpose
 * @link http://php.net/manual/en/soapclient.setcookie.php
 * @param string $name <p>
 * The name of the cookie.
 * </p>
 * @param string $value [optional] <p>
 * The value of the cookie. If not specified, the cookie will be deleted.
 * </p>
 * @return void No value is returned.
 */
public function __setCookie ($name, $value = null) {}

public function __getCookies () {}

/**
 * (PHP 5 &gt;= 5.0.1)<br/>
 * Sets the location of the Web service to use
 * @link http://php.net/manual/en/soapclient.setlocation.php
 * @param string $new_location [optional] <p>
 * The new endpoint URL.
 * </p>
 * @return string The old endpoint URL.
 */
public function __setLocation ($new_location = null) {}

/**
 * (PHP 5 &gt;= 5.0.5)<br/>
 * Sets SOAP headers for subsequent calls
 * @link http://php.net/manual/en/soapclient.setsoapheaders.php
 * @param mixed $soapheaders [optional] <p>
 * The headers to be set. It could be <b>SoapHeader</b>
 * object or array of <b>SoapHeader</b> objects.
 * If not specified or set to <b>NULL</b>, the headers will be deleted.
 * </p>
 * @return bool <b>TRUE</b> on success or <b>FALSE</b> on failure.
 */
public function __setSoapHeaders ($soapheaders = null) {}

}

2 个答案:

答案 0 :(得分:0)

就像对待任何PHP类一样:

class mySoapClient extends SoapClient {
    //override parent function
    public function nativeFunction() {
          //...some logic ...
    }
}

关键是你需要知道它是否会像你想要的那样容易。有时更容易使用第三方库,您可以访问代码并查看如何更改代码。

答案 1 :(得分:0)

最后解决方案根本不是很好。

在将SOAP类连接到有很多问题的服务的情况下,我所做的是下载WSDL结构并根据我的需要进行修改。

我不喜欢这个解决方案,因为如果API供应商改变了什么,我就不会知道它。

在下面的代码中,您可以看到为固定XML调用SOAP调用并询问服务。

$urlXML = "FIXEDXML.xml";
    $cliente = new SoapClient($urlXML, array(
        "trace" => true,
        'soap_version' => SOAP_1_1,
        'cache_wsdl' => WSDL_CACHE_NONE
    ));

    var_dump($cliente);

    echo "<h1> FUNCIONES DISPONIBLES </h1>";
    echo '<pre>';
    $functiones = $cliente->__getFunctions();
    var_dump($functiones);
    echo '</pre>';
} catch (Exception $exc) {
    var_dump($exc);
    echo $exc->getTraceAsString();
}

我希望这对某人有用。我很长一段时间都试图让它发挥作用。