Page 1 of 1
Using PHP to call SOAP Web Service
Posted: Fri Mar 21, 2014 10:02 am
by TravisT6983
I will warn you that i have never used PHP to call a web service but have spent the last 2 days googling with trial and error and think that i have come to a point where i have no idea what to do as nothing is working.
Here is my PHP code...
Code: Select all
<?php
$soapclient = new SoapClient('Link-To-EndPoint');
$params = array('Username' => 'Username',
'Password' => 'Password',
'clientRequestId' => 1,
'projectNumber' => 64111,
'requestDateTime' => '2014-03-16T11:05:24.572Z',
'itemNumber' => 'F00573019120B',
'projectNumber' => 64111
);
$response = $soapclient->GetItemDetailInfo($params);
echo '<pre>';
var_dump($response);
echo '</pre>';
?>
And here is the request, probably worth noting that this gives the full response and works when i use SOAP UI to make the request.
Code: Select all
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header>
<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<UsernameToken>
<Username>Username</Username>
<Password>Password</Password>
</UsernameToken>
</Security>
</env:Header>
<env:Body>
<getItemDetailInfoRequest>
<requestHeader>
<clientRequestId>1</clientRequestId>
<projectNumber>64111</projectNumber>
<requestDateTime>2014-03-20T14:05:24.572Z</requestDateTime>
</requestHeader>
<getItemDetailInfoList>
<itemDetailSearchCriteria>
<itemNumber>F00573019120B</itemNumber>
<projectNumber>64111</projectNumber>
</itemDetailSearchCriteria>
</getItemDetailInfoList>
</getItemDetailInfoRequest>
</env:Body>
</env:Envelope>
When i load the page i get a blank screen nothing comes out from $response, I have no idea if i am close to making this work or if i am super far off with this. Using the above code i am able to make a public weather SOAP Web Service work which makes me think i am close.
Any help would be appreciated!
Re: Using PHP to call SOAP Web Service
Posted: Fri Mar 21, 2014 3:35 pm
by Christopher
Do you get any errors? You might want to put try/catch blocks around the lines where you instantiate SoapClient and where you call GetItemDetailInfo.
Re: Using PHP to call SOAP Web Service
Posted: Sat Mar 22, 2014 9:07 am
by cod3x
Hi,
I am new on PHP SOAP Web service. Can you please provide me basic code for client and server using authentication headers ?
What i want to achieve is, that on each call from client i have to check on server if client is authorized to do request. Please help me

