Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / iPhone

Another way of Consuming WCF from PHP, iPhone, JavaMobile, JSON clients

5.00/5 (1 vote)
27 Sep 2010CPOL1 min read 21.8K  
There is a simple way to consume WCF from clients which do not support Microsoft's SOAP implementation. It is achieved by using REST to accept data and convert REST calls to SOAP calls with the PHP Framework.
Let me introduce the tool I use to connect to WCF web services from ANY mobile clients. You can access WCF from iPhone, Java ME devices, Android, PHP front-ends and whatever you like.

http://emission-framework.com/code/Framework.Integration.SOAPWebService/SOAPWebServiceReference.html[^]

Mobile clients do not support what Microsoft did to implement SOAP in WCF. iPhone even does not support SOAP well at all. Java Mobile supports Sun's SOAP but does not like Microsoft's SOAP.

But there is an easy solution to this:

1. We implement a SOAP client in PHP which is easy to use from PHP code;
2. We implement a REST interface to this SOAP client to make it accessible from any client supporting HTTP;
3. We implement additional business logic if appropriate;
4. We use PHP through REST interface and it translates everything to our SOAP server.

The following code calls the SOAP web service from PHP (again part of Emission Framework):

PHP
<?php
/**
 * PHP Framework Integration SOAP Web Service Reference class
 *
 * @author Andrew <andrew@vitche.com>
 * @author Frozen Rain <frozenrain@mail.ru>
 **/
class SOAPWebServiceReference extends WebServiceReference {
    private $_proxyHost = null;
    private $_proxyPort = null;
    public function __construct($uri) {
        parent::__construct($uri);
    }
    public function execute($method, $arguments) {
        $this->_proxyHost = Configuration::get("Framework.Integration.SOAPWebService.SOAPWebServiceReference._proxyHost");
        $this->_proxyPort = Configuration::get("Framework.Integration.SOAPWebService.SOAPWebServiceReference._proxyPort");
        $settings = array();
        if (isset($this->_proxyHost)) {
            $settings['proxy_host'] = $this->_proxyHost;
        }
        if (isset($this->_proxyPort)) {
            $settings['proxy_port'] = $this->_proxyPort;
        }
        $soapClient = new SoapClient($this->_uri, $settings);
        $timeStart = microtime(TRUE);
        $response = $soapClient->$method($arguments);
        $timeInterval = microtime(TRUE) - $timeStart;
        Trace::writeLine("SOAPWebServiceReference::execute($method, ...) took $timeInterval");
        $method .= "Result";
        return SOAPSerializer::fromResult($response->$method);
    }
}
?>


There is an additional task to understand what SoapClient (built-in PHP class) returns to us. Therefore we have an additional logic which parses it (see our site).

Very easy! And it is even easier with Vitche Emission PHP Framework. Because it already has all code bundled.

http://emission-framework.com/index.html[^]

The following lines of code do the whole job of converting REST to SOAP:

PHP
<?php
/**
 * PHP Framework Integration HTTP-to-SOAP Gateway Web Service class
 * This service makes SOAP services available to callers through the usual
 * HTTP REST interface
 **/
/**
 * Description of HTTPSOAPGatewayWebService
 *
 * @author Andrew <andrew@vitche.com>
 * @author Frozen Rain <frozenrain@mail.ru>
 **/
class HTTPSOAPGatewayWebService extends HTTPWebService {
    function __construct() {
        parent::__construct('');
    }
    function onMethodCall($strClassName, $strMethodName, $arRequest) {
        // Parse request
        $url = null;
        $arguments = array();
        foreach ($arRequest as $key => $value) {
            if ('url' == $key) {
                $url = $value;
            } else {
                $arguments[$key] = $value;
            }
        }
        $reference = new SOAPWebServiceReference($url);
        return JSONSerializer::toString($reference->execute($strMethodName, $arguments));
    }
}
?>


And now a bit more about accessing JSON services from iPhone. I had no wish to write anything in my own.

But I liked the following very much and now use it: http://github.com/stig/json-framework[^]

Also note that it has some parsing errors because it does not support "undefined" in JSON. And we are using "undefined" to serialize "null" values in Vitche PHP Framework.

Write me and I will send a fixed version of JSON framework. Or find that yourself.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)