Skip to main content

Learn SOAP

SOAP Introduction

- SOAP stands for Simple Object Access Protocol.
- SOAP is a protocol for accessing web services.
- SOAP is based on XML.


==========================================================================
 
WSDL
 
WSDL stands for Web Services Description Language
WSDL is an XML-based language for describing Web services.
WSDL is a W3C recommendation

  ==========================================================================

What is SOAP?

- SOAP stands for Simple Object Access Protocol
- SOAP is a communication protocol
- SOAP is for communication between applications
- SOAP is a format for sending messages
- SOAP communicates via Internet
- SOAP is platform independent
- SOAP is language independent
- SOAP is based on XML
- SOAP is simple and extensible
- SOAP allows you to get around firewalls
- SOAP is a W3C recommendation

==========================================================================

Why SOAP?

It is important for application development to allow Internet communication between programs.

SOAP provides a way to communicate between applications running on different operating systems, with different technologies and programming languages.

Today's applications communicate using Remote Procedure Calls (RPC) between objects like DCOM and CORBA, but HTTP was not designed for this. RPC represents a compatibility and security problem; firewalls and proxy servers will normally block this kind of traffic.

A better way to communicate between applications is over HTTP, because HTTP is supported by all Internet browsers and servers. SOAP was created to accomplish this.

==========================================================================

SOAP Building Blocks

A SOAP message is an ordinary XML document containing the following elements:

An Envelope element that identifies the XML document as a SOAP message
A Header element that contains header information
A Body element that contains call and response information
A Fault element containing errors and status information

==========================================================================

Syntax Rules

Here are some important syntax rules:

A SOAP message MUST be encoded using XML
A SOAP message MUST use the SOAP Envelope namespace
A SOAP message MUST use the SOAP Encoding namespace
A SOAP message must NOT contain a DTD reference
A SOAP message must NOT contain XML Processing Instructions

==========================================================================

Skeleton SOAP Message

<?xml version="1.0"?>
<soap:Envelope
xmlns:soap="http://www.w3.org/2001/12/soap-envelope"
soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:Header>
...
</soap:Header>

<soap:Body>
...
  <soap:Fault>
  ...
  </soap:Fault>
</soap:Body>

</soap:Envelope>
==========================================================================

Example : (Need to fetch the top goal scores of the tournament from footballpool)

This post will show a simple example of how to interpretate a WSDL file and a very simple, yet quick example of how to extract information from this file through PHP.
First of all please  enable SOAP in your PHP configuration.
Find the methods and its required parameters webservice url using WSDL.



//$url = 'http://footballpool.dataaccess.eu/data/info.wso?wsdl';


/*


// for now lets concentrate on part of xml element from which we can fetch the information

<xs:element name="TopGoalScorers">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="iTopN" type="xs:int" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

*/

//we now finally know that the method TopGoalScorers which we saw in the first WSDL fragment expects one parameter as input. This parameter is called iTopN and is of the type int.

//let fetch the information using PHP using SoapClient
$url = 'http://footballpool.dataaccess.eu/data/info.wso?wsdl';
$client = new SoapClient($url);
$result = $client->TopGoalScorers(array('iTopN'=>5));

//print the result in table
$array = $result->TopGoalScorersResult->tTopGoalScorer;

  print "
    <table border='2'>
      <tr>
        <th>Rank</th>
        <th>Name</th>
        <th>Goals</th>
      </tr>
  ";
 
  foreach($array as $k=>$v){
    print "
      <tr>
        <td align='right'>" . ($k+1) . "</td>
          <td>" . $v->sName . "</td>
          <td align='right'>" . $v->iGoals . "</td>
        </tr>";
  }
 
  print "</table>";

==========================================================================

Example: (Example for cyberlogic.gr SOAP webservice)


$soapUrl   = "http://wl.filos.com.gr/services/WebService.asmx";
$namespace = "http://www.cyberlogic.gr/webservices/";
$action    = "PlaceSearch";

// create the request XML
$request            = new SimpleXMLElement('<PlaceSearchRequest/>');
$request->Username  = 'SERUNCD';
$request->Password  = 'TA78UNC';
$request->PlaceType = 'Cities';
$request->Language  = 'en';

// create the soap variable
$var = new DOMDocument();
$var->appendChild($var->createElementNS($namespace, 'xml'));
$var->documentElement->nodeValue = $request->asXML();
$soapVar = new SoapVar($var->saveXML($var->documentElement), XSD_ANYXML);

// create soap client in non-WSDL mode
$client = new SoapClient(null, array(
    'location' => $soapUrl,
    'uri'      => $namespace
));

