How to send SOAP requests in php

458 views Asked by At

Okay so I have a Python server running and I've been using "suds" as a client side which has been surprisingly easy, when I tried to run a similar code in PHP I got very confused since I was a beginner with it?

from suds.client import Client

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

is there something with this ease in php or can anyone show me a sample code which would return the same result?

Considering the server receives this:

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

and returns a list of flights

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

this is the webservice:

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

my PHP segment:

<?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 Error:

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

Exception info:

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

There are 1 answers

2
Newbie On BEST ANSWER

this solution worked for me, i instantiated an associative array with the name of variables i'm expecting at server side then used var_dump to get the result of the output

<?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);
?>