HTML-PHP Webservice



Introduction

       Webservice is a software system designed to support interoperable machine-to-machine interaction over a network. Here I am explaining about how can we communicate between Hybrid App(HTML/CSS/JS - Client) and PHP(Server) using Webservice. Here I made the webservice call from the Jquery script with callback, which sends the results of the long-running operation to the client when the results are finally ready.

Client Side:

    
 $.ajax({
   url: 'http://localhost/webservice.php?callback=',
   type: "GET/POST",
   data:{data:JSON.stringify([{action:'getUserData', userId: 1234}])},
   dataType: "jsonp",
   success: function res(data) 
   {
    alert(data.code);
    //do the actions
   },
   error: function error(response)
   {
     alert("Some Error");
   }
});

Here url is the location of the PHP file. If it is in your localhost will be like as shown above.

Here we are specifying the dataType as the jsonp because we are expecting data of type JSON from the server, and why we didn't specified as JSON because, along with callback datatype JSON will not work.

Server Side:

webservice.php

    
<?php

$requestParametersJson = $_GET['data'];

$requestParametersArr  = json_decode($requestParametersJson],TRUE);  

echo $_GET['callback'] . '(' ."{'code':'success'}". ')';
?>


This way you will get the passed parameters from the client request as JSON. You can can decode that to an array using json_decode. So for example in tha above client request you can access the parameter "action" like $requestParametersArr[0]['action]. And the json value can return to the JS with the callback like above.






Page settings Search Description Options Custom Robots Tags default

No comments:

Post a Comment

Technologies

1. Nodejs
  • Express
  • Loopback
  • SailsJs
2. Angular
  • 2
  • 4
  • 5
  • 8
3. JQuery
4. PHP
  • LAMP
  • Codeigniter
  • Lumen
5. Database
  • MySQL
  • PostgreSQL
  • MongoDB
6. Artificial Intelligence
  • IBM Watson API
7. Amazon
  • S3
  • EC2
  • SES
  • ELB
8. REST & SOAP API integrations
9. Push notifications
  • FCM - Android
  • APNS - IOS
10. RETS(Real estate domain) servers
11. Standard Implementations
  • 508
  • LTI