PHP 8.1.31 Released!

La clase SoapClient

(PHP 5, PHP 7, PHP 8)

Introducción

La clase SoapClient proporciona un cliente para servidores » SOAP 1.1, » SOAP 1.2. Puede ser usada en modo WSDL o modo no-WSDL.

Sinopsis de la Clase

class SoapClient {
/* Métodos */
public __call(string $function_name, array $arguments): mixed
public __construct(mixed $wsdl, array $options = ?)
public __doRequest(
    string $request,
    string $location,
    string $action,
    int $version,
    int $one_way = 0
): string
public __getTypes(): array
public __setCookie(string $name, string $value = ?): void
public __setLocation(string $new_location = ?): string
public __setSoapHeaders(mixed $soapheaders = ?): bool
public __soapCall(
    string $function_name,
    array $arguments,
    array $options = ?,
    mixed $input_headers = ?,
    array &$output_headers = ?
): mixed
}

Tabla de contenidos

add a note

User Contributed Notes 9 notes

up
23
hugues at zonereseau dot com
13 years ago
When you need to connect to services requiring to send extra header use this method.

Here how we can to it with PHP and SoapClient

<?php
class exampleChannelAdvisorAuth
{
public
$DeveloperKey;
public
$Password;

public function
__construct($key, $pass)
{
$this->DeveloperKey = $key;
$this->Password = $pass;
}
}

$devKey = "";
$password = "";
$accountId = "";

// Create the SoapClient instance
$url = "";
$client = new SoapClient($url, array("trace" => 1, "exception" => 0));

// Create the header
$auth = new ChannelAdvisorAuth($devKey, $password);
$header = new SoapHeader("http://www.example.com/webservices/", "APICredentials", $auth, false);

// Call wsdl function
$result = $client->__soapCall("DeleteMarketplaceAd", array(
"DeleteMarketplaceAd" => array(
"accountID" => $accountId,
"marketplaceAdID" => "9938745" // The ads ID
)
),
NULL, $header);

// Echo the result
echo "<pre>".print_r($result, true)."</pre>";
if(
$result->DeleteMarketplaceAdResult->Status == "Success")
{
echo
"Item deleted!";
}
?>
up
1
spam at kacke dot de
2 years ago
In case a soap response returns nodes with the attribute xsi:nil="false", they are currently not processed by php-soapclient.

This seems to be a bug (not correct).

Workaround:

class mySoapClient extends SoapClient {
public function __doRequest($request, $location, $action, $version, $one_way = 0) {
$response = parent::__doRequest($request, $location, $action, $version, $one_way);
$response = str_replace('xsi:nil="false"',"",$response);
return $response;
}
}

Took me a day. Hope it can help
Toppi
up
1
Stefan
10 years ago
There is a known bug with some versions of Xdebug which can cause SoapClient to not throw an exception but instead cause a fatal error.

Surround the SoapClient call with xdebug_disable(); and xdebug_enable(); to work around this problem.

For reference:

http://bugs.xdebug.org/view.php?id=249
https://bugs.php.net/bug.php?id=47584
up
0
info at nospam x valiton x com
9 years ago
CAUTION:
I had quite a bit of trouble trying to make a request with fopen through a proxy to a secure url. I kept getting a 400 Bad Request back from the remote host. It was receiving the proxy url as the SNI host. In order to get around this I had to explicity set the SNI host to the domain I was trying to reach. It's apparently the issue outlined in this bug:

https://bugs.php.net/bug.php?id=63519

<?php
$domain
= parse_url($file, PHP_URL_HOST);
$proxy_string = "tcp://" . WP_PROXY_HOST . ":" . WP_PROXY_PORT;
$opts = array(
'http' => array( 'proxy' => $proxy_string ),
'ssl' => array( 'SNI_enabled' => true, 'SNI_server_name' => $domain));
$context = stream_context_create($opts);
$handle = fopen( $file, 'r', false, $context );
?>

src:
http://php.net/manual/en/context.http.php#114314
up
-1
Ricardo Pedrassani
8 years ago
If the XML have identities with same name in different levels there is a solution. You don´t have to ever submit a raw XML (this PHP SOAP object don´t allows send a RAW XML), so you have to always translate your XML to a array, like the example below:

$originalXML = "
<xml>
<firstClient>
<name>someone</name>
<adress>R. 1001</adress>
</firstClient>
<secondClient>
<name>another one</name>
<adress></adress>
</secondClient>
</xml>"

//Translate the XML above in a array, like PHP SOAP function requires
$myParams = array('firstClient' => array('name' => 'someone',
'adress' => 'R. 1001'),
'secondClient' => array('name' => 'another one',
'adress' => ''));

$webService = new SoapClient($someURL);
$result = $webService->someWebServiceFunction($myParams);
up
-1
peter dot hansen at fastit dot net
15 years ago
When you get errors like:
"Fatal error: Uncaught SoapFault exception: [HTTP] Error Fetching http headers in"
after a few (time intensive) SOAP-Calls, check your webserver-config.

Sometimes the webservers "KeepAlive"-Setting tends to result in this error. For SOAP-Environments I recommend you to disable KeepAlive.

Hint: It might be tricky to create a dedicated vhost for your SOAP-Gateways and disable keepalive just for this vhost because for normal webpages Keepalive is a nice speed-boost.
up
-3
acopantepuy at gmail dot com
9 years ago
when they want to pass variables into the http header that is how it is done:

<?php

$aHTTP
['http']['header'] = "User-Agent: PHP-SOAP/5.5.11\r\n";

$aHTTP['http']['header'].= "username: XXXXXXXXXXX\r\n"."password: XXXXX\r\n";

$context = stream_context_create($aHTTP);

$client=new SoapClient("https://ocppws-cert.extra.bcv.org.ve:443/AltoValor/BancoUniversal?WSDL",array('trace' => 1,"stream_context" => $context));

$result = $client->jornadaActiva();
var_dump($result);
?>
up
-3
jjlopez
13 years ago
If you are making soap calls in WSDL mode , and the address of your web service includes a port different from 80 (like http://my_ip_address:8080//service.asmx?wsdl), the WSDL file is fetched correctly, but all subsequent requests are made without any port in the host field. This causes a SoapFault exception when trying to call any of the service’s methods.

You need to redefine the soapClient class and force the port in each call.

See this example:

http://www.victorstanciu.ro/php-soapclient-port-bug-workaround/
up
-4
mcinantyspam at fejm dot pl
8 years ago
Please note, that if you provide values that contain illegal xml characters (ASCII codes 0-8, 11-12, 14-15 - or x0-x8, xB-xC, xE-xF in hex), php's SoapClient will do send them in request, although such request is improper because it does not meet XML 1.0 requirements.
You always have to replace or remove these characters in your data before supplying them to SoapClient
To Top