• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

Passing an xml from a php machine to a remote webservice using soap.

I have to send an XML document from a Linux server (apache, PHP) to a remote system using SOAP as a webservice.  I need assistance or guidance on how to communicate with that server and how to include the data if anyone could help.  I've been researching it on the web but I find a lot of....
The Linux server is CentOs 5 with Apache (2.2.3), php (5.3.6), SOAP installed.  Remote server has SOAP webservice installed.

What I'm doing is pulling data from a shipment once a user has finished adding parts, creating the XML document then I need to pass it to the transportation server.

Thanks!
0
Gabriel7
Asked:
Gabriel7
  • 2
1 Solution
 
Ray PaseurCommented:
As a general rule, the owner of the remote server (who has determined that the communication must use a SOAP protocol) must tell you how to present the sequence of calls to the remote server.  Usually they have a user's guide with examples, and anything we would tell you here would just be guessing.

However there may be a better answer.  When you ask the owner of the remote server for the user's guide, ask them if they have a RESTful interface.  In REST applications, each request is atomic and complete and each response is complete and usually instantaneous.  As an example, the entire WWW is a RESTful service.  The URL makes the client request and the web site makes the server response.  A classic example of a REST interface is the Yahoo Geocoder (you can Google it).  Some additional explanation is available at these two links:
http://en.wikipedia.org/wiki/Representational_state_transfer
http://www.ibm.com/developerworks/webservices/library/ws-restful/

Another example of a simple REST web service is shown in the code snippet.  Input first name, output family name.  It is intentionally over-simplified to illustrate the essential moving parts.  This uses the GET method, transmitting the API key and input arguments in the URL.   If larger inputs are required or the inputs must modify the data model, the POST method makes sense.  It is available for you to test here:
http://www.laprbass.com/RAY_REST_get_last_name.php?key=ABC&name=Ray

Some of my colleagues say that SOAP is the devil.  I don't go quite that far, but I know that the web is littered with the rotting husks of failed SOAP projects.  In contrast, I have never seen a RESTful interface that you could not understand and use immediately.  So if you can convince them to expose a RESTful interface you will be way ahead in your project.

Best of luck with it, ~Ray
<?php // RAY_REST_get_last_name.php
error_reporting(E_ALL);



// DEMONSTRATE HOW A RESTFUL WEB SERVICE WORKS
// INPUT FIRST NAME, OUTPUT FAMILY NAME
// CALLING EXAMPLE:
// file_get_contents('http://laprbass.com/RAY_REST_get_last_name.php?key=ABC&resp=XML&name=Ray');



// OUR DATA MODEL CONTAINS ALL THE ANSWERS - THIS COULD BE A DATA BASE - AS SIMPLE OR COMPLEX AS NEEDED
$dataModel
= array
( 'Brian'   => 'Portlock'
, 'Ray'     => 'Paseur'
, 'Richard' => 'Quadling'
, 'Dave'    => 'Baldwin'
)
;


// RESPONSE CAN BE PLAIN TEXT OR XML FORMAT
$alpha = NULL;
$omega = NULL;
if ( (isset($_GET["resp"])) && ($_GET["resp"] == 'XML') )
{
    // PREPARE THE XML WRAPPER
    $alpha = '<response>';
    $omega = '</response>';
}



// TEST THE 'API KEY' - THIS COULD BE A DATA BASE VALIDATION LOOKUP - AS SIMPLE OR COMPLEX AS NEEDED
$key = (!empty($_GET["key"])) ? $_GET["key"] : FALSE;
if ($key !== 'ABC')
{
    echo $alpha . 'BOGUS API KEY' . $omega;
    die();
}



// LOOK UP THE FAMILY NAME
$name = (!empty($_GET["name"])) ? $_GET["name"] : 'UNKNOWN';

// IF THE NAME FROM THE URL IS FOUND IN THE DATA MODEL
if (array_key_exists($name, $dataModel))
{
    // RETURNS THE APPROPRIATE FAMILY NAME FROM THE DATA MODEL
    echo $alpha . $dataModel[$name] . $omega;
    die();
}

// RETURNS THE UNKNOWN NAME INDICATOR
else
{
    echo $alpha . 'UNKNOWN' . $omega;
    die();
}

Open in new window

0
 
Rose BabuSenior Team ManagerCommented:
Hi,

in PHP, there are NuSOAP and SOAP Client modules available to access a web service. using those you can send request to your SOAP server and receive the response.

Sample PHP code to consume a web service using NuSOAP

download NuSOAP package from http://sourceforge.net/projects/nusoap/ and extract it
<?php
// Pull in the NuSOAP code
require_once ('nusoap.php');
// Create the client instance
$client = new soapclient ( 'http://localhost/service.php?wsdl', true );
// Check for an error
$err = $client->getError ();
if ($err) {
	// Display the error
	echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}
// Call the SOAP method
$result = $client->call ( 'hello', array ('name' => 'John' ) );
// Check for a fault
if ($client->fault) {
	echo '<h2>Fault</h2><pre>';
	print_r ( $result );
	echo '</pre>';
} else {
	// Check for errors
	$err = $client->getError ();
	if ($err) {
		// Display the error
		echo '<h2>Error</h2><pre>' . $err . '</pre>';
	} else {
		// Display the result
		echo '<h2>Result</h2><pre>';
		print_r ( $result );
		echo '</pre>';
	}
}
// Display the request and response
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars ( $client->request, ENT_QUOTES ) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars ( $client->response, ENT_QUOTES ) . '</pre>';
// Display the debug messages
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars ( $client->debug_str, ENT_QUOTES ) . '</pre>';
?>

Open in new window

Using PHP SOAPClient
<?php
ini_set('error_reporting', E_ALL);

$client = new SoapClient('http://www.domain.com/Service.php?wsdl'); // Update your web service URL

echo "<u>Response</u> <br /><br />";
$arrParameters = array('inputParameter' => 'input data goes here');
$response = $client->YourWebMethod($arrParameters);	// Update YourWebMethod with the actual WebMethod name

print_r($response);

?>

Open in new window


You may try anyone of the above to consume and get response.

can u post the SOAP webservice url or request/response structure? so that i can give you some sample code to access the web service.
0
 
Rose BabuSenior Team ManagerCommented:
Have you tried this? any luck?
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now