Using PHP to call SOAP Web Service

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TravisT6983
Forum Newbie
Posts: 19
Joined: Wed Jul 02, 2008 2:15 pm

Using PHP to call SOAP Web Service

Post 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!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Using PHP to call SOAP Web Service

Post 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.
(#10850)
cod3x
Forum Newbie
Posts: 2
Joined: Sat Mar 22, 2014 8:50 am

Re: Using PHP to call SOAP Web Service

Post 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 :( ....
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Using PHP to call SOAP Web Service

Post 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();
}
(#10850)
cod3x
Forum Newbie
Posts: 2
Joined: Sat Mar 22, 2014 8:50 am

Re: Using PHP to call SOAP Web Service

Post by cod3x »

Thank you for your help. How about server .. how can i check header infos received from client ?

Thx again
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Using PHP to call SOAP Web Service

Post by Christopher »

Have you looked at the documentation at php.net? There are lots of examples:

http://us1.php.net/manual/en/book.soap.php

http://us1.php.net/manual/en/soapclient ... eaders.php
(#10850)
TravisT6983
Forum Newbie
Posts: 19
Joined: Wed Jul 02, 2008 2:15 pm

Re: Using PHP to call SOAP Web Service

Post by TravisT6983 »

Christopher wrote:Have you looked at the documentation at php.net? There are lots of examples:

http://us1.php.net/manual/en/book.soap.php

http://us1.php.net/manual/en/soapclient ... eaders.php
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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Using PHP to call SOAP Web Service

Post 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'.
(#10850)
TravisT6983
Forum Newbie
Posts: 19
Joined: Wed Jul 02, 2008 2:15 pm

Re: Using PHP to call SOAP Web Service

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Using PHP to call SOAP Web Service

Post by Christopher »

Try moving requestHeader into the SOAP Header block. That is where it should be.
(#10850)
Post Reply