How to GET data from API and show it on PHP page?

How to GET data from API and show it on PHP page?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Retrieving Exoclick API Data in WordPress Without Curl

Integrating third-party data into a WordPress site requires bridging the gap between a static content management system and dynamic external services. When you encounter an API that expects curl commands, it is often because those examples demonstrate raw HTTP requests. However, making direct curl calls from within a WordPress theme or plugin is rarely the best approach due to security risks, lack of error handling context, and potential conflicts with WordPress's own output buffering.

The most robust solution for this scenario involves using the native PHP cURL extension or leveraging a library like Guzzle, which abstracts the complexity while maintaining compatibility with WordPress environments. Since you prefer not to use curl directly in your code logic (likely due to the verbosity of raw commands), implementing an abstraction layer is the standard engineering practice. This separates the network interaction logic from your presentation layer.

Handling Authentication and Tokens

Your Exoclick API example shows a login request that returns a session token details object. In modern RESTful architectures, relying on long-lived session tokens stored in variables can be risky if not managed correctly. However, since you explicitly prefer using tokens, we will focus on the Authorization: Bearer header pattern shown in your curl example.

To fetch data securely, you must first obtain a valid access token. The code snippet you provided uses a custom Request class. If that crashes your website upon placing it in a random page, it is likely due to one of two reasons: missing class dependencies (the Request class might not be loaded or defined) or output buffering issues common in WordPress when echoing raw data directly to the screen.

Here is how you can refactor that authentication logic using PHP's built-in cURL extension without requiring external classes, ensuring it plays nicely with WordPress:

function exoclick_get_access_token($api_token) {
    $url = 'https://api.exoclick.com/v2/login';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(['api_token' => $api_token]));
    
    // Do not print response directly to avoid breaking WordPress output buffering
    $response_body = curl_exec($ch);
    
    if ($response_body === false) {
        return ['error' => 'cURL failed'];
    }

    return json_decode($response_body, true);
}

Fetching Statistics Data

Once you have the token, your workflow shifts to retrieving the specific data endpoint: https://api.exoclick.com/v2/statistics/publisher/country. The critical detail here is the HTTP header. You cannot simply append the token to the URL; it must be sent via the request headers.

If you attempt to hardcode this into a standard WordPress page template, you risk exposing your token in browser developer tools or causing cache issues. A better pattern is to create a dedicated function that returns the data as an associative array, allowing your PHP logic to handle the JSON parsing before rendering it.

function exoclick_fetch_statistics($token) {
    $url = 'https://api.exoclick.com/v2/statistics/publisher/country?';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, [
        'Accept: application/json',
        'Authorization: Bearer ' . $token
    ]);
    
    $data = curl_exec($ch);
    curl_close($ch);

    if ($data === false) {
        return ['error' => 'Request failed'];
    }

    return json_decode($data, true);
}

Integrating into WordPress

The key to success in a WordPress environment is avoiding direct echo statements inside your API calls. Instead, store the returned data in a variable and let WordPress handle the rendering. For example, if you are using the Gutenberg block editor or a custom plugin page, you can hook this function into wp_enqueue_scripts or call it directly within an init action.

If you are working with Laravel, the ecosystem offers excellent packages like tymon/jwt-auth, but since your stack is WordPress, sticking to native PHP cURL or a lightweight wrapper ensures stability without introducing unnecessary dependencies. Always sanitize the data returned from the API before displaying it on the frontend to prevent XSS vulnerabilities, especially if the API returns user-generated content or dynamic statistics that might be interpreted as HTML.

By encapsulating the network calls into functions and respecting WordPress's output buffering mechanisms, you can securely retrieve Exoclick data without disrupting your site's core functionality.

Our network