使用PHP SoapClient调用Web服务 - 如何操作?

时间:2009-04-19 22:54:16

标签: php web-services soap

我开始疯狂的Web服务。我试图使用PHP调用以下WSDL并继续无处可寻:

http://webservices.sabre.com/wsdl/sabreXML1.0.00/usg/SessionCreateRQ.wsdl

我在网上找到了以下一段代码,来自有类似问题的人,但我无法让它工作:

$soap = new SoapClient('http://webservices.sabre.com/wsdl/sabreXML1.0.00/usg/SessionCreateRQ.wsdl', array( 'trace' => true, 'soap_version' => SOAP_1_2, "exceptions" => 0));

$eb = new EbXmlMessage();
$sec = new Security();
$scrq = new SessionCreateRQ();

try {
$omg = $soap->SessionCreateRQ($scrq, $sec,$eb);
}
catch (Exception $e)
{
print_r($e);
}
//debug
print "Request: \n".
htmlspecialchars($soap->__getLastRequestHeaders()) ."\n";
print "Request: \n".
htmlspecialchars($soap->__getLastRequest()) ."\n";
print "Response: \n".
$soap->__getLastResponseHeaders()."\n";
print "Response: \n".
$soap->__getLastResponse()."\n";

print_r($omg);
//the first envelope headers
class EbXmlMessage
{
public $From = array('PartyId' => 'mysite.com');
public $To = array('PartyId' => 'myprovider.com');
public $CPAId = 'ZZZZ';
public $ConversationId = 'myconv@id.com';
public $Service = 'Session';// or SessionCreate?
public $Action = 'SessionCreateRQ';
public $MessageData = array( 'MessageId' => 'messageid', 'Timestamp' => '2009-04-18T15:15:00Z');


}
//the security token
class Security {
public $Username = "xxxxx";
public $Password = "yyyyy";
public $Organization = "ZZZZ";
public $Domain = "DEFAULT";
}
//this is suppoused to be the payload, or the xml i need to send at the end
class SessionCreateRQ
{
public $POS = array(
'Source' => array(
'_'=>"",
'PseudoCityCode'=>'ZZZZ'
));
}


我一直收到以下错误:

响应: HTTP / 1.1 500内部服务器错误 SOAPAction:“” Content-Type:text / xml; charset = utf-8 日期:太阳,2009年4月19日22:21:34 GMT 连接:关闭 服务器:SWS

回应:

  

soap-env:要内化的Client.InvalidEbXmlMessageUnable   messagejavax.xml.soap.SOAPException:无法内部化消息   com.sun.xml.messaging.saaj.soap.MessageImpl。(MessageImpl.java:135)     在   com.sun.xml.messaging.saaj.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:32)     在   com.sabre.universalservices.gateway.control.SoapProcessor.getRequest(SoapProcessor.java:263)     在   com.sabre.universalservices.gateway.control.WSGateway.handleRequest(WSGateway.java:380)     在   com.sabre.universalservices.gateway.control.WSGateway.doPost(WSGateway.java:306)     在javax.servlet.http.HttpServlet.service(HttpServlet.java:710)at   javax.servlet.http.HttpServlet.service(HttpServlet.java:803)at at   org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)     在   org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)     在   org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)     在   org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)     在   org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)     在   org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)     在   org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)     在   org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)     在   org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)     在   org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:852)     在   org.apache.coyote.http11.Http11AprProtocol $ Http11ConnectionHandler.process(Http11AprProtocol.java:584)     在   org.apache.tomcat.util.net.AprEndpoint $ Worker.run(AprEndpoint.java:1508)     在java.lang.Thread.run(Thread.java:595)引起:   javax.xml.soap.SOAPException:无效   内容类型:application / soap + xml at   com.sun.xml.messaging.saaj.soap.MessageImpl.verify(MessageImpl.java:159)     在   com.sun.xml.messaging.saaj.soap.MessageImpl。(MessageImpl.java:91)     ......还有19个

     

SoapFault对象(       [message:protected] =>无法内化消息       [string:private] => .....

这个服务应该在系统上验证我并返回一个安全对象,以便在以后的调用中使用 - 一个字符串(?),然后我可以将它存储在一个会话变量中,用于以下调用。

任何帮助都非常感谢!!!

4 个答案:

答案 0 :(得分:1)

尝试使用 wsdl2php 。它使wsdl文件中的php类。它使用php的SoapClient发送数据。

这是一篇很好的帖子,解释了如何做到这一点:

http://itworkarounds.blogspot.com/2011/10/simple-soap-client-with-wsdl2php-using.html

答案 1 :(得分:0)

我注意到的一件事是SoapFault对象中有 faultcode 值:

[faultcode] => soap-env:Client.InvalidEbXmlMessage

这可能是开始调试的有用途径。

我尝试将您的EbXmlMessage的结构与XSDschema documentation进行比较,但我看不出有任何明显的原因导致它被宣布无效。

答案 2 :(得分:0)

您是否尝试将Content-type标头更改为text / xml?

答案 3 :(得分:0)

只需使用nuSOAP即可。我不喜欢PHP原生的SoapClient。 nuSoap为你生成一个wsdl,所以你不必担心如何创建一个...这里是nuSOAP,这是一个简单的示例代码,或者你可以下载整个工作代码here

服务器

<?php
// include the SOAP classes
require_once('nuSOAP/lib/nusoap.php');

function HelloWorld(){
return 'HelloWorld'; // Returns HelloWorld string
}

function Hello($name){
return 'Hello '.$name; // Returns Hello with name string parameter
}



// create the server object
$server = new nusoap_server();

// Initialize WSDL support
$server->configureWSDL('webservicenamespace', 'urn:webservicenamespace');



$server->register('HelloWorld',             array(), array('result' => 'xsd:string')); //xsd:string; xsd:boolean; xsd:integer and so on..
$server->register('Hello',                  array('name' => 'xsd:string'), array('result' => 'xsd:string')); // array('parametername' => 'parametertype'),array('result' => 'returntype');


if (isset($error))
{
    $fault =
            $server->fault('soap:Server','',$error);
}

// send the result as a SOAP response over HTTP $HTTP_RAW_POST_DATA
$post = file_get_contents('php://input');
$server->service($post);
?>

客户

<?php
// Pull in the NuSOAP code
require_once('nuSOAP/lib/nusoap.php');

// Create the client instance

$client = new nusoap_client('http://pathtourl/sample_webservice.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')); // Call function name, parameters;

// 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>';
?>

现在当你想创建一个客户端时,你需要你的wsdl,你可以通过在你的链接上添加?wsdl 来获得它,即( webservice.php?wsdl )< / p>

希望这会有所帮助:)祝你的网络服务好运。