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>"; ?>
Tutorial Categories:
i try to use code above,
but i have error as “looks like we got no XML document”
how to solve it ?
thank you
i tried this but am not getting any result
any error? please post here.
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.
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.
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 !
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/"));
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.
nhgjghjghjg
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.
Could you explain me ?
Thank you !
uff… thank you so much ..you saved me…. Thanks, Thanks, Thanks, Thanks…..
Can u explain a login in php using web services
This topic has really helped me out. Very simple tutorial on webservice for the begineers.
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
Thanks for your suggestion we will follow for our upcoming tutorials
Tks a lot. And please can u do a script on how to upload the content of a csv file to a form which will enable you to choose where each field should be inserted ?
working on it and hopefully make something soon
I think I am in love with PhpGang Lol
Thanks for your suggestion we will follow for our upcoming tutorials
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
need to integrate bluedart api using webservices bhaijaan need your help integration on core php
How to pass multiple parameter and using username and password.
How to pass multiple parameter and using username and password.
how to update table using php web services in mysql
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.
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
Can you update your example code with corrections suggested. Does SimpleView.php work?