[英]PHP Form Search JSON API
我正在嘗試為API JSON提要創建一個帶有query_result.php頁面的query.php。 我一次可以得到10件商品的退貨,但是通過查詢“年份”,我仍然不能只獲得那一年,例如YearBuilt =“ 1999”我仍然可以得到所有年份的退貨船只。
QUERY.PHP
<!DOCTYPE HTML>
<html>
<head>
<title>Yacht Search</title>
<link type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/themes/ui-darkness/jquery-ui.css" rel="stylesheet">
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.2/jquery-ui.min.js"></script>
</head>
<body>
<form action="http://www.yachts.theskywatergroup.com/yatco_data_feeds/datafeed_customer/query_results.php" target="_blank" method="get">
Year Built: <input id="YearBuilt" type="text" placeholder="Year Built" name="YearBuilt" value="" size="7" maxlength="4" />
<input name="search" type="submit" class="" value="Search Yachts" />
</form>
</body>
</html>
QUERY_RESULTS.PHP
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<link href="http://datafeed.yatco.com/yatco.css" rel="stylesheet" type="text/css" />
<meta charset="utf-8">
</head>
<body>
<?php
$apikey = 'XXX';
$service_url = 'http://data.yatco.com/dataservice/' . $apikey . '/search?pagesize=10&pageindex=0&format=json';
$curl = curl_init($service_url);
//curl_setopt($curl, CURLOPT_REFERER, 'http://localhost');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$curl_response = curl_exec($curl);
if ($curl_response === false) {
$info = curl_getinfo($curl);
curl_close($curl);
die('error occured during curl exec. Additioanl info: ' . var_export($info));
}
curl_close($curl);
$decoded = json_decode($curl_response);
if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {
die('error occured: ' . $decoded->response->errormessage);
}
foreach ($decoded as $name => $value) {
if ($name != 'Vessels') {
echo $name . ' = ' . $value . '<br />';
} else {
foreach ($value as $entry) {
?>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td style="background-color: #a1a1a1; height: 1px;" colspan="3">
</td>
</tr>
<tr>
<td class="SearchHeader" colspan="3">
<span><?php Echo $entry->Boatname ?></span> <span><?php Echo $entry->AskingPriceFormatted?>
</span> <span><?php Echo $entry->ApproxPriceFormatted ?></span> (<a href="#"
class="ResultsLink" onclick="viewVessel(<?php Echo $entry->VesselID ?>);">View Details</a>)
</td>
</tr>
<tr>
<td valign="top" align="left" width="200px"><img src='<?php Echo $entry->ProfileURL ?>' alt="" title="" style="max-height: 200px;
max-width: 200px;" onclick="viewVessel(<?php Echo $entry->VesselID ?>);" /></td>
<td valign="top" style="padding: 5px 0px 0px 5px;" width="200px">
<table>
<tr>
<td>
<span class="SearchLabel">LOA:</span> <span class="SearchData"><?php Echo $entry->LOAFeet ?>
</span>
</td>
</tr>
<tr>
<td>
<span class="SearchLabel">Model Year:</span> <span class="SearchData"><?php Echo $entry->YearBuilt ?>
</span>
</td>
</tr>
<tr>
<td>
<span class="SearchLabel">Beam:</span> <span class="SearchData"><?php Echo $entry->BeamFeet ?>
</span>
</td>
</tr>
<tr>
<td>
<span class="SearchLabel">Draft:</span> <span class="SearchData"><?php Echo $entry->DraftFeet ?>
</span>
</td>
</tr>
<tr>
<td>
<span class="SearchLabel">Staterooms/Sleeps:</span> <span class="SearchData"><?php Echo $entry->NumStaterooms ?></span>/
<span class="SearchData"><?php Echo $entry->NumSleeps ?></span>
</td>
</tr>
<tr>
<td height="5">
</td>
</tr>
</table>
</td>
<td valign="top" style="padding: 5px 0px 0px 5px;">
<table>
<tr>
<td>
<span class="SearchLabel">Builder:</span> <span class="SearchData"><?php Echo $entry->Builder ?>
</span>
</td>
</tr>
<tr>
<td>
<span class="SearchLabel">Model:</span> <span class="SearchData"><?php Echo $entry->Model ?>
</span>
</td>
</tr>
<tr>
<td>
<span class="SearchLabel">Type:</span> <span class="SearchData"><?php Echo $entry->MainCategory ?>
</span>
</td>
</tr>
<tr>
<td>
<span class="SearchLabel">Engine:</span> <span class="SearchData"><?php Echo $entry->EngineInfo ?>
</span>
</td>
</tr>
<tr>
<td>
<span class="SearchLabel">Location:</span> <span class="SearchData"><?php Echo $entry->VesselLocation ?>
</span>
</td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
<?php
}
}
}
?>
</body>
</html>
據我所知,我看到了兩種可能的解決方案:
由於您正在查詢API,因此請檢查API是否允許使用“ YearBuilt”參數,以便它們可以為您過濾結果。 在這種情況下,您只需更改您的API請求,而代碼中的其他操作都不會更改。
如果上述方法不起作用,您可以隨時進行過濾。 在foreach循環中,添加一條if語句,以檢查$entry->YearBuilt
是否與在表單中輸入的年份匹配(可能是通過與$_GET['YearBuilt']
進行比較$_GET['YearBuilt']
。
編輯
如果您不能在API級別本身進行過濾,則在您的foreach
添加if
語句,如下所示:
if ($name != 'Vessels') {
echo $name . ' = ' . $value . '<br />';
} else {
foreach ($value as $entry) {
if($entry->YearBuilt == $_GET['YearBuilt']) {
// all of the HTML code as it is
}
?>
$_GET['YearBuilt']
包含用戶選擇的年份。 將其與從$entry->YearBuilt
獲得的船只年份進行比較,僅保留兩者匹配的結果。
$ service_url =' http://data.yatco.com/dataservice/ '。 $ apikey。 '/ search?pagesize = 5&pageindex = 0&format = json&VesselType = motor&LOARange = 30,135&YearRange = 1999,2000';
試試這個,你會得到最好的結果
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.