Trouble filtering API results with PHP/conditionals -
first, apologies in advance may pretty green question! i'm getting hang of php, , there's not ton of support out there apis...
i'm trying filter listing results i've returned using etsy api. i'd limit them specific category or taxonomy id, none of php conditionals i'm using seem returning results. few of things i've tried far:
<?php define("api_key", xxx); $url = "https://openapi.etsy.com/v2/listings/active?tags=unicorn,unicorns&keywords=unicorn,unicorns&includes=images:1:0&api_key=" . api_key; while (isset($url) && $url != '' && $next_page < 3) { $curl = curl_init($url); curl_setopt($curl, curlopt_returntransfer, 1); $response_body=curl_exec($curl); curl_close($curl); $response = json_decode($response_body); $taxo = $response->results->taxonomy_id; if($taxo == 66) { foreach ($response->results $listing) { echo "<li>" . '<a href="' . $listing->url . '" target="_blank"><img src="' . $listing->images[0]->url_170x135 . '" alt=""></a>' . "<p>" . $listing->title . "</p>" . "<p>~*~" . $listing->price . " " . $listing->currency_code . "~*~</p>" . $listing->taxonomy_id . "</li>"; } } else { echo "no results"; } $next_page = $response->pagination->next_page; $baseurl = "https://openapi.etsy.com/v2/listings/active?tags=unicorn,unicorns&keywords=unicorn,unicorns&includes=images:1:0&api_key=" . api_key . "&page="; $url = $baseurl . $next_page; } ?>
i've tried both , while here:
<?php define("api_key", xxx); $url = "https://openapi.etsy.com/v2/listings/active?tags=unicorn,unicorns&keywords=unicorn,unicorns&includes=images:1:0&api_key=" . api_key; if(isset($url) && $url != '' && $next_page < 3 && $taxo == 66) { $curl = curl_init($url); curl_setopt($curl, curlopt_returntransfer, 1); $response_body=curl_exec($curl); curl_close($curl); $response = json_decode($response_body); $taxo = $response->results->taxonomy_id; foreach ($response->results $listing) { echo "<li>" . '<a href="' . $listing->url . '" target="_blank"><img src="' . $listing->images[0]->url_170x135 . '" alt=""></a>' . "<p>" . $listing->title . "</p>" . "<p>~*~" . $listing->price . " " . $listing->currency_code . "~*~</p>" . $listing->taxonomy_id . "</li>"; } $next_page = $response->pagination->next_page; $baseurl = "https://openapi.etsy.com/v2/listings/active?tags=unicorn,unicorns&keywords=unicorn,unicorns&includes=images:1:0&api_key=" . api_key . "&page="; $url = $baseurl . $next_page; var_dump($url); } else { echo "no results"; } ?>
i feel i'm missing obvious here, i'm super stuck. guidance on i'm doing wrong?
sample responses returned listing using call included in above code:
results:
[ { listing_id: 264154010, state: "active", user_id: 22167794, category_id: 69190377, title: "swim mermaids print, hand lettering, watercolor, green blue purple", creation_tsz: 1452898819, ending_tsz: 1463349619, original_creation_tsz: 1452898657, last_modified_tsz: 1452898849, price: "10.00", currency_code: "usd", quantity: 15, tags: [ "rainbows", "unicorns", "fairies", "mermaids", "purple ", "blue", "green", "hand lettering", "abstract", "lettering", "hand", "watercolor" ], category_path: [ "art", "print", "giclee" ], category_path_ids: [ 68887312, 68892154, 69190377 ], url: "https://www.etsy.com/listing/264154010/swim-with-mermaids-print-hand-lettering?utm_source=funappyay&utm_medium=api&utm_campaign=api", taxonomy_id: 121, taxonomy_path: [ "art & collectibles", "prints", "giclee" ], }, { listing_id: 234448248, state: "active", user_id: 30961143, category_id: 68887486, title: "keep calm , hug me baby blanket afghan crochet pattern peach.unicorn", creation_tsz: 1452898542, ending_tsz: 1463349342, original_creation_tsz: 1432388408, last_modified_tsz: 1452898542, price: "1.99", currency_code: "gbp", quantity: 41, tags: [ "baby", "blanket", "crochet", "crochet pattern", "blanket pattern", "keep calm", "puff stitch", "baby blanket", "instant download", "baby girl", "baby boy", "hug me", "dk yarn pattern" ], category_path: [ "patterns" ], category_path_ids: [ 68887486 ], url: "https://www.etsy.com/listing/234448248/keep-calm-and-hug-me-baby-blanket-afghan?utm_source=funappyay&utm_medium=api&utm_campaign=api", taxonomy_id: 729, taxonomy_path: [ "craft supplies & tools", "patterns & tutorials" ], },
all right, figured out! in case else looking answer in future, messy solution:
<?php define("api_key", xxx); $url = "https://openapi.etsy.com/v2/listings/active?tags=unicorn,unicorns&keywords=unicorn,unicorns&includes=images:1:0&api_key=" . api_key; while(isset($url) && $url != '') { $curl = curl_init($url); curl_setopt($curl, curlopt_returntransfer, 1); $response_body=curl_exec($curl); curl_close($curl); $response = json_decode($response_body); foreach ($response->results $listing) { $taxo = $listing->taxonomy_id; if($taxo == 41 || $taxo == 873){ echo '<li><a href="' . $listing->url . '" target="_blank"><img src="' . $listing->images[0]->url_170x135 . '" alt=""></a>' . "<p>" . $listing->title . "</p>" . "<p>~*~" . $listing->price . " " . $listing->currency_code . "~*~</p>" . $taxo . '</li>'; } } $next_page = $response->pagination->next_page; $baseurl = "https://openapi.etsy.com/v2/listings/active?tags=unicorn,unicorns&keywords=unicorn,unicorns&includes=images:1:0&api_key=" . api_key . "&page="; $url = $baseurl . $next_page; } ?>
still struggling little understanding why setting $taxo outside of foreach loop unsuccessful, insight there still appreciated.
Comments
Post a Comment