1

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

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

Samle json url here

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

$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

"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
      },...
Michael Kohne
  • 11,888
  • 3
  • 47
  • 79
Erdal Bakkal
  • 343
  • 1
  • 6
  • 21

1 Answers1

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:

// 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.

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

Community
  • 1
  • 1
urban
  • 5,392
  • 3
  • 19
  • 45
  • thnx you @urban for answer, i dont need use tags and city just search in json array (deals->description and, deals->title).. This code is search but how to use in form and with ajax instantly search? do you have any idea i didnt found any answer about it. My knowladge is a little bit about it. – Erdal Bakkal Dec 26 '15 at 10:48
  • @ErdalBakkal How do you want to display your data? An end-to-end solution would call the PHP script on keydown of a text field (live) and perform some caching. In this case it depends a lot on what you want to use. If you go with tables: I have seen [datatables](https://www.datatables.net/) plugin but not used it. I have used [bootstrap-table](http://issues.wenzhixin.net.cn/bootstrap-table/) plugin but obviously that is for bootstrap... – urban Dec 26 '15 at 11:01
  • yes can you update your answer with bootstap-table? I want to show title. For example im searching "Cinema" and result show in "cinema" keywords has titles of deals. Can you create a so basic example? – Erdal Bakkal Dec 26 '15 at 11:05