PHP5 en SOAP

March 12th, 2007 by Tijs

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".

PHP:
  1. <?php
  2. // require the class that will handle the calls
  3. require_once 'classes/API.php';
  4.  
  5. // disable the soap.wsdl_cache
  6. ini_set("soap.wsdl_cache_enabled", "0");
  7.  
  8. // get RAW_POST_DATA
  9. if(!isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = file_get_contents('php://input');
  10.  
  11. // set options for the server
  12. $options = array('uri' => 'http://localhost/SOAP/');
  13.  
  14. // init server
  15. $server = new SoapServer(null, $options);
  16.  
  17. // set the class that will handle the SOAP-requests
  18. $server->setClass('API');
  19.  
  20. // handle SOAP-requests
  21. $server->handle();
  22. ?>

Dan de class die de SOAP-requests zal afhandelen.

PHP:
  1. <?php
  2. class API
  3. {
  4. /**
  5. * returns 'Hello World'
  6. *
  7. * @return string
  8. */
  9. public function sayHello()
  10. {
  11. return 'Hello World';
  12. }
  13. }
  14. ?>

Dan de "client".

PHP:
  1. <?php
  2. // set client options
  3. $options = array('location' => 'http://localhost/SOAP/server.php', 'uri' => 'http://localhost/SOAP/');
  4.  
  5. try {
  6. // init client
  7. $client = new SoapClient(null, $options);
  8. // make SOAP-request and print it
  9. echo '<pre>';
  10. echo "\n".$client->sayHello();
  11. echo '</pre>';
  12. }
  13. // catch SOAP-errors
  14. catch(SoapFault $e)
  15. {
  16. // print error
  17. print_r($e);
  18. }
  19. ?>

Opmerkingen zijn uiteraard welkom!

Technorati , , , ,

11 Responses to “PHP5 en SOAP”

  1. Patrick says:

    Ik heb de scriptjes aangemaakt, en inderdaad, het werkt als een tierelier.
    Leuke om te zien is dat de client daarna een HTTP POST van de data doet naar de server:
    192.168.1.105 – - [25/Oct/2007:13:06:36 +0200] “GET /development/SOAP/client.php HTTP/1.1″ 200 23 “http://testing.local/development/SOAP/” “Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.8) Gecko/20071008 Firefox/2.0.0.8″
    192.168.1.107 – - [25/Oct/2007:13:06:36 +0200] “POST /development/SOAP/server.php HTTP/1.1″ 200 544 “-” “PHP-SOAP/5.2.0-8+etch7″

  2. Marcel says:

    Hey ik had even een vraagje ik gebruik zelf
    XAMPP en heb nu binnen htdoc’s een mapje aan gemaakt genaamd SOAP daar heb ik server.php aan gemaakt client.php aan gemaakt en een map gemaakt classes en daar in heb ik API.php aangemaakt als ik nu localhost/SOAP doe zie ik alleen die 3 files.. iemand enige idee hoe dit zou moeten of kunnen werken?

    ik hoop van jullie te horen

    groetjes
    Marcel

  3. Robin says:

    Wat ben ik blij dit te vinden. Bedankt voor de big help!

    Nuttige informatie over SOAP en PHP is niet zo makkelijk te vinden.

    Bedankt

  4. JC says:

    fantastic code!

  5. vpn says:

    Welcome to Free-VPN-Unlim.com
    Even, if malefactors can intercept the information which you have transferred in a NETWORK, they cannot read it.

  6. shkafy.com says:

    How do people actually make money with porn?

  7. יבואן says:

    יבוא בקלות של מוצרים מסין? מוצרים תוצרת סין נעליים  ביגוד מכונות תעשייה הו בכלל לייצר בסין אנחנו נעזור לך בכל התהליך ייצוג בקנטון פייר -1888- קטלוגים ופרטים

  8. richmedia says:

    How much money do i need to survive in Budapest as a foreign student?

  9. Gabriella Schachsieck says:

    Hi , I have been using a excellent way to make lots and lots of money online blogging. I expect this is primaraly for the website administrator but there are probably lots more bloggers reading this. I’ve made thousands using the techniques detailed in the product and its only been 2 months. Auto Blog System X

  10. Dylan Madera says:

    Sick of obtaining low numbers of useless visitors to your website? Well i want to let you know about a fresh underground tactic that produces me personally $900 each day on 100% AUTOPILOT. I possibly could be here all day and going into detail but why dont you just check their site out? There is really a excellent video that explains everything. So if your seriously interested in making hassle-free hard cash this is the website for you. Auto Traffic Avalanche

  11. Terrance Uchytil says:

    Tired of getting low numbers of useless traffic to your site? Well i want to inform you of a brand new underground tactic that makes me personally $900 every day on 100% AUTOPILOT. I possibly could be here all day and going into detail but why dont you simply check their site out? There is really a excellent video that explains everything. So if your serious about producing easy cash this is the site for you. Auto Traffic Avalanche

Leave a Reply