文字

The SoapClient class

(PHP 5 >= 5.0.1)

简介

The SoapClient class provides a client for » SOAP 1.1, » SOAP 1.2 servers. It can be used in WSDL or non-WSDL mode.

类摘要

SoapClient {
public mixed __call ( string $function_name , string $arguments )
public SoapClient ( mixed $wsdl [, array $options ] )
public string __doRequest ( string $request , string $location , string $action , int $version [, int $one_way = 0 ] )
public array __getFunctions ( void )
public string __getLastRequest ( void )
public string __getLastRequestHeaders ( void )
public string __getLastResponse ( void )
public string __getLastResponseHeaders ( void )
public array __getTypes ( void )
public void __setCookie ( string $name [, string $value ] )
public string __setLocation ([ string $new_location ] )
public bool __setSoapHeaders ([ mixed $soapheaders ] )
public mixed __soapCall ( string $function_name , array $arguments [, array $options [, mixed $input_headers [, array &$output_headers ]]] )
public SoapClient ( mixed $wsdl [, array $options ] )
}

Table of Contents

  • SoapClient::__call — Calls a SOAP function (deprecated)
  • SoapClient::__construct — SoapClient constructor
  • SoapClient::__doRequest — Performs a SOAP request
  • SoapClient::__getFunctions — Returns list of available SOAP functions
  • SoapClient::__getLastRequest — Returns last SOAP request
  • SoapClient::__getLastRequestHeaders — Returns the SOAP headers from the last request
  • SoapClient::__getLastResponse — Returns last SOAP response
  • SoapClient::__getLastResponseHeaders — Returns the SOAP headers from the last response
  • SoapClient::__getTypes — Returns a list of SOAP types
  • SoapClient::__setCookie — The __setCookie purpose
  • SoapClient::__setLocation — Sets the location of the Web service to use
  • SoapClient::__setSoapHeaders — Sets SOAP headers for subsequent calls
  • SoapClient::__soapCall — Calls a SOAP function
  • SoapClient::SoapClient — SoapClient constructor

用户评论:

