Sensation Energy

Tutorials


How to use web services

Web Service

Introduction

Web services are communication services between two computers. In this tutorial we will use SOAP and REST and our databases will be JSON and XML. And all of that together with PHP I MySql.

This means that when you create your JSON or KML file (database) you can send it as a service in an application written in another or php programming language. You can also receive databases and plug them into your application.

Example: You can create your json in our" Union Database" and pass it, and then upload it to any service in any programming language.

Union Database

View/Export/Upload

This is an example, and you can see the details and description of the code on our JSON and XML tutorial in a frame "Function 7 & Grid" at the following address:

Function 7 & Grid

Connect with database and database

Connection and base are required when working with JSON and XML with base.

conn.php


<?php
$server = "localhost";
$user = "root";
$password = "root";
$base = "example";
$con = mysqli_connect($server, $user, $password, $base);
?>

xml- database


  CREATE TABLE `xml` (
  `user_id` int(20) NOT NULL,
  `name` varchar(100) NOT NULL,
  `email` varchar(100) NOT NULL,
  `year` int(20) NOT NULL
  ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
  ALTER TABLE `users`
  ADD PRIMARY KEY (`user_id`);
  ALTER TABLE `users`
  MODIFY `user_id` int(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=102;


How they work web services

Note to better understand:

  • 1. You create database file Example user.json
    
    [
    {
    "user_id":"0",
    "name":"zdravo",
    "email":"admin@mail.com",
    "years":"55"
    },
    {
    "0":"67",
    "id":"67",
    "1":"0",
    "user_id":"0",
    "2":"test",
    "name":"admin",
    "3":"admin1@mail.com",
    "email":"admin2@mail.com",
    "4":"55",
    "years":"55"
    }
    ]
    
    
    
    <?xml version="1.0" encoding="utf-8"?>
    <parent>
    <child>
    <user_id>1</user_id>
    <name>admin</name>
    <years>20</years>
    <email>admin@mail.com</email>
    </child>
    <child>
    <user_id>2</user_id>
    <name>admin2</name>
    <years>22</years>
    <email>admin2@mail.com</email>
    </child>
    </parent>
    
    
  • 2. You send JSON or XML database filese. Example user.json
  • 3. Accept and insert into the application to whom you sent it
  • 4. Someone sends you KML or Json data base files, you download it and put it in your application
  • 5. It is very important that when someone sends you you have to customize the database to fail your application or vice versa

Note to better understand:

  • user_id - id
  • ime - name
  • godina - years
  • email - email
  • osoba.json - user.jsoun
  • osoba.xml - user.xml
  • roditelj - parent
  • dete - child
  • books - books

Examples:


1. <a href="osoba.xml" target="_blank"><button class="btn-2">View XML file</button></a>
2. <a href="osoba.xml" download><button class="btn-3">Export XML file</button></a>
3. <a href="osoba.json" target="_blank"><button class="btn-1">View JSON file</button></a>
4. <a href="osoba.json" download><button class="btn-5">Export JSON  file</button></a>

Rest

Representational state transfer (REST) is a de-facto standard for a software architecture for interactive applications that typically use multiple Web services. (source: Wikipedia)

To know if you have an interactive application that needs a database, then you can use a REST server to send data from the database to the application.

REST API (PHP MySql)

The API is the URL of a web application that sends HTTP requests to GET and takes POST data. The Application Programming Interface (API) is an application programming interface and is a set of protocols and tools for creating applications. The API allows communication between different programming languages.

In the example below I will show you the PHP procedure that allows us the API through JSON.


<?php
//connection
require 'conn.php';
header("Content-Type: application/json");
header("Acess-Control-Allow-Origin: *");
//standard select  and num rows procedural PHP
$con = "SELECT * FROM accounts";
$res = mysqli_query($konekcija, $con);
$no = mysqli_num_rows($res);
//Classic IF loop that tests if "no > 0 "show data in JSON format, and if "<" or "=" then show message no data
if($no > 0)
{
$va = mysqli_fetch_all($res);
echo json_encode($va);
}
else
{
echo json_encode(array("Message" => "Values not found.", "status" => false));
}
?>

An example is a combination of PHP, MySql and XML

As we previously stated the description of the code for REST (PHP, MySql and XML) can be found at Function 7 & Grid - "F 7 JSON & XML"


<table>
<thead>
<tr>
<th>Id</th>
<th>User_id</th>
<th>Name</th>
<th>Email</a></th>
<th>Year</th>
</tr>
</thead>
<tbody>
<?php
require 'conn.php';
$sql = "SELECT * FROM xml";
$v = array();
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result)){
$id= $row['id'];
$user_id= $row['user_id'];
$name= $row['name'];
$email= $row['email'];
$year =  $row['year'];
?>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $user_id; ?></td>
<td><?php echo $name; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $year; ?></td>
</tr>
<?php
array_push($v, $row);
$v = array();
$user_id = $row[1];
$name = $row[2];
$email = $row[3];
$year = $row[4];
$v[]= array (
'user_id' => $user_id,
'name' => $name,
'email' => $email,
'year' => $year
);
$v[] = $row;
}
?>
<?php
$fp = fopen('osoba.json', 'w');
fwrite($fp, json_encode($v));
fclose($fp)
?>
<?php
function createXMLfile($v){
$filePath = 'users.xml';
$dom = new DOMDocument('1.0', 'utf-8');
$root = $dom->createElement('books');
for($i=0; $i< count($v); $i++){
$user_id  =  $v[$i]['user_id'];
$name = $v[$i]['name'];
$email = $v[$i]['email'];
$year = $v[$i]['year'];
$cha = $dom->createElement('child');
$user_id = $dom->createElement('user_id', $user_id);
$child->appendChild($user_id);
$name = $dom->createElement('name', $name);
$child->appendChild($name);  $year = $dom->createElement('year', $year);
$child->appendChild($year);
$email = $dom->createElement('email', $email);
$child->appendChild($email);
$root->appendChild($child);
}
$dom->appendChild($root);
$dom->save($filePath);
}
?>
</tbody>
</table>

Soap

SOAP (formerly an acronym for Simple Object Access Protocol) is a messaging protocol specification for exchanging structured information in the implementation of web services in computer networks. source: Wikipedia)

When you have a specific file SOP allows you to shoulder the message, To know that you can receive or send specific files. Example JSON and XML.

An example is a combination of PHP and XML

As we previously stated the description of the code for SOAP (PHP, XML) can be found at Function 7 & Grid - "F 7 JSON & XML"


<table>
<thead>
<tr>
<th>Name</th>
<th>Email</a></th>
<th>Year</th>
</tr>
</thead>
<tbody>
<?php
$books = simplexml_load_file('users.xml');
file_put_contents('users.xml', $books->asXML());
?>
<?php
foreach($books as $dete)
{
?>
<tr>
<td><?php echo $dete->name; ?></td>
<td><?php echo $dete->email; ?></td>
<td><?php echo $dete->year; ?></td>
</tr>
<?php } ?>
</tbody>
</table>

Content


Documentation




Answer the following question:

You must be logged in


Advertising