如何在php中发送SOAP请求

时间:2015-06-13 12:02:35

标签: php python soap tornado

好的,所以我有一个运行的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中是否有这样的易用性,或者有人可以向我展示一个会返回相同结果的示例代码吗?

考虑到服务器收到此信息:

class Input(complextypes.ComplexType):
    dpt = str 
    arr = str

并返回航班列表

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

这是网络服务:

@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 )

1 个答案:

答案 0 :(得分:1)

这个解决方案对我有用,我实例化了一个关联数组,其中包含我在服务器端期望的变量名称,然后使用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);
?>