PHP / SOAP - 尝试学习,但在实施时遇到问题
我正在尝试自学 SOAP,只是为了稍微扩展我的技能,但我遇到了困难,我想知道是否有善良的开发人员可以提供帮助?
我已经设置了我的服务器:
http://www.domain1.com/server.php
<?php
// Pull in the NuSOAP code
require_once('soap/nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('hellowsdl', 'urn:hellowsdl');
// Register the method to expose
$server->register('hello', // method name
array('name' => 'xsd:string'), // input parameters
array('return' => 'xsd:string'), // output parameters
'urn:hellowsdl', // namespace
'urn:hellowsdl#hello', // soapaction
'rpc', // style
'encoded', // use
'Says hello to the caller' // documentation
);
// Define the method as a PHP function
function hello($name) {
return 'Hello, ' . $name;
}
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
现在我尝试在单独的服务器上设置客户端:
http://www. domain2.com/client.php
<?php
// Pull in the NuSOAP code
require_once('soap/nusoap.php');
// Create the client instance
$client = new soapclient('http://domain.com/server.php?wsdl', true);
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
// At this point, you know the call that follows will fail
}
// Call the SOAP method
$result = $client->call('hello', array('name' => 'Scott'));
// Check for a fault
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
// Display the request and response
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>
但我无法让这该死的东西工作。服务器显示得很好 - wdsl 输出了很多。但客户端无法/不会连接并完成交易。我收到这样的消息:
Warning: SoapClient::SoapClient() expects parameter 2 to be array, boolean given in /home/public_html/slidebank_soap_client.php on line 5
Fatal error: Uncaught SoapFault exception: [Client] SoapClient::SoapClient() [<a href='soapclient.soapclient'>soapclient.soapclient</a>]: Invalid parameters in /home/soap/slidebank_soap_client.php:5 Stack trace: #0 /home/soap/slidebank_soap_client.php(5): SoapClient->SoapClient('http://testing....', true) #1 {main} thrown in /home/public_html/soap/slidebank_soap_client.php on line 5
这就是我困惑的地方......
有什么想法吗?
H
I'm trying to teach myself SOAP, just to extend my skillset a bit, but I've hit a wall and I was wondering if a kind developer out there can help?
I've set up my server thus:
http://www.domain1.com/server.php
<?php
// Pull in the NuSOAP code
require_once('soap/nusoap.php');
// Create the server instance
$server = new soap_server();
// Initialize WSDL support
$server->configureWSDL('hellowsdl', 'urn:hellowsdl');
// Register the method to expose
$server->register('hello', // method name
array('name' => 'xsd:string'), // input parameters
array('return' => 'xsd:string'), // output parameters
'urn:hellowsdl', // namespace
'urn:hellowsdl#hello', // soapaction
'rpc', // style
'encoded', // use
'Says hello to the caller' // documentation
);
// Define the method as a PHP function
function hello($name) {
return 'Hello, ' . $name;
}
// Use the request to (try to) invoke the service
$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
?>
And now I've tried to set up a client on a seperate server:
http://www.domain2.com/client.php
<?php
// Pull in the NuSOAP code
require_once('soap/nusoap.php');
// Create the client instance
$client = new soapclient('http://domain.com/server.php?wsdl', true);
// Check for an error
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
// At this point, you know the call that follows will fail
}
// Call the SOAP method
$result = $client->call('hello', array('name' => 'Scott'));
// Check for a fault
if ($client->fault) {
echo '<h2>Fault</h2><pre>';
print_r($result);
echo '</pre>';
} else {
// Check for errors
$err = $client->getError();
if ($err) {
// Display the error
echo '<h2>Error</h2><pre>' . $err . '</pre>';
} else {
// Display the result
echo '<h2>Result</h2><pre>';
print_r($result);
echo '</pre>';
}
}
// Display the request and response
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';
?>
But I can't get the fecking thing working. The server displays just fine - wdsl output the lot. But the client can't/won't connect and complete the transaction. I get this message:
Warning: SoapClient::SoapClient() expects parameter 2 to be array, boolean given in /home/public_html/slidebank_soap_client.php on line 5
Fatal error: Uncaught SoapFault exception: [Client] SoapClient::SoapClient() [<a href='soapclient.soapclient'>soapclient.soapclient</a>]: Invalid parameters in /home/soap/slidebank_soap_client.php:5 Stack trace: #0 /home/soap/slidebank_soap_client.php(5): SoapClient->SoapClient('http://testing....', true) #1 {main} thrown in /home/public_html/soap/slidebank_soap_client.php on line 5
And this is where I'm stumped...
Any ideas?
H
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个线程可能对您有帮助,特别是回答#9和#10。
This thread may help you, especially answer #9 and #10.
我曾经非常喜欢将 nusoap 与 PHP3/4 一起使用(即 PHP 没有内置 SOAP 客户端时),但是使用 PHP5 的 SOAP 客户端,我发现现在不再需要 nusoap 了。
PHP5 内置 SOAP 客户端的一个优点是它更清晰、更容易实现,因此提及它可能对您有帮助(我知道有些人选择使用 nusoap,但很多人显然不知道它的存在)在 PHP5 中,因为很多“HOWTO”指南已经过时并且忽略提及它)。
用法示例:
关于服务类型的注释:
当涉及 SOAP 服务时,理想情况下您希望实现文档/文字服务,而不是 RPC/Encoded 服务,因为 RPC/Encoded 是一种难以使用和使用的格式这个原因已被 WS-I 弃用,转而支持文档/文字(这更容易使用,而且设计更符合逻辑)。
首先,如果可以的话,您可能希望尝试针对现有服务实现客户端 - 这样您就知道它至少可以正常工作,这可能会为您省去一些麻烦。
不幸的是,尽管 PHP 在使用它们方面表现出色,但它对文档/文字服务的支持并不特别好。这个 IIRC 至少有一个类似 nusoap 的第三方库,但它并没有完全满足我的需求。
(如有过时,欢迎指正。)
I used to be a fan of using nusoap with PHP3/4 (i.e. back when PHP didn't have a built in SOAP client) but with PHP5's SOAP Client I find I don't have any need for nusoap these days.
An advantage PHP5's built in SOAP client is that it's much cleaner and simplier to implement and so mention it as it might be helpful to you (I know some choose to use nusoap instead, but a lot of people apparently just aren't aware it's there in PHP5 as a lot of "HOWTO" guides are out of date and neglect to mention it).
Example usage:
On a note regarding service types:
When it comes to a SOAP service, ideally you want to implement a Document/Literal service, rather than an RPC/Encoded service, as RPC/Encoded is a difficult format to work with and for that reason has been deprecated by the WS-I in favour of Document/Literal (which is both much easier to work with and more logical in design).
To start with, you might want to try implementing a client against an existing service if you can - that way you know it's at least working properly, which might save you a few headaches.
Unfortunately - while it's excellent at consuming them - PHP doesn't have particularly great support for serving Document/Literal services. There is at least one third party nusoap-like library for this IIRC, but it didn't quite cut it for me.
(If any of this is outdated, corrections welcome.)
以防万一一些谷歌用户发现这个...
我按照 ccheneson 的建议编辑了 php.ini,并且即使在同一个开发服务器上,该代码也运行得很好。
客户端 - 基于 PHP 模式的 Drupal 页面:
服务:
Just in case some googlers find this...
I edited the php.ini as ccheneson suggested and this code worked just fine, even on the same dev server.
Client - based in a Drupal page in PHP mode:
Service: