You are on page 1of 6

Hng dn to web service vi

PHP v MySQL
Kungfu PHP 14 Thng Hai, 2015 Hng dn to web service vi PHP v MySQL2017-05-
06T07:55:23+00:00Tng hp PHP 8 Comments

Bn tng nghe v web service ? Web service l g ? Cch hot ng


ca n nh th no? Trong bi vit ny mnh s gii thiu n cc bn
v nh ngha web service, c im ca web service v hng dn cc
bn cch hin thc 1 web service n gin, c bn cho nhng bn mi
bt u tm hiu v web service vi PHP.

1. Web services l g ?

Web service l mt tp cc phng thc c gi thc hin t xa thng


qua mt a ch url. Kt qu tr v ca web service thng di dng
json hoc xml. Web service thng c s dng to cc ng dng
phn tn.

Kin trc webservice n gin

2. c im ca web service
Khng ph thuc vo ngn ng lp trnh

C th c truy cp t bt c ng dng no

H tr thao tc gia cc thnh phn khng ng nht

Chi ph pht trin thp

D bo tr

3. To web service vi PHP v MySQL

phn ny, mnh s hng dn cc bn cch to 1 web service n


gin, c chc nng cung cp ton b danh sch user c trong database
di dng json v xml.

Bc 1 : To mt bng l users s cha thng tin thnh vin ca ng


dng web nh l : username, password, name, email,

MySQL

1 CREATE TABLE IF NOT EXISTS `users` (


2 `id` int(11) NOT NULL AUTO_INCREMENT,
3 `username` varchar(30) NOT NULL,
4 `password` varchar(30) NOT NULL,
5 `name` varchar(255) NOT NULL,
6 `email` varchar(255) NOT NULL,
7 PRIMARY KEY (`id`)
8 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Bn nn insert mt vi mu d liu vo bng users ny chng ta c


d liu test.

Bc 2 : To mt th mc tn l webservice, t trong th mc gc ca
website. Tip n trong th mc webservice, to 1 file t tn l
connection.php. File ny c trch nhim to kt ni n c s d liu.
PHP

<?php
1
$server_username = "root"; // in username ng nhp mysql
2
$server_password = ""; // in password ng nhp mysql
3
$server_host = "localhost";// in tn host
4
$database = 'kungfuphp'; // tn database
5
6
// to bin kt ni ti database
7
$conn = mysqli_connect($server_host,$server_username,$server_password,$database) or die("khng th
8
kt ni ti database");

Bc 3 : To 1 file t tn l server.php, File ny chu trch nhim ly


thng tin c gi t client, x l d liu v tr ra di dng json hoc
XML. Ni dung ca file ny nh sau :

PHP

1 <?php
2 if(isset($_GET["getUser"])){
3 // kim tra nh dng d liu tr ra l json hay xml
4 $format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml';
5
6
7 //to mng users lu thng tin ton b user trong db
8 $users = array();
9
10 //gi file kt ni db
11 require_once("connection.php");
12 //truy vn ly ton b thng tin trong bng users
13 $sql = "select * from users";
14 $query = mysqli_query($conn,$sql);
15 while ($user = mysqli_fetch_assoc($query) {
16 $users[] = array('user' => $user);
17 }
18 // tr ra d liu di dng json
19 if ($format == 'json') {
20 header('Content-type: application/json');
21 echo json_encode(array('users'=>$users));
22 }else{
23 // tr ra d liu di dng xml
24 header('Content-type: text/xml');
25 echo '<users>';
26 foreach($users as $index => $user) {
27 if(is_array($user)) {
28 foreach($user as $key => $value) {
29 echo '<',$key,'>';
30 if(is_array($value)) {
31 foreach($value as $tag => $val) {
32 echo '<',$tag,'>',htmlentities($val),'</',
33 $tag,'>';
34 }
35 }
36 echo '</',$key,'>';
37 }
38 }
39 }
40 echo '</users>';
41 }
mysqli_close();
}else{
42
echo "Khng c d liu tr v";
43
}

Gii thch 1 t v on code trn, on code trn ly cc tham s


getUser, format c truyn trn url c dng nh sau :

url ly thng tin user tr v di dng json

PHP

1 http://localhost/webservice?getUser&format=json

user ly thng tin user tr v di dng xml

PHP

1 http://localhost/webservice?getUser&format=xml

Vi cc tham s c truyn vo ny, service s thc thi vic truy vn


d liu trong db, tr ra d liu di dng json hoc xml ton b thng
tin user c trong db.

Chng hn vi json, d liu tr ra s c nh dng nh sau :

XHTML

{"users":[{"user":{"id":"1",'username":"teo123","password":"123456",name :"Nguyen Van


Teo","email":"teo123@gmail.com"}},{"user":
1 {"id":"2",'username":"ti123","password":"123456",name :"Nguyen Van Ti","email":"ti123@gmail.com"}},
{"user":{"id":"3",'username":"tam123","password":"123456",name :"Nguyen Van
Tam","email":"tam123@gmail.com"}}}

Cn i vi xml d liu tr ra s nh sau :


XHTML
1 <users>
2 <user>
3 <id>1</id>
4 <username>teo123</username>
5 <password>123456</password>
6 <name>Nguyen Van Teo</name>
7 <email>teo123@gmail.com</email>
8 </user>
9 <user>
10 <id>2</id>
11 <username>ti123</username>
12 <password>123456</password>
13 <name>Nguyen Van Ti</name>
14 <email>ti123@gmail.com</email>
15 </user>
16 <user>
17 <id>3</id>
18 <username>tam123</username>
19 <password>123456</password>
20 <name>Nguyen Van Tam</name>
21 <email>tam123@gmail.com</email>
22 </user>
23 </users>

Bc 5 : Ty thuc vo mc ch s dng, d liu tr v s c x l


khc nhau theo mun ngi s dng. c th ly c d liu t
service, ta s dng phng thc curl hoc file_get_contents.

To mt file l client.php, t trong th mc webservice. Ni dung file


ny n gin ch l ly d liu tr v t webservice thng qua url v x
l d liu theo 1 mc ch no . on code trong client.php s
nh sau :
PHP

1 //Ly d liu tr v dng xml


2 $du_lieu_tra_ve_xml = file_get_contents("http://localhost/webservice?getUser&format=xml");
3
4 //Ly d liu tr v dng json
5 $du_lieu_tra_ve_json = file_get_contents("http://localhost/webservice?getUser&format=json");
6
7 //Code X l d liu tr v theo ca bn ...

4. Tng kt

T v d mc 3, cc bn ch cn hiu n gin webservice ch l 1


dch v, m ti n nhn yu cu thng qua url v cc tham s, sau
n s x l tr v d liu di nh dng xml hoc json theo yu cu
ca ngi dng dch v. Xy dng webservice rt c li v nh dng
json hoc xml l nh dng d liu c th s dng trn nhiu nn tng
nh di ng (android, ios,), website (php, jsp,)

You might also like