Solved

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

Posted on 2011-09-29
3
247 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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now