如何使用url从休息Web服务中的数据库中获取一个特定项?

时间:2014-11-14 21:18:25

标签: php xml json web-services rest

我是php和webservices中的新手,并尝试构建一个简单的休息网络服务,我的数据库中有一个表,坐标为几个城市。

我编写此代码以获取xml中的数据:

<?php 

//get data
require_once('connecttodatabase.php'); 
mysql_select_db($database_excal, $excal);

//build query
$query = 
  "SELECT 
  cityId, 
  cityName, 
  cityLon, 
  cityLat 
  FROM cities ORDER BY cityId ASC";

$rscities = mysql_query($query, $excal) or 
  die(mysql_error());
$row_rscities = mysql_fetch_assoc($rscities);
$totalRows = mysql_num_rows($rscities);

// build root XML element
$cities = new SimpleXMLElement("<cities></cities>");

// loop data and build data structure
$i=0;
while ($i < $totalRows) {
  $cityId = mysql_result($rscities, $i, 'cityId');
  $cityName = htmlentities( mysql_result($rscities, $i, 'cityName'), ENT_QUOTES, 'UTF-8');
  $cityLon = htmlentities( mysql_result($rscities, $i, 'cityLon'), ENT_QUOTES, 'UTF-8');
  $cityLat = htmlentities( mysql_result($rscities, $i, 'cityLat'), ENT_QUOTES, 'UTF-8');

  $city = $cities->addChild('city');
  $city->addChild('cityId', $cityId);
  $city->addChild('title', $cityName);
  $city->addChild('description', $cityLon);
  $city->addChild('graphic', $cityLat);

  $i++;
}
mysql_free_result($rscities); 

//format for pretty printing
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($cities->asXML());

//Send to browser
header('Content-type: text/xml');
echo $dom->saveXML();
?>

当我输入url到这个文件时,我得到了这些:

<?xml version="1.0"?>
<cities>
  <city>
    <cityId>1</cityId>
    <title>london</title>
    <description>-0.13</description>
    <graphic>51.51</graphic>
  </city>
  <city>
    <cityId>2</cityId>
    <title>tokyo</title>
    <description>139.69</description>
    <graphic>35.69</graphic>
  </city>
  <city>
    <cityId>3</cityId>
    <title>paris</title>
    <description>2.35</description>
    <graphic>48.85</graphic>
  </city>
</cities>

我也写这段代码来获取json中的数据:

<?php 

//get data
require_once('connecttodatabase.php'); 
mysql_select_db($database_excal, $excal);

//build query
$query = 
  "SELECT 
  cityId, 
  cityName, 
  cityLon, 
  cityLat 
  FROM cities ORDER BY cityId ASC";

$rscities = mysql_query($query, $excal) or 
  die(mysql_error());

$arRows = array();
while ($row_rscities = mysql_fetch_assoc($rscities)) {
  array_push($arRows, $row_rscities);
}

header('Content-type: application/json');
echo json_encode($arRows);

?>

我从这段代码中得到了这些:

[{"cityId":"1","cityName":"london","cityLon":"-0.13","cityLat":"51.51"},{"cityId":"2","cityName":"tokyo","cityLon":"139.69","cityLat":"35.69"},{"cityId":"3","cityName":"paris","cityLon":"2.35","cityLat":"48.85"}]

我的问题是如何使用url中的某些参数获取这些城市中的一个,而不是获取所有数据库,例如当我输入此url localhost / coordinates / cities.php?cityName = london我只得到伦敦坐标。< / p>

像这样的东西,依赖webservice:http://api.openweathermap.org/data/2.5/weather?q=London&mode=xml

2 个答案:

答案 0 :(得分:0)

您必须在sql语句中使用where子句。

$query = 
"SELECT 
cityId, 
cityName, 
cityLon, 
cityLat 
FROM cities 
WHERE cityName = '".$some_variable."'
ORDER BY cityId ASC";

答案 1 :(得分:0)

从链接中获取参数:

$city = $_GET['city'];

然后你用

$query = 
  "SELECT 
  cityId, 
  cityName, 
  cityLon, 
  cityLat 
  FROM cities 
  WHERE cityName=" . mysql_real_escape($city) . "
ORDER BY cityId ASC";

有关我建议的where子句的更多信息: http://www.w3schools.com/sql/sql_where.asp

另外我真的建议您使用PDO而不是mysql_query http://php.net/manual/de/function.mysql-query.php

希望这有帮助!