Solved

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

Posted on 2011-09-29
3
248 Views
Last Modified: 2012-06-27
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
Comment
Question by:Gabriel7
  • 2
3 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36890753
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
 
LVL 16

Accepted Solution

by:
Rose Babu earned 500 total points
ID: 37629163
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
 
LVL 16

Expert Comment

by:Rose Babu
ID: 37756704
Have you tried this? any luck?
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question