Create php webservice in json format

C

<?php
include(‘config.php’);
$webid = $_GET[‘webid’];
$callback = $_REQUEST[‘callback’];

$sql = “SELECT * FROM `Restaurant_main` where `webid` = ‘$webid'”;
$result = mysql_query($sql)or die(mysql_error());
$row1 = mysql_fetch_array($result);
$db_webid = $row1[‘webid’];
if($db_webid == $webid){
$sql1 = “SELECT * FROM `Restaurant_main` where `webid` = ‘$webid'”;
$result1 = mysql_query($sql1)or die(mysql_error());

$response = array();
$posts = array();
while($row=mysql_fetch_array($result1))
{
$id=$row[‘Id’];
$name=$row[‘Name’];
$email=$row[’email’];
$website=$row[‘website’];
$img1=$row[‘img1’];
$img2=$row[‘img2’];

$posts[] = array(‘Id’=> $id, ‘Name’=> $name,’email’=> $email,’website’=> $website,’img1’=> ‘http://demos.nanostuffs.com/RestaurantApp/uploads/’.$img1,’img2’=> ‘http://demos.nanostuffs.com/RestaurantApp/uploads/’.$img2);
}
if ($callback) {
header(‘Content-Type: text/javascript’);
echo $callback . ‘(‘ . json_encode($posts) . ‘);’;
} else {
header(‘Content-Type: application/x-json’);
echo json_encode($posts);
}
}
?>

Note: use this as

http://localhost/RestaurantApp/webservice/res_main_new.php?webid=1&callback=callback1

About the author

swati nuna
By swati nuna

Category