I’m having trouble connecting to a RapidAPI service from my WordPress site. I’m trying to get country information from their REST countries API but keep running into issues.
When I try to make the API call, my site just shows a generic technical difficulties message instead of the data I expect. I’ve set up Composer in my local XAMPP environment and added the unirest library to handle HTTP requests.
Here’s the code I’m using to fetch the data:
$response = wp_remote_get( 'https://countries-api-v2.p.rapidapi.com/nations',
array(
'headers' => array(
'X-RapidAPI-Host' => 'countries-api-v2.p.rapidapi.com',
'X-RapidAPI-Key' => 'abc123def456ghi789jkl012mno345pqr678stu'
)
));
if( is_wp_error( $response ) ) {
return false;
}
$content = wp_remote_retrieve_body( $response );
$nations_data = json_decode( $content );
print_r( $nations_data );
I think the headers might not be formatted correctly for the wp_remote_get function. Has anyone successfully integrated RapidAPI services with WordPress? What’s the proper way to pass the required headers?
Your code looks fine, but WordPress is weird with external API calls. Could be timeout issues, SSL problems, or your host blocking requests.
I hit the same wall building a dashboard that pulled from multiple APIs. Instead of fighting wp_remote_get() and hosting headaches, I just used Latenode.
You create a workflow that handles the RapidAPI call, processes everything, then sends clean data to WordPress via webhook or database update. No more PHP HTTP drama or server blocks.
Best part? Error handling, retries, and data transformation all in one visual setup. Need more APIs later? Just drag and drop instead of debugging PHP.
I do all my API integrations this way now. Way more reliable than crossing your fingers with WordPress.
Check it out: https://latenode.com
Headers look fine to me. The problem’s probably in your error handling, not the API call itself. When wp_remote_get fails, it doesn’t always trigger is_wp_error - you need to check the HTTP response code before trying to decode the JSON. I’ve hit this same issue with RapidAPI endpoints where the call succeeds but returns error messages as valid JSON. Add wp_remote_retrieve_response_code($response) and make sure it’s 200. Also, some RapidAPI services need specific user agent strings or extra headers. Try adding ‘user-agent’ to your headers with something like ‘WordPress/5.x’. That generic technical difficulties message means WordPress caught an exception somewhere, so wrap your json_decode in a try-catch block to see what’s actually breaking.
Been there with RapidAPI getting blocked. It’s probably not your headers - likely WordPress security or your host blocking stuff. Add a timeout to wp_remote_get since the default 5 seconds often isn’t enough. Also check if your host has curl enabled and allows outbound connections on 443. Tons of shared hosts silently block API calls to stop abuse. Quick test: make a basic curl request in a standalone PHP file outside WordPress. If that works but wp_remote_get doesn’t, then WordPress or some plugin is messing with it. Oh, and SSL verification fails all the time - try adding ‘sslverify’ => false to your args for testing (don’t leave it like that though).