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

gzszwxb4  于 2022-11-15  发布在  PHP
关注(0)|答案(4)|浏览(209)

I am going crazy starting off with Web Services. I am trying to call the following WSDL using PHP and keep getting nowhere:
http://webservices.sabre.com/wsdl/sabreXML1.0.00/usg/SessionCreateRQ.wsdl
I found the following piece of code on the net, from someone with similar problems, but I could not get it to work either:
$soap = new SoapClient(' [http://webservices.sabre.com/wsdl/sabreXML1.0.00/usg/SessionCreateRQ.wsdl](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'
));
}

`
I keep getting the following error:
Response: HTTP/1.1 500 Internal Server Error SOAPAction: "" Content-Type: text/xml;charset=utf-8 Date: Sun, 19 Apr 2009 22:21:34 GMT Connection: close Server: SWS
Response:
soap-env:Client.InvalidEbXmlMessageUnable to internalize messagejavax.xml.soap.SOAPException: Unable to internalize message at com.sun.xml.messaging.saaj.soap.MessageImpl.(MessageImpl.java:135) at com.sun.xml.messaging.saaj.soap.MessageFactoryImpl.createMessage(MessageFactoryImpl.java:32) at com.sabre.universalservices.gateway.control.SoapProcessor.getRequest(SoapProcessor.java:263) at com.sabre.universalservices.gateway.control.WSGateway.handleRequest(WSGateway.java:380) at com.sabre.universalservices.gateway.control.WSGateway.doPost(WSGateway.java:306) at javax.servlet.http.HttpServlet.service(HttpServlet.java:710) at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263) at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:852) at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:584) at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1508) at java.lang.Thread.run(Thread.java:595) Caused by: javax.xml.soap.SOAPException: Invalid Content-Type:application/soap+xml at com.sun.xml.messaging.saaj.soap.MessageImpl.verify(MessageImpl.java:159) at com.sun.xml.messaging.saaj.soap.MessageImpl.(MessageImpl.java:91) ... 19 more
SoapFault Object ( [message:protected] => Unable to internalize message [string:private] => .....
This service should be validating me on the system and returning a security object to be used in later calls - a string(?) which I can then store in a session variable for the following calls.
Any help GREATLY appreciated!!!

4szc88ey

4szc88ey1#

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

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

因此,这可能是开始调试的有用途径。
我尝试将EbXmlMessage的结构与XSDschema documentation进行比较,但我看不出任何明显的原因来说明它被声明为无效。

weylhg0b

weylhg0b2#

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

jdgnovmf

jdgnovmf3#

尝试使用wsdl2php。它从wsdl文件中生成php类。它使用php的SoapClient来发送数据。
这里有一个很好的职位解释如何做到这一点:
http://itworkarounds.blogspot.com/2011/10/simple-soap-client-with-wsdl2php-using.html

at0kjp5o

at0kjp5o4#

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

服务器

<?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
希望这对你有帮助:)祝你的网络服务好运。

相关问题