PHP : Function Reference : SOAP Functions : SoapClient->__construct()
SoapClient {
__construct(mixed wsdl, array options); }
This constructor creates SoapClient objects
in WSDL or non-WSDL mode.
Parameters
- wsdl
-
URI of the WSDL file or NULL if working in
non-WSDL mode.
Note:
During development stage, you may want to disable WSDL caching by the
mean of the soap.wsdl_cache_ttl php.ini setting,
otherwise changes made to the WSDL file will have no effect until
soap.wsdl_cache_ttl is expired.
- options
-
An array of options. If working in WSDL mode, this parameter is optional.
If working in non-WSDL mode, you must set the location
and uri options, where location is
the URL to request and uri is the target namespace of the
SOAP service.
The style and use options only work in
non-WSDL mode. In WSDL mode, they come from the WSDL file.
The soap_version option specifies whether to use SOAP
1.1, or SOAP 1.2 client.
For HTTP authentication, you may use the login and
password options. For making an HTTP connection through
a proxy server, use the options proxy_host ,
proxy_port , proxy_login
and proxy_password .
For HTTPS client certificate authentication use
local_cert and passphrase options.
The compression option allows to use compression
of HTTP SOAP requests and responses.
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.
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.
Setting the boolean trace option enables use of the
methods
SoapClient->__getLastRequest,
SoapClient->__getLastRequestHeaders,
SoapClient->__getLastResponse and
SoapClient->__getLastResponseHeaders.
The exceptions option is a boolean value defining whether
soap errors throw exceptions of type
SoapFault.
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.
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).
Other options are stream_context ,
features , cache_wsdl and
user_agent .
Examples
Example 2271. SoapClient examples
<?php
$client = new SoapClient("some.wsdl");
$client = new SoapClient("some.wsdl", array('soap_version' => SOAP_1_2));
$client = new SoapClient("some.wsdl", array('login' => "some_name",
'password' => "some_password"));
$client = new SoapClient("some.wsdl", array('proxy_host' => "localhost",
'proxy_port' => 8080));
$client = new SoapClient("some.wsdl", array('proxy_host' => "localhost",
'proxy_port' => 8080,
'proxy_login' => "some_name",
'proxy_password' => "some_password"));
$client = new SoapClient("some.wsdl", array('local_cert' => "cert_key.pem"));
$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
'uri' => "http://test-uri/"));
$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
'uri' => "http://test-uri/",
'style' => SOAP_DOCUMENT,
'use' => SOAP_LITERAL));
$client = new SoapClient("some.wsdl",
array('compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP));
$server = new SoapClient("some.wsdl", array('encoding'=>'ISO-8859-1'));
class MyBook {
public $title;
public $author;
}
$server = new SoapClient("books.wsdl", array('classmap' => array('book' => "MyBook")));
?>
arjan van bentem
When using HTTP basic authentication, PHP will only send the credentials when invoking the service, not when fetching the WSDL.
To solve this, one needs to fetch the WSDL manually (or using PHP, of course!) and store it on the file system. Obviously, the first parameter for SoapClient(..) then needs to refer to that local copy.
Alternatively, the user annotations at http://nl3.php.net/manual/en/ref.soap.php state that one could encode the login and password into the URL as well.
See http://bugs.php.net/bug.php?id=27777
matt_schiros
When using classmap to map the SOAP results to a class, the constructor of the object you've mapped to is _not_ called. This applies to the PHP5 __construct() and the PHP4 ClassName() constructors.
brian grayless
Using a WSDL file is the way to go, however, for my particular application, the LOCATION:PORT needed to be dynamic so that my SOAP clients would be able to call a different service based on the client domain.
If you are using a WSDL, SoapClient() requires a URL direct to an actual URL and does not let you use a PHP file that outputs the dynamic WSDL XML in its stead. So, I ended up making a separate WSDL for each possible service needed and had to maintain them all if the service description changed.
Finally, after some fiddling, I was able to create a PHP page with the proper Mime type headers so that I could then trick SoapClient() to think that it was being passed a file with a ".wsdl" extension.
Example:
<?php
// filename: wsdl.php
header('Content-Type: application/xml; charset=UTF-8');
header('Content-Disposition: attachment; filename="filename.wsdl"');
$wsdl = 'path/wsdl_name.wsdl';
// read in file
$handle = fopen($wsdl, "r");
$wsdl_xml = fread($handle, filesize($wsdl));
fclose($handle);
// put code here to replace url and port in xml
echo $wsdl_xml;
?>
Now, in order to make this work, you can't just call a relative path to the file. I believe it has to go through Apache to properly set the mime type headers, etc... So you would use a full "http://....." address as the path to the wsdl.php file.
<?php
//... somewhere in your soap client code
$wsdl_loc = "http://yourdomain.com/wsdl.php";
$soap_client = new SoapClient($wsdl_loc, $client_param_array);
?>
Another, perhaps not so clean, way of achieving this would be to modify your .htaccess file in the directory where your WSDL file exists to force ".wsdl" files to run through the PHP engine. Add the following to your .htaccess:
AddType application/x-httpd-php .php .wsdl
You can then put dynamic PHP code snippets in your *.wsdl files to change whatever values you need to.
There are pros and cons to each solutions. The Mime solution probably taxes the system more as it has to read the file in every time a soap request is made. The htaccess solution makes it so you have to depend on either a modified .htaccess or Apache conf file.
Perhaps if you set the "soap.wsdl_cache_enabled", using ini_set(), to 1 (default), the caching will make it so it doesn't read the file every time for the Mime solution.
giosh94mhz
Oops!
I have written the wrong exception message in my last post.
The correct exception is:
SoapFault exception: [Client] looks like we got no XML document in <document>
php
If you're using CLI and there are multiple IP addresses available for outgoing SOAP-requests, try this "secret" to set outgoing IP:
e.g. for local IP 10.1.4.71:
$opts = array('socket' => array('bindto' => '10.1.4.71:0'));
$context = stream_context_create($opts);
$client = new SoapClient(null, array('location'=>'http://...','uri' => '...','stream_context' => $context));
You can also set other options for the stream context, please refer to this page:
Appendix M: http://www.php.net/manual/en/wrappers.php
Bye,
Nils Sowen
giosh94mhz
If you get the following exception:
'Cannot open the XML file: the file is not well-formatted!'
PHP files of your SOAP-server contains syntax-error (probably).
If everything looks ok, try to remove EVERY blank-space and new-line outside the <?php ... ?> tag... this should save you an headache!
jared
If you connect to a SoapServer, that has been created with session persistence, you can access the server's session id via SoapClient->_cookies[<session_id_name>][0]. This property becomes available after your first client method call.
I have only tested this with session.use_cookies=1.
marius mathiesen
I kept having a problem using an HTTP proxy with SOAP. The proxy_port parameter has to be an integer, ie. "proxy_port"=>"80" won't work, you'll have to use "proxy_port"=>80.
HTH,
Marius
jim plush
As of version 5.0.4 you can now dynamically change your location even if you're using a wsdl
<?php
$client = new SoapClient("http://some.host.net/wsdl/somefile.wsdl",array(
"location" => 'myurl.com'));
?>
notice you can now use the "location" key.
This means you can have the same wsdl file not define a location until runtime which is great if you have a development test site or if you distribute your files to other companies.
Prior to this change you would have to ship a custom wsdl file to every client you had with their location hardcoded.
lsmith
As noted in the bug report http://bugs.php.net/bug.php?id=36226, it is considered a feature that sequences with a single element do not come out as arrays. To override this "feature" you can do the following:
$x = new SoapClient($wsdl, array('features' =>
SOAP_SINGLE_ELEMENT_ARRAYS));
|
|