Ik ben de laatste dagen aan het uitzoeken hoe ik een webservice kan maken. Na veel lezen, opzoeken, vragen stellen hebben de mannen van Openminds mij op het juiste pad gebracht (waarvoor dank), namelijk SOAP.
Omdat ik heel wat problemen ben tegengekomen zet ik hieronder even de code om anderen te helpen. Let wel, ik ben ook nieuw met SOAP. Dus sla mij niet dood als het niet direct werkt.
Eerst heb ik de "server geschreven".
PLAIN TEXT PHP:- <?php
- // require the class that will handle the calls
- require_once 'classes/API.php';
- // disable the soap.wsdl_cache
- ini_set("soap.wsdl_cache_enabled", "0");
- // get RAW_POST_DATA
- if(!isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = file_get_contents('php://input');
- // set options for the server
- $options = array('uri' => 'http://localhost/SOAP/');
- // init server
- $server = new SoapServer(null, $options);
- // set the class that will handle the SOAP-requests
- $server->setClass('API');
- // handle SOAP-requests
- $server->handle();
- ?>
Dan de class die de SOAP-requests zal afhandelen.
PLAIN TEXT PHP:- <?php
- class API
- {
- /**
- * returns 'Hello World'
- *
- * @return string
- */
- public function sayHello()
- {
- return 'Hello World';
- }
- }
- ?>
Dan de "client".
PLAIN TEXT PHP:- <?php
- // set client options
- $options = array('location' => 'http://localhost/SOAP/server.php', 'uri' => 'http://localhost/SOAP/');
- try {
- // init client
- $client = new SoapClient(null, $options);
- // make SOAP-request and print it
- echo '<pre>';
- echo "\n".$client->sayHello();
- echo '</pre>';
- }
- // catch SOAP-errors
- catch(SoapFault $e)
- {
- // print error
- print_r($e);
- }
- ?>
Opmerkingen zijn uiteraard welkom!
basic, cross post, PHP5, SOAP, webservice