Reputation: 343
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
},...
Upvotes: 1
Views: 508
Reputation: 5682
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.
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
Upvotes: 1