WordPress wp_remote_get() not working with RapidAPI external service

I’m having trouble connecting to a RapidAPI service from my WordPress site. I want to pull 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 technical difficulties message instead of the data I need. I set up Composer in my local XAMPP environment and added the unirest library to my composer.json:

{
    "require-dev": {
        "mashape/unirest-php": "3.*"
    }
}

Here’s the PHP code I’m using to fetch the data:

$response = wp_remote_get( 'https://world-countries-api.p.rapidapi.com/countries', 
array(
    'headers' => array(
        "X-RapidAPI-Host" => "world-countries-api.p.rapidapi.com",
        "X-RapidAPI-Key" => "abc123def456ghi789jkl012mno345pqr678stu901vwx234yz"
    )
));

if( is_wp_error( $response ) ) {
    return false;
}

$content = wp_remote_retrieve_body( $response );
$countries = json_decode( $content );
print_r($countries);

The API key is valid and I can access the endpoint through other tools. What could be causing this connection problem?

Check your wp_remote_get response code before processing the body. WordPress might get a valid response but with an error status you’re missing. Add $response_code = wp_remote_retrieve_response_code($response); after your wp_remote_get call and make sure it returns 200. I’ve seen RapidAPI return 401 or 403 errors that don’t trigger is_wp_error() but still break things. Also, some WordPress hosts have aggressive security plugins that block external API calls. Try disabling security plugins like Wordfence or Sucuri temporarily to see if they’re interfering. Check your PHP error logs too - that technical difficulties message usually hides the actual PHP errors that would tell you what’s breaking.

Had the same problem with RapidAPI calls timing out on WordPress. Your code looks fine, but WordPress can be weird with external API calls and fail silently. First, check your server’s SSL setup - lots of hosts block outbound connections by default. Test by adding ‘sslverify’ => false to your wp_remote_get arguments (just don’t leave it there). Also check if your host allows connections to RapidAPI’s IP ranges. The default timeout’s probably too short too - try ‘timeout’ => 30 in your arguments. Turn on WordPress debugging with define(‘WP_DEBUG’, true) in wp-config.php. You’ll get actual error messages instead of that generic technical difficulties page, which should tell you what’s really going wrong.

Turn on WordPress debug logging first - you’ll see what’s actually breaking. Drop define(‘WP_DEBUG_LOG’, true) into wp-config and check /wp-content/debug.log after it fails. RapidAPI can be picky about user-agent headers too. WordPress’s default might get blocked, so try adding ‘user-agent’ => ‘YourSite/1.0’ to your headers array.