使用PHP /条件

时间:2016-01-15 22:38:19

标签: php api etsy

首先,提前为可能是一个非常绿色的问题道歉!我只是掌握了PHP,并没有大量支持API ...

我目前正在尝试过滤使用Etsy API返回的一些列表结果。我想将它们仅限于特定的类别或分类ID,但我正在使用的PHP条件似乎都没有返回任何结果。到目前为止我尝试过的一些事情:

<?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;

}

?>

我在这里尝试了两种方式:

<?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";
}

?>

我觉得我在这里遗漏了一些明显的东西,但我太过困难了。关于我做错了什么的指导?

使用上述代码中包含的调用为列表返回的示例响应:

结果:

[         {         listing_id:264154010,         州:“活跃”,         user_id:22167794,         category_id:69190377,         标题:“与美人鱼打印,手刻字,水彩,绿紫蓝”,         creation_tsz:1452898819,         ending_tsz:1463349619,         original_creation_tsz:1452898657,         last_modified_tsz:1452898849,         价格:“10.00”,         currency_code:“USD”,         数量:15,         标签:[         “彩虹”,         “独角兽”,         “仙女”,         “美人鱼”,         “紫色”,         “蓝色”,         “绿色”,         “手刻字”,         “抽象”,         “刻字”,         “手”,         “水彩”         ]         category_path:[         “艺术”,         “打印”,         “画册专辑”         ]         category_path_ids:[         68887312,         68892154,         69190377         ]         网址:“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:[         “艺术与收藏品”,         “打印”,         “画册专辑”         ]         },         {         listing_id:234448248,         州:“活跃”,         user_id:30961143,         category_id:68887486,         标题:“Peach.Unicorn保持冷静,拥抱我的婴儿毯阿富汗钩针图案”,         creation_tsz:1452898542,         ending_tsz:1463349342,         original_creation_tsz:1432388408,         last_modified_tsz:1452898542,         价格:“1.99”,         currency_code:“GBP”,         数量:41,         标签:[         “宝宝”,         “毯”,         “钩针”         “钩针图案”,         “毯子图案”,         “保持冷静”,         “泡芙针”,         “婴儿毯”,         “立即下载”,         “女婴”,         “小男孩”,         “拥抱我”,         “dk纱线图案”         ]         category_path:[         “模式”         ]         category_path_ids:[         68887486         ]         网址:“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:[         “工艺用品和工具”,         “模式和教程”         ]         },

1 个答案:

答案 0 :(得分:0)

好吧,我明白了!如果其他人在将来寻找答案,我的解决方案有点凌乱:

<?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;

}

?>

仍在努力了解为什么在foreach循环之外设置$ taxo是不成功的,所以任何洞察力仍然受到赞赏。