如何用php发送SOAP请求

2024-09-28 01:31:11 发布

您现在位置:Python中文网/ 问答频道 /正文

好吧,我有一个Python服务器在运行,我一直在使用“suds”作为客户端,这非常简单,当我试图在PHP中运行类似的代码时,我很困惑,因为我还是个初学者?在

from suds.client import Client

url = 'http://localhost:8080/FlightService?wsdl'
client = Client(url=url)
output = client.service.getFlightList("dxb","ksa")
print(output)

在php中有没有这样简单的东西,或者有人能给我看一个返回相同结果的示例代码吗?在

考虑到服务器接收到:

^{pr2}$

并返回航班列表

class flight(complextypes.ComplexType):
    id    = int
    dpt  = str
    arr  = str
    price = float
    date = str
    tickets = int

这是Web服务:

@webservice(_params=Input,_returns=[flight])
def getFlightList(self, input):

我的PHP段:

<?php   
    $url = 'http://172.27.130.98:8080/FlightService?wsdl';
    $client = new SoapClient($url);
    echo("Hello!");
    $result = $client->bookFlight("dxb","ksa");
    $result2 = $client->handle();
    echo($result);
    echo($result2);
?>

PHP错误:

Fatal error: Uncaught SoapFault exception: [HTTP] Error Fetching http headers in C:\wamp\www\soap\soap.php:6 Stack trace: #0 [internal function]: SoapClient->__doRequest('<?xml version="...', 'http://172.27.1...', 'http://172.27.1...', 1, 0) #1 C:\wamp\www\soap\soap.php(6): SoapClient->__call('getFlightList', Array) #2 C:\wamp\www\soap\soap.php(6): SoapClient->getFlightList('dxb', 'ksa') #3 {main} thrown in C:\wamp\www\soap\soap.php on line 6

异常信息:

SoapFault Object ( [message:protected] => Error Fetching http headers [string:Exception:private] => [code:protected] => 0 [file:protected] => C:\wamp\www\soap\soap.php [line:protected] => 6 [trace:Exception:private] => Array ( [0] => Array ( [function] => __doRequest [class] => SoapClient [type] => -> [args] => Array ( [0] => ksa [1] => http://172.27.130.98:8080/FlightService [2] => http://172.27.130.98:8080/FlightService/getFlightList [3] => 1 [4] => 0 ) ) [1] => Array ( [file] => C:\wamp\www\soap\soap.php [line] => 6 [function] => __call [class] => SoapClient [type] => -> [args] => Array ( [0] => getFlightList [1] => Array ( [0] => dxb [1] => ksa ) ) ) [2] => Array ( [file] => C:\wamp\www\soap\soap.php [line] => 6 [function] => getFlightList [class] => SoapClient [type] => -> [args] => Array ( [0] => dxb [1] => ksa ) ) ) [previous:Exception:private] => [faultstring] => Error Fetching http headers [faultcode] => HTTP [xdebug_message] => ( ! ) SoapFault: Error Fetching http headers in C:\wamp\www\soap\soap.php on line 6 Call Stack #TimeMemoryFunctionLocation 10.0006243160{main}( )..\soap.php:0 20.0758264872getFlightList ( )..\soap.php:6 30.0758265336__call ( )..\soap.php:6 )

Tags: clienthttpurlwwwlineerrorarraysoap
1条回答
网友
1楼 · 发布于 2024-09-28 01:31:11

这个解决方案对我有效,我用服务器端期望的变量名实例化了一个关联数组,然后使用var_dump来获得输出结果

<?php   
    $url = 'http://172.27.130.98:8080/FlightService?wsdl';
    $client = new SoapClient($url);
    echo("Hello!");
    $trip["dpt"] = "dxb";
    $trip["arr"] = "krt";
try{    $output = $client->getFlightList($trip);
    }
    catch(soapFault $e)
    {
    print_r($e);
    }
    var_dump($output);
?>

相关问题 更多 >

    热门问题