Hey everyone, I’m stuck trying to pull info from an external API into my WordPress site. I’m using the Rest Countries API from RapidAPI, but I’m hitting a wall.
I’ve set up my XAMPP environment with Composer and added the Unirest PHP library. Here’s what my composer.json
looks like:
{
"require-dev": {
"apiconnect/php-client": "^2.0"
}
}
In my WordPress code, I’m using wp_remote_get()
to make the API call:
$api_response = wp_remote_get('https://country-data-api.example.com/all', [
'headers' => [
'API-Host' => 'country-data-api.example.com',
'API-Key' => 'your_api_key_here'
]
]);
if (!is_wp_error($api_response)) {
$response_body = wp_remote_retrieve_body($api_response);
$country_data = json_decode($response_body);
var_dump($country_data);
} else {
echo 'API request failed';
}
But all I’m getting is a ‘technical difficulties’ error. Any ideas what I’m doing wrong or how to debug this? Thanks in advance for any help!
hey man, ive had similar issues w/ API stuff. make sure ur using the right headers for RapidAPI - X-RapidAPI-Key and X-RapidAPI-Host. also check if ur hosting provider blocks outgoing connections. u can test w/ curl or postman. if that doesnt work, try enabling WP debugging in wp-config.php to get more details. good luck!
Having dealt with similar API integration issues, I can offer some insights. First, ensure your API credentials are correct and up-to-date. RapidAPI often requires specific headers, so double-check you’re using ‘X-RapidAPI-Key’ and ‘X-RapidAPI-Host’ in your request.
Next, verify your server can actually reach the API endpoint. Sometimes, hosting providers block outgoing connections. A quick test with cURL or a tool like Postman can confirm this.
If those check out, enable WordPress debugging by adding these lines to your wp-config.php:
define(‘WP_DEBUG’, true);
define(‘WP_DEBUG_LOG’, true);
define(‘WP_DEBUG_DISPLAY’, false);
This will log errors to a debug.log file in wp-content, providing more details about the ‘technical difficulties’ you’re encountering.
Lastly, consider increasing the timeout for your API request. Sometimes, slow responses can cause issues. Add a ‘timeout’ parameter to your wp_remote_get() call, like this:
$api_response = wp_remote_get(‘YOUR_API_URL’, [
‘headers’ => […],
‘timeout’ => 30
]);
As someone who’s integrated quite a few APIs into WordPress sites, I can tell you that ‘technical difficulties’ errors can be frustrating. From what I see in your code, there are a couple of things that stand out to me.
First, your composer.json file doesn’t seem to include the Unirest library you mentioned. You might want to add “mashape/unirest-php”: “^3.0” to your require section.
Secondly, your API call structure looks a bit off. For RapidAPI, you typically need to set the ‘X-RapidAPI-Key’ and ‘X-RapidAPI-Host’ headers. Try modifying your wp_remote_get() call like this:
$api_response = wp_remote_get('https://restcountries-v1.p.rapidapi.com/all', [
'headers' => [
'X-RapidAPI-Host' => 'restcountries-v1.p.rapidapi.com',
'X-RapidAPI-Key' => 'your_actual_api_key_here'
]
]);
Also, enable WordPress debugging to get more detailed error messages. Add these lines to your wp-config.php:
define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
This will log errors to a debug.log file in your wp-content directory, which should give you more insight into what’s going wrong. Hope this helps!