PHP表单搜索JSON API

时间:2013-12-23 17:49:35

标签: php json api

我正在尝试使用query_result.php页面为API JSON提要创建一个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>&nbsp; <span><?php Echo $entry->AskingPriceFormatted?>
                                    </span>&nbsp; <span><?php Echo $entry->ApproxPriceFormatted ?></span>&nbsp; (<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>&nbsp; <span class="SearchData"><?php Echo $entry->LOAFeet ?>
                                                </span>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <span class="SearchLabel">Model Year:</span>&nbsp; <span class="SearchData"><?php Echo $entry->YearBuilt ?>
                                                </span>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <span class="SearchLabel">Beam:</span>&nbsp; <span class="SearchData"><?php Echo $entry->BeamFeet ?>
                                                </span>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <span class="SearchLabel">Draft:</span>&nbsp; <span class="SearchData"><?php Echo $entry->DraftFeet ?>
                                                </span>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <span class="SearchLabel">Staterooms/Sleeps:</span>&nbsp; <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>&nbsp; <span class="SearchData"><?php Echo $entry->Builder ?>
                                                </span>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <span class="SearchLabel">Model:</span>&nbsp; <span class="SearchData"><?php Echo $entry->Model ?>
                                                </span>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <span class="SearchLabel">Type:</span>&nbsp; <span class="SearchData"><?php Echo $entry->MainCategory ?>
                                                </span>&nbsp;
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <span class="SearchLabel">Engine:</span>&nbsp; <span class="SearchData"><?php Echo $entry->EngineInfo ?>
                                                </span>
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <span class="SearchLabel">Location:</span>&nbsp; <span class="SearchData"><?php Echo $entry->VesselLocation ?>
                                                </span>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </tbody>
                    </table>
    <?php
    }
    }
}

 ?> 
 </body>
</html> 

2 个答案:

答案 0 :(得分:0)

据我所知,我看到两种可能的解决方案:

  1. 由于您要查询API,请检查API是否允许“YearBuilt”参数,以便他们为您过滤结果。在这种情况下,您只需更改API请求,代码中的其他内容也不会更改。

  2. 如果上述方法不起作用,您可以随时自行过滤。在foreach循环中,添加if语句以检查$entry->YearBuilt是否与表单中输入的年份相匹配(可能通过与$_GET['YearBuilt']的比较。

  3. 修改

    如果您无法在API级别进行过滤,请在if中添加foreach语句,如下所示:

    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获得的船只年份进行比较,并仅保留两者匹配的结果。

答案 1 :(得分:0)

$ service_url =&#39; http://data.yatco.com/dataservice/&#39; 。 $ apikey。 &#39;?/搜索页大小= 5&安培;的PageIndex = 0&安培;格式= JSON&安培; VesselType =电机&安培; LOARange = 30135&安培; YearRange = 1999,2000&#39 ;;

试试这个,你会得到最好的结果