[#1] deepesh [2015-11-26 08:12:08]

$client = new SoapClient('xxxxxx');
  $result =$client->GetProductTypeForPromotionCodeMaster(array("CompanyID" =>37));
 $res= json_encode($result);

How to further print it in table??

[#2] info at nospam x valiton x com [2015-04-13 08:06:28]

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($filePHP_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

[#3] acopantepuy at gmail dot com [2015-04-11 02:45:46]

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);
?>

[#4] acopantepuy at gmail dot com [2015-04-11 02:42:21]

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: C1040001760102\r\n"."password: bcv2020\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);
?>

[#5] romuloum at hotmail dot com [2015-02-19 16:22:26]

After migrating to PHP 5.6.5, the soap 1.2 did not work anymore. So I solved the problem by adding optional parameters SSL.
My error: failed to load external entity
How to solve:
// options for ssl in php 5.6.5
$opts = array(
    'ssl' => array('ciphers'=>'RC4-SHA', 'verify_peer'=>false, 'verify_peer_name'=>false)
);
// SOAP 1.2 client
$params = array ('encoding' => 'UTF-8', 'verifypeer' => false, 'verifyhost' => false, 'soap_version' => SOAP_1_2, 'trace' => 1, 'exceptions' => 1, "connection_timeout" => 180, 'stream_context' => stream_context_create($opts) );
$oSoapClient = new SoapClient ( $url . "?WSDL", $params );

[#6] wira dot sanjaya dot saputro at gmail dot com [2015-01-05 11:59:29]

Hi,

can you more specified how to solve this?
i also stuck in here.

Thanks

[#7] softontherocks at gmail dot com [2014-11-22 08:32:11]

I posted in this URL http://softontherocks.blogspot.com/2014/02/web-service-soap-con-php.html a full example of a nusoap web service.

There is defined the server and the cliente who calls the web service.

I hope it would be useful for you.

[#8] martin dot schenck at plugsurfing dot com [2014-09-15 15:34:26]

If you want to connect to a server that only supports SSLv2/3 and/or TLS 1.0 (no TLS 2 or 3), tell the SOAP client if you get a connection error by setting the appropriate stream context:
<?php
  $opts 
= array(
    
'ssl' => array('ciphers'=>'RC4-SHA')
  );

  
$objSoapClient = new SoapClient(
    
'https://example.com/?wsdl',
    array ( 
"encoding"=>"ISO-8859-1",
    
'stream_context' => stream_context_create($opts)
    
// your options
    
);
?>


You might need to disable the SOAP caching of WSDLs in order for the SOAP client to use the new context.

[#9] Stefan [2014-06-03 11:04:00]

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

[#10] Anonymous [2013-09-12 21:25:01]

I got the following error when trying to load the WSDL file:

Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: <message> 'MessageName' already defined

In my case there was no double definitions but it was including other files - I think one of them contained a duplicate message with a different namespace (which SoapClient doesn't like). 

I resolved it by loading the WSDL in SoapUI, right-clicking and selecting "Export definition". The WSDL that then got created worked fine. Hope this helps somebody.

[#11] Guillermo Prandi [2012-02-20 14:10:37]

To make an HTTPS call with a client certificate, you can do it this way:

1) Create a file containing both the key and the signed certificate. I did this by concatenating my (.pem) key and the certificate I've got signed from my CA (also .pem format). The file looked like this:

-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKagQC1N27Ilb9pWil2NaX2qM8FquXBXK5T1AydOv7sCotsc8MAwbi7
......(snip)......
wAiOCD4K9TyMS76pIS8UyfJl/oIrn7EF24BUpaUfsh8=
-----END RSA PRIVATE KEY-----
-----BEGIN CERTIFICATE-----
MIIC/zCCAmigAwIBAgIEAIl1JzANBgkqhkiG9w0BAQQuADBQMQswCQYxVQQGEaJV
......(snip)......
eSPds1hLKYSg0bd3uI7LhaDLOC1PPgb77sYe/uYkUWuHBzllts5x/pfue0zaIBKG
Omjy
-----END CERTIFICATE-----

2) Reference your certificate in the creation of the SOAP client, like this:

$protected_url = "https://my-server/?wsdl";
$my_cert_file = "/my/path/to/mycert.pem";

$client = new SoapClient($protected_url, array('local_cert', $my_cert_file) );

[#12] egiardina at badatmath dot com [2011-08-03 18:05:31]

This one drove me nuts... if you are connecting to a web service run on Cassini, (Visual Studio's web server) from php, you may not be able to call web service functions or load a WSDL out-of-the-box. 

My set up is that I am using xampp on my windows development machine, and also using Visual Studio's built in web-server. I built a web service in visual studio. I wrote a simple PHP script, running under xampp, to access this service, so the two projects can 'talk' to each other. What I found was the WSDL was never loaded. 

So I moved the WSDL local to the PHP file and accessed it directly. Still no calls. Turns out, PHP's SoapClient seems to have a problem with 'localhost' as a web service endpoint. So I hand-edited the web service endpoint URL to read 127.0.0.1 instead of localhost, and Voila! Web Service calls work:

Here is my PHP, now working:
<?php
$client 
= new SoapClient("http://localhost/code/soap.wsdl");
$something =  $client->HelloWorld(array());
echo 
$something->HelloWorldResult;
die();

?>


Here is my modified WSDL:
  <wsdl:service name="MyService">
    <wsdl:port name="MyServiceSoap" binding="tns:MyServiceSoap">
      <soap:address location="http://127.0.0.1:55274/MyService/MyService.asmx" />
....

    </wsdl:port>

[#13] stepan dot zarubin at gmail dot com [2011-06-30 11:51:23]

Well, this example works fine:

<?php
try {  
    
$x = @new SoapClient("non-existent.wsdl");  
} catch (
Exception $e) {  
    echo 
$e->getMessage();

?>


Just make sure use NEW with @.

[#14] jjlopez [2011-03-09 11:36:12]

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/

[#15] hugues at zonereseau dot com [2011-02-17 21:17:31]

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"$authfalse);

// 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($resulttrue)."</pre>";
if(
$result->DeleteMarketplaceAdResult->Status == "Success")
{
    echo 
"Item deleted!";
}
?>

[#16] fourat dot zouari at tritux dot com [2009-06-20 08:08:55]

In addition to the KeepAlive trick which is a "server-side" modification, on the "client side" default_socket_timeout should be increased from its default value (60) when you deal with ~slow SOAP servers.
As for the KeepAlive, if creating a new separate vhost for the soap api is not possible, you can add this to your existing vhost: BrowserMatch "^PHP-SOAP" nokeepalive
where PHP-SOAP is the agent name of your soap client, if you dont know what agent name your client use, just checkout the access.log of your apache.

[#17] peter dot hansen at fastit dot net [2009-05-01 06:57:14]

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.

上一篇: 下一篇: