如何在 marketcheck.com 的Wordpress网站上使用curl执行API?



我是API的新手,我想通过我的WordPress网站上的API从 marketcheck.com 获取数据,以搜索汽车库存。我也不知道在哪里编写 API 代码,无论是在函数.php文件中还是在其他地方。以下是 marketcheck.com 提供的汽车搜索链接:

http://marketcheck-prod.apigee.net/v2/search/car/active?api_key={{api-key}}&year=2015&make=ford&host=marketcheck-prod.apigee.net

在此处输入图像描述

WordPress有一套完整的函数,可以在WordPress环境中使用,而不是cURL。

您可以在函数中创建函数.php以在数组中返回数据,或者在模板中,使用wp_remote_get函数集来获取 BODY 和json_decode。

$url = 'http://marketcheck-prod.apigee.net/v2/search/car/active?api_key={{api-key}}&year=2015&make=ford&host=marketcheck-prod.apigee.net';
$response = wp_remote_get($url);
$body = json_decode(wp_remote_retrieve_body($response), true);

然后,您将遍历$body结果以检索数据。 您似乎想遍历$body>列表

foreach ($body->listings as $listing){
// Do your loop magic here
print_r($listing); // to review your loop data on screen
}

最新更新