• Home
  • About
  • Portfolio
  • Hire me
  • Templates
blog.lopau.com

Just Another Dang Blog

A tech blog about web development, graphic designs, freelancing, cloud computing, mobile development, innovations and seo.
Welcome to lopau.com - a digital playground, online portfolio, web experiments and blog of Paulo Orquillo
« 3D Augmented Reality platform(QR+AR) for iPhone, iPad, Android, BB
Inkling by Wacom »
Sep 05
Create a Web Service using SOAP

This week one of the requirements for our client was to have a force.com application communicate with an external web service running on PHP to generate a barcode sequence. I’ll teach how I was able to setup the web service using NuSOAP and a document/literal as rcp/encoded is not supported by force.com. My goal was to make the task easier on php side so from force.com it would make a request by passing a string delimited by an asterisk and pipe. We process the string and return it back as url for the barcode image. I wont be covering barcode creation though in this tutorial but only the web service and sample client.


e.g. Name*2|Mr.*3

The web service will parse the string convert it to an array. You can ofcourse pass any other type of string with a delimiter.

Here is how the server is.

// Pull in the NuSOAP code
require_once(‘lib/nusoap.php’);
// Create the server instance
$server = new soap_server();
$server->configureWSDL( ‘servicename’, ‘urn:servicename’, ”, ‘document’);
myRegister($server,’DoSomething’,array(
‘in’ => array(
‘sf-parameters’ => ‘xsd:string’),
‘out’ => array(‘Pass’ => ‘xsd:string’)
));
//if in safe mode, raw post data not set:
if (!isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = implode(“\r\n”, file(‘php://input’));
$server->service( $HTTP_RAW_POST_DATA);
function myRegister( &$server, $methodname, $params) {
$server->register($methodname, $params["in"], $params["out"],
‘urn:servicename’, // namespace
$server->wsdl->endpoint .’#’. $methodname, // soapaction
‘document’, // style
‘literal’, // use
‘Generate Barcode Sequence URL’ // documentation
);
}
function DoSomething($params) {
$string = $params;
$list = explode(“|”, $string);
//make associative array
$result = array();
foreach($list as $key=>$values) {
$result[] = explode(“*”, $values);
}

//creates the string
$bcStr = “”;
foreach($result as $values) {
$bcStr .= $values[0];
for ($i = 1; $i < = $values[1]; $i++) {
$bcStr .= "\t";
}
}

return array('Pass'=> $bcStr);
}

You are instantiating a class of the NuSOAP server and configuring it with WSDL. You also create two functions,, one handles the $server->register method which takes the argument incoming request and the outgoing response and the $server->wsdl with arguments defining the server as a document/literal.

Then for my task purpose the incoming delimited string is converted to an associative array and then converted back to string with the proper formatting I require ($bcStr), if you have barcode class you can pass that string and a barcode image should be generated from it. That however is not covered on this tutorial.

Next is simple php client to test the web service out.

// Pull in the NuSOAP code
require_once('lib/nusoap.php');
// Create the client instance
$ns="urn:servicename";
$client = new nusoap_client('http://localhost/soap_server/hellowsdl2.php?wsdl', true);
if ( $client->getError() ) {
print “Soap Constructor Error:
".
	$client->getError()."

“;
}
$params=array(“sf-parameters”=>”Name*2|Mr.*3″);
$result = $client->call( “DoSomething”, array(“parameters”=>$params), $ns);
if ($client->fault) {
//soap_fault
print “

Soap Fault:

(". $client->fault->faultcode .")  ".
	$client->fault->faultstring. "

“;
}
elseif ( $client->getError() ) {
print “

Soap Error:

". $client->getError() ."

“;
}
else {
print “

Result:

". $result["Pass"] ."

“;
}
print ‘

Details:


‘.
‘

Request

' .
htmlspecialchars( $client->request, ENT_QUOTES) .'

‘.
‘

Response

' .
htmlspecialchars( $client->response, ENT_QUOTES) .'

‘.
‘

Debug

' .
htmlspecialchars( $client->debug_str, ENT_QUOTES) .'

‘;
?>

This time I instantiated a nusoap_client note that underscore _ as I got stuck on this earlier and got PHP complaining when it was just nusoapclient. Next I created the string params to be passed to the DoSomething method via $client->call.

Then from here we goback to our force.com app we make a soap request to the webservice and it returns with the response we need. On Force.com make sure you add your web service the Remote Site Settings under Setup > Administrative Setup > Security Controls.

Kudos for the resource and solution for task from the original author mleiv blog’s.


This entry was posted on Monday, September 5th, 2011 at 7:49 am and is filed under PHP, SalesForce, Web Resources. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

One Response to “Create a Web Service using SOAP”

  1. Jack Says:
    February 16th, 2012 at 2:42 am

    Hi, I have question about nusoap, did you manage to import existing wsdl into nusoap and map it with php function / class? I try to do it for a couple of days, wihtout success :/

Leave a Reply

  • Popular Posts

    • Fixing power problems with your LG Flatron Monitor (LG1753S)
    • Finally passed my Force.com Certified Developer Exam
    • DIY: HP Laptop Power Adapter Repair
    • How to Setup PHP, MySQL and phpMyAdmin on Mac OS X Snow Leopard
    • Setup EC2 and FileZilla with PPK file on Mac OS X
    • Safari Bug on Lion: The application Safari canceled restart

    Recent Comments

    • Swati: I am planning to write dev 401 next month.Could you please send me the materials and dumps that you have.My...
    • Apex: Great tut,Thanks
    • matt: Never been a fan of Steve Jobs or Bill Gates as they do seem to steal other peoples ideas then use them as...
    • Ash: I encountered the same issue, I went to activity monitor, checked on windowed processes and force killed Safari,...
    • lasersoul: Hi Guys Similar problem with a LG W2452T near enough to effect the fix in the same way. 4 bulging...
    • Ione Bentler: Actually there is another alternative. Get a refund. That’s what I did. I was so pissed off that...
    • Recent Posts

      • R.I.P Steve Jobs, Stay Hungry Stay Foolish
      • Inkling by Wacom
      • Create a Web Service using SOAP
      • 3D Augmented Reality platform(QR+AR) for iPhone, iPad, Android, BB
      • Create rewrite rules for friendly url for WordPress plugin custom queries
    • Blogroll

      • Abiel Online
      • Abiel's Tech Blog
      • Copongcopong’s Notes from Walk the walk
      • Get Rich Talks
      • Javi’s Go Blog
      • The Passionate Pilgrim’s Musings and Ramblings
      • Tinokla’s Scribbles and Doodles
    • Links

      • LS Blogs
      • Pampanga Province Websites
      • Top Blogs Philippines
      • Travel and Gobble
    • Proudly Pinoy!
    • Add to Technorati Favorites
    • Web Development & Design - Top Blogs Philippines

    • Sponsored Ads

      Summer Savings! $7.49 .com
      Ensogo
    • Categories

      • Adsense
      • Airsoft
      • Android
      • apple
      • Blogs
      • Browsers
      • Cloud Computing
      • CSS
      • Desktop Publishing
      • Facebook
      • Firefox
      • Flex
      • Fonts
      • Free WP Templates
      • Freelance
      • Google
      • Hardware
      • HTML 5
      • iOS
      • Ipad
      • iPhone
      • IT
      • Javascript
      • microsoft
      • Mobile Development
      • Online Scams
      • Others
      • Personal
      • Personal Finance
      • Photography
      • Photoshop
      • PHP
      • Political
      • Projects
      • SalesForce
      • Security
      • SEO
      • Social Marketing
      • Travelling
      • Troubleshooting
      • Tutorials
      • WAMP
      • Web Resources
      • Wordpress
    • Tags

      Adsense among ed andriod apache apple blogging Blogs Cloud Computing CSS cuil development earn online enable gd library Firefox Fonts force.com Freelance godaddy Google google analytics government increase traffic internet fraud internet scams iPhone knol knowledge content Money mysql online purchase page rank pagerank PHP politics pr search engines SEO social media content social networks text browser themes web traffic western union Wordpress zend certification

    Just Another Dang Blog is proudly powered by WordPress
    Entries (RSS) and Comments (RSS).
    Valid XHTML 1.0 Transitional Valid CSS!