如何在WordPress站点用curl调用marketcheck.com API?代码存放位置咨询
Hey there! Let's walk through how to set up the Marketcheck vehicle search API on your WordPress site, and clear up where to put your code.
Where to Put Your API Code?
First off: you can use your theme's functions.php file, but it's safer to use a child theme's functions.php or a custom plugin. Here's why:
- If you edit your parent theme's
functions.php, any updates to the theme will overwrite your changes—gone forever! - A child theme keeps your custom code separate from the parent theme, so updates don't break your work.
- A custom plugin is even more flexible: you can enable/disable it easily, and it's not tied to your theme at all.
Step-by-Step Implementation
Let's build a basic vehicle search feature using the Marketcheck API you shared.
1. Replace Your API Key
First, swap out {{api-key}} in the API URL with your actual Marketcheck API key. Keep this key secure—don't share it publicly!
2. Add the API Function to Your Site
Add this code to your child theme's functions.php or a custom plugin:
// Function to fetch vehicle data from Marketcheck API function fetch_marketcheck_vehicles($year, $make) { // Replace with your actual API key $api_key = 'YOUR_API_KEY_HERE'; // Build the API request URL $api_url = "http://marketcheck-prod.apigee.net/v2/search/car/active?api_key={$api_key}&year={$year}&make={$make}&host=marketcheck-prod.apigee.net"; // Use WordPress's built-in wp_remote_get for reliable requests $response = wp_remote_get($api_url); // Check if the request was successful if (is_wp_error($response)) { return 'Error fetching vehicle data: ' . $response->get_error_message(); } // Get the response body and parse JSON $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); // Return the parsed data or an error message if (json_last_error() !== JSON_ERROR_NONE) { return 'Error parsing API response.'; } return $data; } // Shortcode to display vehicle search form and results function marketcheck_vehicle_search_shortcode() { ob_start(); // Handle form submission if ($_POST['vehicle_search_submit']) { $year = sanitize_text_field($_POST['year']); $make = sanitize_text_field($_POST['make']); // Fetch vehicles $vehicles = fetch_marketcheck_vehicles($year, $make); // Display results if (is_array($vehicles) && !empty($vehicles['listings'])) { echo '<h3>Search Results</h3>'; echo '<ul>'; foreach ($vehicles['listings'] as $vehicle) { echo '<li>' . $vehicle['year'] . ' ' . $vehicle['make'] . ' ' . $vehicle['model'] . ' - $' . number_format($vehicle['price']) . '</li>'; } echo '</ul>'; } else { echo '<p>No vehicles found, or there was an error.</p>'; if (is_string($vehicles)) { echo '<p>' . $vehicles . '</p>'; } } } // Display search form ?> <form method="post"> <label for="year">Year:</label> <input type="text" id="year" name="year" placeholder="e.g., 2015" required> <label for="make">Make:</label> <input type="text" id="make" name="make" placeholder="e.g., ford" required> <button type="submit" name="vehicle_search_submit">Search Vehicles</button> </form> <?php return ob_get_clean(); } add_shortcode('marketcheck_vehicle_search', 'marketcheck_vehicle_search_shortcode');
3. Use the Shortcode on Your Site
Now, you can add the shortcode [marketcheck_vehicle_search] to any page or post where you want the vehicle search form to appear.
Important Notes
- Cache API Requests: To avoid hitting API rate limits and speed up your site, add caching to the
fetch_marketcheck_vehiclesfunction. You can use WordPress transient functions likeget_transient()andset_transient()to store results for a few hours. - Security: Never hardcode your API key in public repos. Instead, use WordPress's
get_option()to store it in the database (you can add a simple settings page to input it), or use environment variables if you have access to your server. - Error Handling: The example includes basic error handling, but you can expand it to handle specific API response codes (like 401 for invalid API keys, 429 for rate limits) with more user-friendly messages.
内容的提问来源于stack exchange,提问作者Sada Khan

