Showing posts with label Client. Show all posts
Showing posts with label Client. Show all posts

Introduction to reliable messaging part 3

Monday, November 10, 2008

In this blog post, we will look at how you can send multiple application messages within a single sequence reliably using

WSClient API.  For this purpose WSClient has an option named willContinueSequence. If you intend to send only a since application message within your reliable sequence, then you do not need to touch this option. However, when sending multiple application messages, you need to use it as follows.

1. When sending the first message, set willContinueSequence=TRUE in WSClient.

2. Send your application messages by using WSMessage object.

3. When you want to send the final application message to be sent within the reliable sequence, set the option lastMessage to true.

Here is a code example demonstrating this.

Step 1. First create an application message.

$requestPayloadString = <<<XML
    <ns1:pingString xmlns:ns1="http://wso2.org/wsfphp/samples/reliable">
        <text>Hello World!</text>
    </ns1:pingString>
XML;

Step 2. Create a WSClient object with willContinueSequence option set to TRUE

$client = new WSClient(array( "reliable" => TRUE, "useWSA" => TRUE, "willContinueSequence" => TRUE));

Step 3. Create and Send WSMessage objects containing application messages.

$message = new WSMessage($requestPayloadString,
           array( "to" => "http://localhost/samples/reliable/ping_service_rm.php",
                  "action" => http://wso2.org/wsfphp/samples/pingString));

$client->send($message);

$message1 = new WSMessage($requestPayloadString,
            array( "to" => "http://localhost/samples/reliable/ping_service_rm.php",
                   "action" => "http://wso2.org/wsfphp/samples/pingString"));

    $client->send($message1);

Step 4. When you want to send the last application message, set the option lastMessage to true in WSMessage object.

$message2 = new WSMessage($requestPayloadString,
    array( "to" => "http://localhost/samples/reliable/ping_service_rm.php",
           "action" => "http://wso2.org/wsfphp/samples/pingString",
           "lastMessage" => TRUE));

$client->send($message2);

Note that this is a ping service and hence the use of the method send in WSClient. You can similarly use request method as well.

ScreenCast - How to Consume a Web Service Using WSF/PHP

Wednesday, October 29, 2008

You can watch a Screencast on How to Consume a Web Service Using WSF/PHP from WSO2 Oxygent Tank (wso2.org) developer portal

Payload the Way You Want with PHP5 built-in SOAP

Wednesday, January 30, 2008

Say you want this payload:

        <ns1:base>
            <itemsLine xsi:type="ns2:urn:test">
               <name xsi:type="xsd:string">A</name>
               <type xsi:type="xsd:string">D</type>
               <from xsi:type="xsd:string">C</from>
               <fluke xsi:type="xsd:string">D</fluke>
            </itemsLine>
            <itemsLine xsi:type="ns2:urn:test">
               <name xsi:type="xsd:string">E</name>
               <type xsi:type="xsd:string">H</type>
               <from xsi:type="xsd:string">G</from>
               <fluke xsi:type="xsd:string">H</fluke>
            </itemsLine>
         </ns1:base>

 

How do you get this XML to be output by the SoapClient?

You can get this exact payload with the following code:

<?php
class itemsLine{
    function itemsLine($a, $b, $c, $d)
    {
        $this->name = $a;
        $this->type = $d;
        $this->from = $c;
        $this->fluke = $d;
    }
}

$client = new SoapClient(null, array('location' => "http://localhost:9090/soap.php",
                                     'uri'      => "http://test-uri/"));
$item1 = new itemsLine('A', 'B', 'C', 'D');
$item2 = new itemsLine('E', 'F', 'G', 'H');

$var1 = new SoapVar($item1 , SOAP_ENC_OBJECT, "urn:test", "http://soapinterop.org/xsd");
$var2 = new SoapVar($item2 , SOAP_ENC_OBJECT, "urn:test", "http://soapinterop.org/xsd");

$client->base(new SoapParam($var1, "itemsLine"), new SoapParam($var2, "itemsLine"));
?>

Hello World Clients with PHP

Tuesday, January 29, 2008

Yesterday I gave you the sources for the services. Here are the Web service clients.

With SOAP Extension:
<?php   
$client = new SoapClient(null, array('location' => "http://localhost:9090/soap_ext/hw_service.php",
'uri' => "http://test-uri/"));
$result =  $client->__soapCall('hello', array('name' => 'Sam'));
printf($result);
?>

With NuSOAP:
<?php
require_once('../lib/nusoap.php');
$client = new soapclient('http://localhost:9090/nusoap-0.7.3/samples/hw_service.php');
$result = $client->call('hello', array('name' => 'Sam'));
printf($result);
?>

With WSF/PHP:
<?php   
$reqestPayloadString = <<<XML
<hello>
<name>Sam</name>
</hello>

XML;
$client = new WSClient(array("to" => "http://localhost:9090/tutorial/hello/hw_service.php"));   
$response = $client->request($reqestPayloadString);   
$simplexml = new SimpleXMLElement($response->str);
$result = $simplexml->return[0];   
printf($result);   
?>  

The simplest PHP Web Service Client

Wednesday, January 2, 2008

Yesterday I showed how to write a very simple Web service, form the article that I am currently writing.

Here is the simplest client, extracted form the same ongoing article.

  1. <?php

  2. $reqestPayloadString = <<<XML
  3. <knock>Knock, knock!!!</knock>
  4. XML;
  5.  
  6. $client = new WSClient(array("to" => "http://localhost/tutorial/first/service.php"));


  7. $response = $client->request($reqestPayloadString);

  8. echo "Service replied asking: '".$response->str."'\n";

  9. ?>

As in the case of the service, the client code is also very simple. The steps used in the client when consuming the service could be summarized as follows.

  1. Create the client instance with the service endpoint address (line 7)
  2. Send the request with the request message (line 9)
  3. Process the received response (line 11)

In this Web service client, when the WSClient instance is created, on line 7, we provide the endpoint address of the service we want to consume as the to option. The address used in this sample is the address of the service that we deployed in the previous section.

Form lines 3 to 5, we define the XML string to be sent as the request.

On line 9, the call to request() method of the WSClient instance, with the request XML string as a parameter, triggers the service invocation. Upon the call to request() method, Web service client forms the request message with the given XML string, sends the request to the service given in the to address and receives the response and stores the response in the WSMessage instance named response. You can access the incoming response as an XML string using the str member variable of the WSMessage instance.

Encrypting SOAP messages using PHP

In this blog I've described how to write a PHP script that uses WSO2 WSF/PHP to do secured web services...