....
Re: Using PHP to call SOAP Web Service
Posted: Sat Mar 22, 2014 11:14 am
by Christopher
You might want to start by trying this, to see if there are any errors:
Code: Select all
<?php
try {
$soapclient = new SoapClient('Link-To-EndPoint');
} catch (Exception $e) {
echo "Exception SoapClient: " . $e->getMessage();
}
try {
$params = array('Username' => 'Username',
'Password' => 'Password',
'clientRequestId' => 1,
'projectNumber' => 64111,
'requestDateTime' => '2014-03-16T11:05:24.572Z',
'itemNumber' => 'F00573019120B',
'projectNumber' => 64111
);
$response = $soapclient->GetItemDetailInfo($params);
echo '<pre>';
var_dump($response);
echo '</pre>';
} catch (Exception $e) {
echo "Exception GetItemDetailInfo: " . $e->getMessage();
}
Re: Using PHP to call SOAP Web Service
Posted: Sat Mar 22, 2014 2:41 pm
by cod3x
Thank you for your help. How about server .. how can i check header infos received from client ?
Thx again
Re: Using PHP to call SOAP Web Service
Posted: Sat Mar 22, 2014 11:11 pm
by Christopher
Re: Using PHP to call SOAP Web Service
Posted: Mon Mar 24, 2014 1:48 pm
by TravisT6983
Hey Chris i have looked a lot of there example but they seem to be simple examples and i was able to get a simple example to work from a public weather Web Service. This is the one that i was able to make work.
Code: Select all
<?php
//Create the client object
$soapclient = new SoapClient('http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL');
//Use the functions of the client, the params of the function are in
//the associative array
$params = array('ZIP' => '48067');
$response = $soapclient->GetCityForecastByZIP($params);
echo '<pre>';
var_dump($response);
echo '</pre>';
?>
All of that works but is VERY basic.
I have since posting this made what i believe are strides to making this work, but still isn't quite there below is my new code with the array sorted out and how i would assume the SOAP header needs to be called with the body.
This is the exception that i am getting when i use the below code now
Exception GetItemDetailInfo: SOAP-ERROR: Encoding: object has no 'requestHeader' property
But i am not sure if that is an error in my code or something isn't lining up in the web service.
Code: Select all
<?php
try {
$soapclient = new SoapClient('Link-To-WSDL');
} catch (Exception $e) {
echo "Exception SoapClient: " . $e->getMessage();
}
try {
$headerbody = array(
'UsernameToken' => array(
'Username' => 'Username',
'Password' => 'Password'
)
);
$paramas = array(
'getItemDetailInfoRequest' => array(
'requestHeader' => array(
'clientRequestId' => 1,
'projectNumber' => 64111,
'requestDateTime' => '2014-03-20T14:05:24.572Z'
),
'getItemDetailInfoList' => array(
'itemDetailSearchCriteria' => array(
'itemNumber' => 'F00573019120B',
'projectNumber' => 64111
)
)
)
);
//Create Soap Header.
$header = new SOAPHeader($soapclient, 'UsernameToken', $headerbody);
//set the Headers of Soap Client.
$headerResponse = $soapclient->__setSoapHeaders($header);
$response = $soapclient->GetItemDetailInfo($paramas);
echo '<pre>';
var_dump($headerResponse);
echo '</pre>';
echo '<pre>';
var_dump($response);
echo '</pre>';
echo '<pre>';
var_dump($paramas);
echo '</pre>';
} catch (Exception $e) {
echo "Exception GetItemDetailInfo: " . $e->getMessage();
}
?>
Any ideas how to get me to where i need to be?
Thank you very much
Travis
Re: Using PHP to call SOAP Web Service
Posted: Mon Mar 24, 2014 3:30 pm
by Christopher
TravisT6983 wrote:This is the exception that i am getting when i use the below code now
Exception GetItemDetailInfo: SOAP-ERROR: Encoding: object has no 'requestHeader' property
But i am not sure if that is an error in my code or something isn't lining up in the web service.
Code: Select all
$paramas = array(
'getItemDetailInfoRequest' => array(
'requestHeader' => array(
'clientRequestId' => 1,
'projectNumber' => 64111,
'requestDateTime' => '2014-03-20T14:05:24.572Z'
),
'getItemDetailInfoList' => array(
'itemDetailSearchCriteria' => array(
'itemNumber' => 'F00573019120B',
'projectNumber' => 64111
)
)
)
);
The error says "no 'requestHeader' property" which is something you specify in your $params. So you need to go back to the spec for the webservice and see what the correct name is. My guess would be something like 'getItemDetailInfoRequestHeader'.
Re: Using PHP to call SOAP Web Service
Posted: Mon Mar 24, 2014 4:05 pm
by TravisT6983
I am certainly not the best at reading the request but this is what works in SOAP UI, obviously replacing UN/PW with the correct ones.
When i load the WSDL URL in SOAP UI, i see that there are 4 requests total the request that this is following is labeled GetItemDetailInfo not sure if this helps at all.
Code: Select all
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<env:Header>
<Security xmlns="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<UsernameToken>
<Username>Username</Username>
<Password>Password</Password>
</UsernameToken>
</Security>
</env:Header>
<env:Body>
<getItemDetailInfoRequest xmlns="">
<requestHeader>
<clientRequestId>1</clientRequestId>
<projectNumber>64111</projectNumber>
<requestDateTime>2014-03-16T11:05:24.572Z</requestDateTime>
</requestHeader>
<getItemDetailInfoList>
<itemDetailSearchCriteria>
<itemNumber>F00573019120B</itemNumber>
<projectNumber>64111</projectNumber>
</itemDetailSearchCriteria>
</getItemDetailInfoList>
</getItemDetailInfoRequest>
</env:Body>
</env:Envelope>
I really appreciate your continued support on this i am lost!
Thanks,
Travis
Re: Using PHP to call SOAP Web Service
Posted: Tue Mar 25, 2014 9:27 am
by Christopher
Try moving requestHeader into the SOAP Header block. That is where it should be.