0

First, apologies in advance for what may be a pretty green question! I'm only just getting the hang of PHP, and there's not a ton of support out there for APIs...

I'm currently trying to filter some listing results I've returned using the Etsy API. I'd like to limit them to only specific category or taxonomy ID, but none of the PHP conditionals I'm using seem to be returning any results. A few of the things I've tried so 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 as $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 for and 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 as $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 like I'm missing something obvious here, but I'm super stuck. Any guidance on what I'm doing wrong?


Sample responses returned for a listing using the call included in above code:

results:

[ 
{ listing_id: 264154010, state: "active", user_id: 22167794, category_id: 69190377, title: "Swim with 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 and Hug Me Baby Blanket Afghan Crochet PATTERN by 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" ], },

  • I should note as well that I'm able to return results when not filtering by taxonomy, but just next_page. – abbeycadabara Jan 15 '16 at 22:40
  • `$response = json_decode($response_body); $taxo = $response->results->taxonomy_id; if($taxo == 66) {...` could be incorrect. What's the value of `$taxo` ? – Kisaragi Jan 15 '16 at 22:51
  • @Kisaragi - $taxo could be many different values, but I'd like to filter results to only listings with a taxonomy_id of 66. I've verified that there is a taxonomy_id with this value, and that there should be some results in there with that ID. – abbeycadabara Jan 15 '16 at 22:53
  • Need to see the data structure. – Kisaragi Jan 15 '16 at 22:55
  • Invalid key. just post a sample response – Kisaragi Jan 15 '16 at 23:12
  • Added to above - result sets are pretty long, sorry about that. – abbeycadabara Jan 15 '16 at 23:23
  • Update on this: after some additional testing, I realized $taxo = $response->results->taxonomy_id is not producing results unless included in the foreach loop (though I don't really understand why that's the case...any clue?). Still, even if I include $listing->taxonomy_id in the foreach loop and then add a specific ID as a conditional in my for/while, I'm getting null. This also occurs for other fields, like views, etc. – abbeycadabara Jan 20 '16 at 00:39
  • Refer to my 2nd comment: Its because you're not initially using any pointers to get your value. If you wanted to get a value the same way outside of the loop : `$taxo = $response->results[0]->taxonomy_id`. Notice the `[0]` – Kisaragi Jan 20 '16 at 14:24
  • Hmm. It looks like including [0] will only return the taxonomy for the first listing result, though, which isn't what I'm looking to do (I still want to identify the unique ID for each listing). This also seems problematic, as the ID for that place will likely change over time. I don't mind setting it within the foreach loop, but it looks like this is causing some performance issues. – abbeycadabara Jan 24 '16 at 23:38

1 Answers1

0

All right, I figured it out! In case anyone else is looking for the answer in the future, my somewhat 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 as $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 a little with understanding why setting $taxo outside of the foreach loop was unsuccessful, so any insight there is still appreciated.

  • BECAUSE your if condition would only run if the FIRST ID `$taxo = $response->results[0]->taxonomy_id` met this condition. Since you set this now in your loop it checks EACH ID. Do you understand now? Refer to comment 2 of the OP. – Kisaragi Jan 25 '16 at 14:04