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
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
class HTTPSOAPGatewayWebService extends HTTPWebService {
function __construct() {
parent::__construct('');
}
function onMethodCall($strClassName, $strMethodName, $arRequest) {
$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.