try {
    // place soap call
    $result = $client->__soapCall(
        $action, array($soapVar), array('soapaction' => "$namespace$action")
    );

    // process result
    foreach ($result->PlaceSearch->Response->Cities->City as $i => $City) {
        printf("#%03d: %s\n", $i, $City->CityName);
    }
} catch (Exception $e) {
    echo $e->getMessage(), "\n";
    echo $client->__getLastRequest(); // set 'trace' => true SoapClient option
}


==========================================================================

Example: Using CURL

$soap_request  = 'xml='.urlencode(trim('
<?xml version="1.0" encoding="utf-8"?>
<PlaceSearchRequest>
    <Username>SERUNCD</Username>
    <Password>TA78UNC</Password>
    <PlaceType>Cities</PlaceType>
    <Language>en</Language>
</PlaceSearchRequest>
'));

$header = array(
    'POST /services/WebService.asmx HTTP/1.1',
    'Host: wl.filos.com.gr',
    'Content-Type: application/x-www-form-urlencoded',
    'Content-Length: '.strlen($soap_request),
);

$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, 'http://wl.filos.com.gr/services/WebService.asmx/PlaceSearch');
curl_setopt($soap_do, CURLOPT_HEADER, false);
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 100);
curl_setopt($soap_do, CURLOPT_TIMEOUT,        100);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_HTTPHEADER,     $header);
curl_setopt($soap_do, CURLOPT_POST,           true);
curl_setopt($soap_do, CURLOPT_POSTFIELDS,     $soap_request);
curl_setopt($soap_do, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');


$output = curl_exec($soap_do);
$info = curl_getinfo($soap_do);

if (curl_exec($soap_do) === FALSE)
{
    $err = 'Curl error: ' . curl_error($soap_do);
    curl_close($soap_do);
    print $err;
}
else
{
    curl_close($soap_do);
}

echo 'The server responded: '.$info['http_code']."\n";
echo 'Output: '.$output;
echo "\n";

==========================================================================

Soap useful more examples
 
https://help.exacttarget.com/en/technical_library/web_service_guide/getting_started_developers_and_the_exacttarget_api/connecting_to_the_api_using_php/
https://developer.decibel.net/sample-code-soap-php

Comments

Popular posts from this blog

Learn phpfox

PHPFox  is a social network script, it is an internet application and when you install it, it is a website. The  phpfox  script comes in 3 packages, each with a different set of modules. it has two products: 1. Nebula (upto phpfox 3.8) 2. Neutron (Newer) You can check the demo on :  http://www.phpfox.com =================================================== To clear cache in phpfox follow the following steps, admincp >> Tools >> Maintenance >> Cache Manager >> Click on Clear All button =================================================== To work facebook app on local Following settings need to done in facebook app   1) go => setting => Advance 2) see "OAuth Settings" area and set "Valid OAuth redirect URIs" =  http:// projectdomain /index.php?do=/user/login/, http:// projectdomain .com/index.php?do=/user/register/, http:// projectdomain .com, http:// projectdomain .com/index.php 3) en...

How to Make Your Own PHP Captcha Generator

In this article we will create file based simple yet successful captcha generator. 3 Major Anti-spamming techniques used? Mathematical Operation like Random number + Random Number = -> The user must specify the answer Random word -> User must type the word Random question -> Obvious one which the user should answer correctly [ex: Are you human?] How Captcha works? The captcha generator generates an IMAGE with the question and then put up a session variable storing the value. User input though an input box. Using php POST, we compare the session variable data with the user input and tell whether its a bot or human. Its coding time The Code First let's write the php script which generates the captcha image. We use the simple header-content change technique, from which we can easily bring up an image from a given text. captcha.php PHP Code: array("Num" => "Num"), 1 => array("Are y...

Joomla Flash Tutorial

Joomla Flash Tutorial Adding a custom-made Flash animation created with Anim-FX to Frontpage is a snap By following these simple steps, you will be up and running in no time at all and impressing all of your visitors with your talents! First create your Flash animations in Anim-FX, and make sure the *.swf and *.txt files are in the base html directory.  Do not make the mistake of uploading them into the images or images/flash directory.  Also the flash does not work if loaded into the template directory.  Both files must be in the html directory where the index.php file is located. Then you can either copy the HTML generated by Anim-FX into the index.php file to the appropriate location or use a Joomla Flash Module.  Edit the index.php file which drives your Joomla template. Method 1: Editing the index.php for your Joomla template: Copy the follow...