使用ajax在JSON api url中搜索

时间:2015-12-26 10:07:42

标签: javascript php jquery json ajax

我有一个api网址,输出JSON,它是关于groupon的每日交易。

我想在此json中创建搜索表单和客户端搜索

Samle json url here

我正在解码这样的JSON并显示它但是如何搜索这个api url我不知道。

$param = array(
    'apiKey' => 'VN43ZG2LLHKHHIU6',
    'publisherId' => 316,
    //'isPopular' => 'on',
    'p' => $sayfasi,
    'itemInPage' => $sayi
);

if(isset($tagid) && $tagid !='')
    $param['tagIds[]'] = $tagid;

if(isset($cityId) && $cityId !='')
    $param['cityId'] = $cityId;



$jsonurl     = 'http://api.myaffwebsiteurl.com/deal/browse.html?'.http_build_query($param);


//$json        = CurlConnect($jsonurl

$json = file_get_contents($jsonurl);

$json_output = json_decode($json);

搜索表单应搜索交易:标题,说明

示例处理json块

"deals":[  
      {  
         "id":"1041296",
         "title":"Tüm Cinemaximum'larda İndirimli Sinema Biletleri 9.50 TL'den Başlayan Fiyatlarla!",
         "description":"Cinemaximum İndirimli Bilet Fırsatı\r\nCinemaximum'larda indirimli bilet fırsatını kaçırmayın. ",
         "howToUse":null,
         "endDate":"2015-12-26 23:59:59",
         "discountPercent":"41",
         "country":"Turkey",
         "businessIds":"",
         "latLngs":"",
         "city":"",
         "provider":"Fırsat Bu Fırsat",
         "realPriceWithSymbol":"16 TL",
         "dealPriceWithSymbol":"9.50 TL",
         "showDealUrl":"http://www.firsatbufirsat.com/firsat/tum-cinemaximumlarda-indirimli-sinema-biletleri?pid=316",
         "buyDealUrl":"http://www.firsatbufirsat.com/satin-al/1041296/tum-cinemaximumlarda-indirimli-sinema-biletleri.html?pid=316",
         "image200_H":"http://img1.mekan.com/files/images/deal/image/200_H/104/1041296_b9ef.jpg?r=2",
         "timeLeft":43377
      },...

1 个答案:

答案 0 :(得分:1)

我假设你想使用PHP在服务器端进行搜索。这可以通过交易的for循环来完成。以下代码根据给定的关键字循环交易和过滤器:

// Decode json into array instead of object. Object will also
// work but you have to do $deal->title later (I think)
$json_output = json_decode($json, true);

// Items you want to keep based on the given keyword
$keep = [];

foreach ($json_output['deals'] as $deal) {
    if ( strpos($deal["title"], $_REQUEST['keyword']) !== false )
        $keep[] = $deal; // Append this deal

    else if ( strpos($deal["description"], $_REQUEST['keyword']) !== false )
        $keep[] = $deal;

}

// $keep array now has all interesting deals. Use it or 
// Return it to the client as JSON (filtered)
echo json_encode($keep);

我尝试搜索您正在使用的API。我正在寻找手册/参考,看看是否可以通过选择tagscity的类似方式对查询执行此操作,但我无法找到它。这将是一个更好(更有效)的解决方案,因为提供商会为您过滤结果。

客户端:Bootstrap-tables

根据评论进行更新。

我试图制作一个基本的bootstrap示例,但它失败了:API没有设置正确的" Access-Control-Allow-Origin"标题,因此浏览器阻止数据。您可能必须通过PHP脚本代理API。确保添加correct headers。它仍然是一个很好的起点,你可以找到它here