2013-12-23 4 views
0

API JSON 피드 용 query_result.php 페이지로 query.php를 만들려고합니다. 한 번에 10 개 항목에 대한 수익을 얻지 만 "연도"를 쿼리하면 여전히 그 해를 얻지 못합니다. 예제 YearBuilt = "1999"나는 모든 연도의 선박을 반환했습니다.PHP 양식 검색 JSON API

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> 
+1

를 얻을 수 있습니다 시도? – Harry

+0

해리, 모델 연도에 입력 한 연도를 반환 할 것이라고 가정했습니다.   YearBuilt?> –

+0

또한 다음은 http : // 쿼리의 URL입니다. yachts.theskywatergroup.com/yatco_data_feeds/datafeed_customer/query2.php. 내가 입력 한 특정 연도의 요트 만 반환하지는 않습니다 ... –

답변

0

: 당신이 조회되기 때문에

  1. 을 API를 사용하여 API가 "YearBuilt"매개 변수를 허용하는지 확인하여 결과를 필터링합니다. 이 경우 API 요청 만 변경하면 코드가 변경됩니다.

  2. 위의 방법으로 해결되지 않으면 항상 필터 처리 할 수 ​​있습니다. 문 $entry->YearBuilt는 올해가 가능 $_GET['YearBuilt']과의 비교를 통해 (형식으로 입력과 일치하는지 여부를 확인하는 경우 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']이의 사용자의 선택이 포함되어 다음과 같이 API 수준 자체는 다음 foreachif 문을 추가 년. $entry->YearBuilt에서 얻은 선박 연도와 비교하고 두 개가 일치하는 경우에만 결과를 유지하십시오.

+0

$ ($ name => $ 값으로 디코딩 된 $) { \t if ($ name! = 'Vessels') { \t \t echo $ name . '='. $ 값. '
'; \t} 다른 { \t의 foreach ($ 항목으로 $ 값) { \t?> –

+0

좋아, 프로그래밍 새로운 것을, 나는 if 문을 수정하지만 여전히 ... foreach는 ($ 이름으로 디코딩 $ 작동하지 않습니다 => $ value) { \t if ($ name! = 'Vessels'&& $ entry-> YearBuilt! = $ _GET [ 'YearBuilt']) { \t \t echo $ name. '='. $ 값.'
'; \t} 다른 \t의 foreach ($ 항목으로 $ 값) { \t?> –

+0

안녕 해리, 응답 주셔서 감사합니다 {. 하지만 그건 효과가 없었습니다. http://yachts.theskywatergroup.com/yatco_data_feeds/datafeed_customer/query2.php. 내가 이것을 연구하면서 생각하고 있는데, "다른"대신에 "and"를 쓰고 싶지 않을까요? 첫 번째 if 문은 값을 반환하므로 "else"문은 실행되지 않습니다. ?? 도와 줘서 고마워. –

0

$ service_url = 'http://data.yatco.com/dataservice/'. $ apikey. '/ search? pagesize = 5 & pageindex = 0 & 형식 = json & VesselType = 모터 & LOARange = 30,135 & YearRange = 1999,2000';

정확히 query_results.php에 양식에 입력 된 "YearBuilt"값을 사용하는 경우이 최상의 결과

+1

원하지 않는 매개 변수를 제거 할 수 있습니다 vessel 유형과 LOARange는 format = json 이후 & YearRange = 1999,2000 '을 사용하십시오. –