使用ajax在JSON api url中搜索

时间:2022-08-22 10:23:51

I have an api URL it's output JSON, it is about daily deals like groupon.

我有一个api URL它的输出JSON,它是关于像groupon这样的每日交易。

I want to create a search form and client search in this json

我想在这个json中创建一个搜索表单和客户端搜索

Samle json url here

Samle json url在这里

I'm decode JSON like that and show it but how to search in this api url i don't know.

我正在解码这样的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);

Search form should search in deals: title, description

搜索表单应搜索交易:标题,描述

Sample deals json block

示例处理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 个解决方案

#1


1  

I assume you want to search on the server-side using PHP. That can be done with a for loop on deals. The following code loops the deals and filters based on a given keyword:

我假设您想使用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);

I tried to search about the API you are using. I was looking for the manual/reference to see if this can be done on the query in a similar way you select tags and city but I couldn't find it. That would be a better (more efficient) solution since the provider filters the results for you.

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

Client-Side: Bootstrap-tables

Updating based on the comments.

根据评论进行更新。

I have tried to make a basic bootstrap example but it fails: The API does not set the correct "Access-Control-Allow-Origin" headers and thus the browser blocks the data. You might have to proxy the API through your PHP script. Make sure you add the correct headers. It can still be a good starting point, you can find it here

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

#1


1  

I assume you want to search on the server-side using PHP. That can be done with a for loop on deals. The following code loops the deals and filters based on a given keyword:

我假设您想使用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);

I tried to search about the API you are using. I was looking for the manual/reference to see if this can be done on the query in a similar way you select tags and city but I couldn't find it. That would be a better (more efficient) solution since the provider filters the results for you.

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

Client-Side: Bootstrap-tables

Updating based on the comments.

根据评论进行更新。

I have tried to make a basic bootstrap example but it fails: The API does not set the correct "Access-Control-Allow-Origin" headers and thus the browser blocks the data. You might have to proxy the API through your PHP script. Make sure you add the correct headers. It can still be a good starting point, you can find it here

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