December 17, 2023 5:01 am

Create a web service with PHP

Web Service are typical Application Programming Interface (API) or Web APIs can accessed via Hypertext Transfer Protocol (HTTP) with an XML serialization in conjunction with other Web-related standards.

[wpdm_file id=3]

This Article show you how to create a simple Web Service.

Soap Server

SimpleServer.php

<?php
// Simple Method get 1 parameter and return with Hello
function AddHello($name)
{
     return "Hello $name";
}
// Create SoapServer object using WSDL file.
// For the simplicity, our SoapServer is set to operate in non-WSDL mode. So we do not need a WSDL file
$server = new SoapServer(null, array('uri'=>'http://localhost/hello'));
// Add AddHello() function to the SoapServer using addFunction().
$server->addFunction("AddHello");
// To process the request, call handle() method of SoapServer.
$server->handle();
?>

Soap Client allows you to communicate with server

SimpleClient.php

<?php
include "nusoap.php"; //Soap Library.
try {
// Create a soap client using SoapClient class
// Set the first parameter as null, because we are operating in non-WSDL mode.
// Pass array containing url and uri of the soap server as second parameter.
$client = new soapclient(null, array(
'location' => "http://localhost/hello/HelloServer.php",
'uri' => "http://localhost/hello"));
// Read request parameter
$param = $_POST['name'];
// Invoke AddHello() method of the soap server (HelloServer)
$result = $client->AddHello($param);
echo $result; // Process the the result
}
catch(SoapFault $ex) {
$ex->getMessage();
}
?>

Soap View to interact for end-user

SimpleView.php

<?php
echo "<h2>Welcome to PHP Web Service</h2>";
echo "<form action='SimpleClient.php' method='POST'/>";
echo "<input name='name' /><br/>";
echo "<input type='Submit' name='submit' value='Send'/>";
echo "</form>";
?>

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:

28 responses to “Create a web service with PHP”

  1. Margono says:

    i try to use code above,
    but i have error as “looks like we got no XML document”
    how to solve it ?
    thank you

  2. thiyagu says:

    i tried this but am not getting any result

    • huzoorbux says:

      any error? please post here.

      • trocatout says:

        I tried this too but i’ve any result too… Could you explain much more how to configure the different apache server ?
        By reading the code i ‘dont unerstand when the soap server is running ?

        I see that the location of the simple client invoke “HelloServer.php” : it’s a mistake ? the web service file name is “SimpleServer.php”. I’m right ?

        Thank you for yours answers.

  3. elecguy says:

    I tried this also as I was looking for a very simple example implementation to try to understand some errors I was getting with a more complex implementation. What a surprise when it came up with the same error that my complex implementation was having.

    FWIW: The SimpleClient throws a SoapFault error that is announced as “Client: “DTD are not supported by SOAP”. I understand that this is the error when the SoapClient gets returned a regular HTML file, not a Soap message.
    I am testing it with localhost, just like the example provided.
    Any ideas on this?

    FWIW: The correction suggested by trocatout is correct.

    • trocatout says:

      I tried again and the client has no results and stop after excedeed time. I tried with and without nusoap lib :

      “Fatal error: Maximum execution time of 30 seconds exceeded in C:wampwwwhelloSimpleClient.php on line 19”

      Could you explain me ?

      Thank you !

    • huzoorbux says:

      Download the code and change the uri parameter in SimpleClient.php file.

      $client = new SoapClient(null, array(

      'location' => "http://Your_Domain/Web_Service/SimpleServer.php",

      'uri' => "http://Your_Domain/Web_Service/"));

  4. hira ba says:

    hira ba

    I tried this also as I was looking for a very simple example implementation to try to understand some errors I was getting with a more complex implementation. What a surprise when it came up with the same error that my complex implementation was having.

  5. hira ba says:

    nhgjghjghjg

  6. hira ba says:

    FWIW: The SimpleClient throws a SoapFault error that is announced as “Client: “DTD are not supported by SOAP”. I understand that this is the error when the SoapClient gets returned a regular HTML file, not a Soap message.

  7. hira ba says:

    Could you explain me ?

    Thank you !

  8. uff… thank you so much ..you saved me…. Thanks, Thanks, Thanks, Thanks…..

  9. Pranav says:

    Can u explain a login in php using web services

  10. Javaid says:

    This topic has really helped me out. Very simple tutorial on webservice for the begineers.

  11. Max John says:

    Hello Please can you do your script in the procedural way and then in the class manner. Some of us have only mastered the procedural way and all those who know classes know procedural (functions).
    Thanks

  12. Benny says:

    Hi guys, I urgently need help.
    For years we program in php but recently I approached the world of the web service, and I have some difficulties to understand them … I hope I can help.
    First I tried to copy and paste the script proposed on my server but does not want to work, but if I use external service wsdl everything works correctly …. Is not there need some special configuration of the server to act as ” server-side “?

    Also with a bit of effort I managed to make simple webservice, but now I find myself to have a particular need …. An external I send a file (xml) in parts base_64 and I should read it and save it on the server a single file decrypted.
    How do I get the files? Can I read them as if it were a $ _files?

    And if dovvessi find me in the situation of having to send a xml file (thus act as a server) as I do?

    I hope you can help me with examples of why I’m really in trouble.

    Thanks so much

  13. Shaan Ansari says:

    need to integrate bluedart api using webservices bhaijaan need your help integration on core php

  14. Manvi Agarwal says:

    How to pass multiple parameter and using username and password.

  15. Manvi Agarwal says:

    How to pass multiple parameter and using username and password.

  16. Ravi Dangar says:

    how to update table using php web services in mysql

  17. Amanjeet Kaur says:

    Thankyou sir! this is only site that made me understand soap service…easy appraoch.
    Please if you could say how to use this in wsdl mode.

  18. Satish Sood says:

    Line No 8 of SimpleClient.php is:-
    ‘location’ => “http://localhost/hello/HelloServer.php”,

    It should be

    ‘location’ => “http://localhost/hello/SimpleServer.php”,

    HelloServer.php needs to be corrected as SimpleServer.php

  19. Kari Järvinen says:

    Can you update your example code with corrections suggested. Does SimpleView.php work?

Leave a Reply

Your email address will not be published. Required fields are marked *