Solved

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

Posted on 2011-09-29
3
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 110

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Magento: different theme for Pc and for mobile devices 4 76
Wordpress Query 5 45
Php logic to add to date card 9 39
Pull Variable from URL  Use php template 1 35
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

749 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