Wednesday, January 12, 2011

Setting up a web service CLIENT script in native PHP

For this post, we will be using a native PHP to create a web service client. please follow the steps below and you we're able to connect and transact in a web service.

1. first thing you have to do is to get the end-point URL of the web service server. let say the end-point wsdl - http://mydomain.com/wservice.wsdl use it to initialize SoapClient with parameters: trace and exceptions.

$this->client = new SoapClient('http://mydomain.com/wservice.wsdl', array('trace' => 1, 'exceptions' => 1));

2. once you have a client object, call the method the same as calling a usual object. let say the method name is helloWorld then passing the parameter 'world', your code should be like this.

$this->client->helloWorld('world');

3. please see the simple code below using class.

<?php
class wservice_client
{
    private $endpoint = "http://mydomain.com/wservice.wsdl";   
    private $client;

    function wservice_client() {
            $param = array (
                            'trace' => 1,
                   'exceptions' => 1
            );

            $this->client = new SoapClient($this->endpoint, $param);
    }

    function helloWorld ( $world ) {
        try {
                $response = $this->client->helloWorld($world);
        } catch (Exception $e) {
                return $response;
        }

        return (array)$response;
    }
}

?>

No comments:

Post a Comment