i was looking for a good example and couldnt find one,
finally found it somewhere(forgot where) i think this is
the best example to make a soap request with multiple params
$params->AWSAccessKeyId = AMAZON_API_KEY;
$params->Request->SearchIndex = 'Books';
$params->Request->Keywords = 'php5 oop';
$amazon = new SoapClient('http://webservices.amazon.com
/AWSECommerceService/AWSECommerceService.wsdl');
$result = $amazon->itemSearch($params);
SoapClient->__construct()
(No version information available, might be only in CVS)
SoapClient->__construct() — Constructeur SoapClient
Description
Ce constructeur crée des objets SoapClient dans le mode WSDL ou non-WSDL.
Liste de paramètres
- wsdl
-
URI du fichier WSDL ou NULL s'il travaille en mode non-WSDL.
Note: Durant le stage de développement, vous devriez vouloir désactiver le cache WSDL via l'option de configuration soap.wsdl_cache_ttl de votre php.ini, sinon, les modifications faites sur le fichier WSDL n'auront aucun effet tant que le cache soap.wsdl_cache_ttl n'aura pas expiré.
- options
-
Un tableau d'options. Si l'on travaille en mode WSDL, ce paramètre est optionnel. En mode non-WSDL, vous devez définir les options location et uri, où location est l'URL à interroger et uri est l'espace de noms cible du service SOAP.
Les options style et use ne fonctionnent que dans le mode non-WSDL. En mode WSDL, ils viennent du fichier WSDL.
L'option soap_version spécifie si l'on doit utiliser le client SOAP version 1.1 ou 1.2.
Pour les identifications HTTP, vous devez utiliser les options login et password. Pour effectuer une connexion HTTP via un proxy, utilisez les options proxy_host, proxy_port, proxy_login et proxy_password. Pour les identifications à l'aide d'un certificat d'un client HTTPS, utilisez les options local_cert et passphrase.
L'option compression permet d'utiliser la compression sur les requêtes et les réponses HTTP SOAP.
L'option encoding définit le jeu d'encodage des caractères internes. Cette option ne modifie pas l'encodage des requêtes SOAP (il est toujours utf-8), mais convertit les chaînes en utilisant ce dernier.
L'option classmap peut être utilisée pour lier quelques types WSDL avec des classes PHP. Cette option doit être un tableau avec les types WSDL en tant que clés et les noms des classes PHP en tant que valeurs.
Définir l'option trace active l'utilisation des méthodes SoapClient->__getLastRequest, SoapClient->__getLastRequestHeaders, SoapClient->__getLastResponse et SoapClient->__getLastResponseHeaders.
L'option exceptions est un booléen définissant si les erreurs SOAP doivent lancer des exceptions du type SoapFault.
L'option connection_timeout définit le délai de connexion en secondes pour la connexion au service SOAP. Cette option ne définit pas un délai de connexion pour les services avec des réponses lentes. Pour limiter la durée d'attente de fin des appels, l'option default_socket_timeout est disponible.
L'option typemap est un tableau dont les clés sont type_name, type_ns (URI de l'espace de noms), from_xml (fonction de rappel acceptant un paramètre de type chaîne) et to_xml (fonction de rappel acceptant un paramètre de type objet).
Les autres options sont stream_context, features, cache_wsdl et user_agent.
Exemples
Exemple #1 Exemple avec SoapClient
<?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")));
?>
SoapClient->__construct()
28-Sep-2008 08:46
16-Sep-2008 06:23
This took me a while to figure out.
As described at the bottom of here: http://bugs.php.net/bug.php?id=38703
having xdebug may interfere with the constructor throwing exceptions
10-Sep-2008 03:53
To connect PHP SOAP to MS SOAP (CRM/EXCHANGE/...) I have created some classes using the explanation below and in other places.
www.reutone.com/heb/articles.php?instance_id=62&actions=show&id=521
19-Jun-2008 03:48
SoapFault exception: [Client] looks like we got no XML document in <document> has been already mentioned to occur when your server outputs something before <?xml ... > tag.
For all those having problems with that, and no access to the server code:
This is how to make a proxy that would clean responses for You
<?php
/**
* Simple class taken from a note by James Ellis [in __doRequest() page of manual]
*/
class Proxy_Client extends SoapClient {
protected $cacheDocument = "";
public function __construct($wsdl, $options) {
parent::__construct($wsdl, $options);
}
/**
* SetCacheDocument() sets the previously cached document contents
*/
public function SetCacheDocument($document) {
$this->cacheDocument = $document;
}
/**
* __doRequest() overrides the standard SoapClient to handle a local request
*/
public function __doRequest() {
return $this->cacheDocument;
}
}
//put this code in your function or wherever You have all required variables set
$client = new SoapClient($wsdl_url,$settings_array);
$void=$client->$method($params); //call this to get response from server
$response_string=$client->__getLastResponse();
//this part removes stuff
$start=strpos($response_string,'<?xml');
$end=strrpos($response_string,'>');
$response_string=substr($response_string,$start,$end-$start+1);
//get your proxy prepared
$proxy = new Proxy_Client($wsdl_url,$settings_array);
//and fill it with the server's response
$proxy->SetCacheDocument($response_string);
$and_finally_the_result_is=$proxy->$method($params);
print_r($and_finally_the_result_is); //this allows You to see what's there
?>
$method is the method's name eg. $method='getVersion';
$params - typical params for a soap method
09-Jun-2008 09:44
When using classmap option to map the SOAP results to a class, the constructor
of the object you've mapped to is not called.
$client = new SoapClient("url_to_wsdl",
array('classmap' => array('contact' => "Contact"));
$params = array("1");
$contact = $client->__soapCall("get_contact", $params);
Expected result:
A contact object that has properties initialized (i.e. db connections,
....).
Actual result:
A contact object without the properties.
Thanks for your help.
David Georges.
27-May-2008 07:23
> When using HTTP basic authentication, PHP will only send
> the credentials when invoking the service, not when
> fetching the WSDL.
The same goes for using an SSL client certficate, the SoapClient will only present the certificate on the actual remote call, not when getting the WSDL. The workaround is the same as above. HttpRequest works as expected.
30-Apr-2008 10:24
The "cache_wsdl" option takes constants like WSDL_CACHE_NONE or WSDL_CACHE_DISK that are listed on the "SOAP constants" page -> /manual/en/soap.constants.php
30-Jan-2008 02:10
A note regarding boolean values that may seem obvious on reflection but could be a gotcha for some:
Seeing a SOAP request example with <SomeBooleanParam>true</SomeBooleanParam> may lead you to pass in string "true" or "false" as the parameter, which is incorrect - the correct method is to use boolean data types.
<?php
$client = new SoapClient($wsdl,$options);
$method = "DoSomething";
$params = new stdClass;
$params->SomeBooleanParam = TRUE;
$client->$method($params);
/**
simplified request snippet would be
<SomeBooleanParam>true</SomeBooleanParam>
**/
//this will also be correct, but not for the right reasons:
$params->SomeBooleanParam = "true";
$client->$method($params);
/**
simplified request snippet would be
<SomeBooleanParam>true</SomeBooleanParam>
**/
//this is where you may be wondering what is going on
$params->SomeBooleanParam = "false";
$client->$method($params);
/**
simplified request snippet would be
<SomeBooleanParam>true</SomeBooleanParam>
**/
//you need to do this instead
$params->SomeBooleanParam = FALSE;
$client->$method($params);
/**
simplified request snippet would be
<SomeBooleanParam>false</SomeBooleanParam>
**/
?>
Hope that helps!
20-Dec-2007 11:22
If you want to use compression there is a missing parameter in the example above:
<?php
$client = new SoapClient("some.wsdl",
array('compression' => SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP | 5));
?>
If you use SOAP_COMPRESSION_GZIP you have to add the compression level. Otherwise it will not compress the request. Took me some hours ;)
Stephan
08-Jul-2007 08:46
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>
08-Jul-2007 08:28
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!
08-Feb-2007 04:20
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));
30-Nov-2006 09:26
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.
01-Mar-2006 01:57
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
22-Dec-2005 03:40
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
28-Aug-2005 11:31
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.
12-Aug-2005 08:31
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
11-Jul-2005 09:56
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.
04-Mar-2005 11:30
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.
