• ceenee

    (@ceenee)


    Hi all,

    General JSON question for you…

    There is a need on a WordPress site to import JSON data from 3 different REST API URLs. Each of the 3 feeds has the same API key which I can test successfully in Postman outside of WordPress. The goal is to display on a WP page 3 columns, each of which would display a list of values from one specific JSON field in each feed.

    I looked into WP All Import Pro and there is the ability to download a JSON file, but it doesn’t provide the facility to add API keys to the request so the download request fails. I looked through their docs and they do provide customized hooks, but I’m not seeing anything with regard to passing API keys for authorization.

    Can anyone recommend a plugin, or perhaps a direction with WP All Import, that might accomplish this or would this require a custom plugin solution?

    Thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • 4thhubbard

    (@4thhubbard)

    You can create a custom plugin or add code to your theme’s functions.php file to fetch and display the data. Example:

    function fetch_api_data() {
        $urls = [
            'https://api.example.com/data1',
            'https://api.example.com/data2',
            'https://api.example.com/data3',
        ];
    
        $api_key = 'YOUR_API_KEY';
        $responses = [];
    
        foreach ($urls as $url) {
            $response = wp_remote_get($url, [
                'headers' => [
                    'Authorization' => 'Bearer ' . $api_key,
                ],
            ]);
    
            if (is_wp_error($response)) {
                continue; // Handle error as needed
            }
    
            $responses[] = json_decode(wp_remote_retrieve_body($response), true);
        }
    
        return $responses;
    }
    
    add_shortcode('api_data', 'fetch_api_data');
    
    function display_api_data() {
        $data = fetch_api_data();
        ob_start();
        foreach ($data as $item) {
            // Customize output here
            echo '<pre>' . print_r($item, true) . '</pre>';
        }
        return ob_get_clean();
    }

    You can then use the shortcode [api_data] in your posts or pages to display the data.

    You can use the same API key for all three calls without any issues. The key can be included in the headers of each request, as shown in the example.

    This way, all three API calls will successfully authenticate using the same key. Just ensure that the API service allows multiple simultaneous requests with the same key and that you stay within any rate limits imposed by the API.

    4thhubbard

    (@4thhubbard)

    A little further research I found this regarding WP All Import; it may help as well:

    You can use the WP All Import plugin to import data from multiple REST API endpoints, even if they share the same API key. Here’s how you can do it:

    Steps to Import JSON Data Using WP All Import

    1. Install the WP All Import Plugin:
    • If you haven’t already, install and activate the WP All Import plugin (you may need the Pro version for API functionality).
    1. Create a New Import:
    • Go to the WP All Import dashboard and click on “New Import.”
    • Select “From URL” and enter the first API endpoint URL.
    1. Set Up API Request:
    • In the import settings, you can add your API key in the request headers. Look for the “HTTP Headers” section.
    • For example, you might add:
      Authorization: Bearer YOUR_API_KEY
    1. Configure the Import:
    • Follow the wizard to configure your import. Map the JSON fields to the appropriate WordPress fields (like post title, content, custom fields, etc.).
    • Test the import to make sure it pulls in the data correctly.
    1. Repeat for Other Endpoints:
    • To import data from the other two endpoints, you’ll need to create separate import jobs for each URL.
    • Follow the same steps to set the API key and configure the import for each endpoint.
    1. Scheduling and Automation (Optional):
    • If you want to run these imports regularly, you can schedule the imports using WP All Import’s scheduling features.

    Using a Single Import for Multiple URLs

    If you prefer to combine data from all three endpoints into a single import, you might need to create a custom PHP function to fetch and merge the data from all URLs before importing it. This would require some coding within the WP All Import plugin’s custom PHP options. Tips

    • Testing: Make sure to test each import separately to confirm that the data is being retrieved correctly.
    • Error Handling: Monitor for any potential API errors or rate limits that could affect your imports.

    Using WP All Import makes it easier to handle complex data structures and automate imports without extensive coding.

    Thread Starter ceenee

    (@ceenee)

    Mary,

    I can’t thank you enough for both of your helpful posts! I really appreciate the time you took to lay out both options clearly including the potential of using WP All Import. I’m going to try these out this week!

    Thank you again sooo much—you have already made me feel much less stressed. ??

    Cheers!

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.